前提・実現したいこと
パーセプトロンのコードを理解したいです.パーセプトロンの仕組み自体はおおよそ理解したのですが、コードにおいてわからないところがありましたので、質問します。
発生している問題・エラーメッセージ
一部の変数について不明な点がありました.下記コードを参照した後に見てほしいのですが、fitメソッドの定義のところに
X : {array-like}, shape = [n_samples, n_features]
Training vectors, where n_samples is the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples]
Target values.
というコメントがあります。このコメントが何を意味しているのかわかりません。
・コメントで配列のようである、と書いてあるが、ならその後のshapeは何なのか
・コード中で、self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape[1])のようにXをクラスのように扱う一方、for xi, target in zip(X, y):のようにイテラブルとしても扱っているように見えるが、これはどういうことなのか
・コメントのXのarray-likeには中括弧があるが、Yにはない、なぜなのか
等々の疑問があります。
該当のソースコード
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_ = [] 118 119 for _ in range(self.n_iter): 120 errors = 0 121 for xi, target in zip(X, y): 122 update = self.eta * (target - self.predict(xi)) 123 self.w_[1:] += update * xi 124 self.w_[0] += update 125 errors += int(update != 0.0) 126 self.errors_.append(errors) 127 return self
補足情報(FW/ツールのバージョンなど)
コードは途中までしか載せておりません。(全体のコードはここ)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/08/11 13:55