前提・実現したいこと
numpy.randomでRandomStateメソッドを使いたいです。
発生している問題・エラーメッセージ
[pylint] E1101:Module 'numpy.random' has no 'RandomState' member
該当のソースコード
Python
1# coding: utf-8 2 3 4import numpy as np 5import pandas as pd 6import matplotlib.pyplot as plt 7from matplotlib.colors import ListedColormap 8 9# *Python Machine Learning 2nd Edition* by [Sebastian Raschka](https://sebastianraschka.com), Packt Publishing Ltd. 2017 10# 11# Code Repository: https://github.com/rasbt/python-machine-learning-book-2nd-edition 12# 13# Code License: [MIT License](https://github.com/rasbt/python-machine-learning-book-2nd-edition/blob/master/LICENSE.txt) 14 15# # Python Machine Learning - Code Examples 16 17# # Chapter 2 - Training Machine Learning Algorithms for Classification 18 19# Note that the optional watermark extension is a small IPython notebook plugin that I developed to make the code reproducible. You can just skip the following line(s). 20 21 22 23 24 25# *The use of `watermark` is optional. You can install this IPython extension via "`pip install watermark`". For more information, please see: https://github.com/rasbt/watermark.* 26 27# ### Overview 28# 29 30# - [Artificial neurons – a brief glimpse into the early history of machine learning](#Artificial-neurons-a-brief-glimpse-into-the-early-history-of-machine-learning) 31# - [The formal definition of an artificial neuron](#The-formal-definition-of-an-artificial-neuron) 32# - [The perceptron learning rule](#The-perceptron-learning-rule) 33# - [Implementing a perceptron learning algorithm in Python](#Implementing-a-perceptron-learning-algorithm-in-Python) 34# - [An object-oriented perceptron API](#An-object-oriented-perceptron-API) 35# - [Training a perceptron model on the Iris dataset](#Training-a-perceptron-model-on-the-Iris-dataset) 36# - [Adaptive linear neurons and the convergence of learning](#Adaptive-linear-neurons-and-the-convergence-of-learning) 37# - [Minimizing cost functions with gradient descent](#Minimizing-cost-functions-with-gradient-descent) 38# - [Implementing an Adaptive Linear Neuron in Python](#Implementing-an-Adaptive-Linear-Neuron-in-Python) 39# - [Improving gradient descent through feature scaling](#Improving-gradient-descent-through-feature-scaling) 40# - [Large scale machine learning and stochastic gradient descent](#Large-scale-machine-learning-and-stochastic-gradient-descent) 41# - [Summary](#Summary) 42 43 44 45 46 47 48# # Artificial neurons - a brief glimpse into the early history of machine learning 49 50 51 52 53 54# ## The formal definition of an artificial neuron 55 56 57 58 59 60# ## The perceptron learning rule 61 62 63 64 65 66 67 68 69 70 71# # Implementing a perceptron learning algorithm in Python 72 73# ## An object-oriented perceptron API 74 75 76 77 78 79class Perceptron(object): 80 """Perceptron classifier. 81 Parameters 82 ------------ 83 eta : float 84 Learning rate (between 0.0 and 1.0) 85 n_iter : int 86 Passes over the training dataset. 87 random_state : int 88 Random number generator seed for random weight 89 initialization. 90 Attributes 91 ----------- 92 w_ : 1d-array 93 Weights after fitting. 94 errors_ : list 95 Number of misclassifications (updates) in each epoch. 96 """ 97 def __init__(self, eta=0.01, n_iter=50, random_state=1): 98 self.eta = eta 99 self.n_iter = n_iter 100 self.random_state = random_state 101 102 def fit(self, X, y): 103 """Fit training data. 104 Parameters 105 ---------- 106 X : {array-like}, shape = [n_samples, n_features] 107 Training vectors, where n_samples is the number of samples and 108 n_features is the number of features. 109 y : array-like, shape = [n_samples] 110 Target values. 111 Returns 112 ------- 113 self : object 114 """ 115 rgen = np.random.RandomState(self.random_state) 116 self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1]) 117 self.errors_ = []
試したこと
色々調べたのですが、原因がわかりませんでした。numpyはインポートしているのに、何故使えないのか見当がつきません。
補足情報(FW/ツールのバージョンなど)
今現在はパーセプトロンを理解したいための勉強中なので、コードは途中までしか入力していません。従って、問題が解決しても、実際にはこのコードを実行しても特に何も起こりません。(コード全体のURLはhttps://github.com/rasbt/python-machine-learning-book-2nd-edition/blob/master/code/ch02/ch02.py)
開発環境はVisual Studio Codeです。
pylintが見つけられてないだけで、コードは動くんじゃないですかね。
よく見ればコードは動いているようです。少し調べてみます。
回答2件
あなたの回答
tips
プレビュー