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

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

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

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

Q&A

解決済

2回答

3758閲覧

パーセプトロンにおいてよくわからないデータ構造があった

退会済みユーザー

退会済みユーザー

総合スコア0

Python 3.x

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

0グッド

0クリップ

投稿2018/08/11 13:00

編集2018/08/11 13:04

前提・実現したいこと

パーセプトロンのコードを理解したいです.パーセプトロンの仕組み自体はおおよそ理解したのですが、コードにおいてわからないところがありましたので、質問します。

発生している問題・エラーメッセージ

一部の変数について不明な点がありました.下記コードを参照した後に見てほしいのですが、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/ツールのバージョンなど)

コードは途中までしか載せておりません。(全体のコードはここ)

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

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

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

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

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

guest

回答2

0

scikit-learnのfitのメソッドにある形式と同じですので、参考までにドキュメントのリンクを置いておきます。

LinearRegressionのfit

投稿2018/08/11 13:44

tachikoma

総合スコア3601

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

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

退会済みユーザー

退会済みユーザー

2018/08/11 13:55

ありがとうございます。参考にします。
guest

0

ベストアンサー

array-likeというのは配列のように扱えるオブジェクトということで、pythonのlistやnumpyの配列などが該当します。

shape = [n_samples, n_features]は、n_samples行n_features列の2次元配列を渡せということですね。

属性を持ち、かつiterableというのはこれといって不自然な点はないと思いますが・・・どういう点がわからないのか明確にしてください。

中括弧についてはよくわかりません。

投稿2018/08/11 13:12

hayataka2049

総合スコア30933

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

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

退会済みユーザー

退会済みユーザー

2018/08/11 13:48

調べてみたところ、__iter__メソッドなどでイテラブルなクラスがつくれるようですね。知らなかったです。回答ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問