質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

10194閲覧

使いたいメソッドがなぜか存在しない扱いになってしまっている

退会済みユーザー

退会済みユーザー

総合スコア0

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

1クリップ

投稿2018/08/10 13:33

編集2018/08/10 13:38

前提・実現したいこと

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です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tachikoma

2018/08/10 13:39

pylintが見つけられてないだけで、コードは動くんじゃないですかね。
退会済みユーザー

退会済みユーザー

2018/08/10 13:48

よく見ればコードは動いているようです。少し調べてみます。
guest

回答2

0

ベストアンサー

定義場所はここですね。

https://github.com/numpy/numpy/blob/master/numpy/random/mtrand/mtrand.pyx#L593

cdefはcythonのキーワードですね。pip等でインストールしたものはビルと済みのライブラリをコピーするだけなので、mtrandの中に何が入っているか静的解析では知りようがないっすね。

投稿2018/08/10 14:02

tachikoma

総合スコア3601

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hayataka2049

2018/08/10 14:08 編集

cythonですか、なるほど from ~~ import * 自体はちゃんと解析されるんですかね?
tachikoma

2018/08/10 14:19

cythonだけはwrapperとして使ってるだけですかね。fromも解析遠てると思いますが、__all__が見えてないと静的解析のやりようがない気がします。
退会済みユーザー

退会済みユーザー

2018/08/10 14:46

んー…。調べたんですけど何かよくわかんないです…。本当にざっくりで大丈夫なので、何で静的解析できないか教えてもらえませんか?
tachikoma

2018/08/10 15:03

macの環境での話ですが、site-packages/numpy/randomの中にmtrand.cpython-36m-darwin.soという共有ライブラリがあると思います。Windowsだとdllでしょうか。 これが該当の関数を持ったコンパイル済みのライブラリなのですが、テキストではないので静的解析ツールはこの中身を見ることが出来ません。一方、Python自体は共有ライブラリに入っている関数を読み込む方法を知っています。これが実行できるけど、静的解析が通らない原因です。
退会済みユーザー

退会済みユーザー

2018/08/10 16:40 編集

ああそうなのですか。おおよその意味はわかりました。わかりやすく説明してくれてありがとうございました。
guest

0

エラーはpylintが出しているようですが、そのまま実行するとどうなりますか?

静的解析で追えていないだけで、実体としてはあるんじゃないかという気がします。

追記

~~numpyの実装を見に行ったら、なんかお行儀悪いことしてる・・・
~~
numpy/init.py at master · numpy/numpy · GitHub

~~これのせいでしょうね。
~~
対策は「無視」でいいです。pylintのエラーが出ても、実害はありません。

追記2

と思ったけど、間違いかもしれません(追記1に関して)。

tachikomaさんの回答の方が的を射ている気がしますが、未確認なのでとりあえず上に関しては保留します。

投稿2018/08/10 13:40

編集2018/08/10 14:57
hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/08/10 13:47 編集

実行時に調べてみると、エラーは起こりませんでした。なぜ存在しない扱いをしてしまうのか、どうすれば直せるのかはよくわかりませんが、少しpylintについて調べてみます。
hayataka2049

2018/08/10 13:51 編集

こいつの実体はmtrand.RandomStateで、違うところに書いてあるモジュールをnp.randomの名前空間に突っ込んでるんですね __init__.pyに from hoge import Fuga, Piyo と書いてあったら、静的解析で「そのモジュールにはFugaとPiyoがあるのね」と理解するのは簡単です。おそらくそれは大抵のツールはやってくれると思います。 今回のケースのように from hoge import * だと、少なくともpylintは世話してくれないのでしょうね。
退会済みユーザー

退会済みユーザー

2018/08/10 14:52 編集

ワイルドカードインポートは知らなかったです。色々見た感じだと確かに名前の混乱とか起きそうであんまりよくなさそうな方法ですねー。ですが、一応こっちの環境で適当なモジュールについてワイルドカードインポートするプログラム書いたら大丈夫だったので、多分別の原因があるのだと思います(素人目ですし、あくまで再現ですが)。ですので、多分もう一方の方の回答が原因だと思われます。色々参考になりました。ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問