我试图代码python3一个感知算法。我下面从塞巴斯蒂安Raschka一本书的例子。他的代码可以在这里找到:(https://github.com/rasbt/python-machine-learning-book-2nd-edition)。
不幸的是我无法弄清楚,为什么错误:类型错误:对象()不带任何参数出现,以及如何处理它。
我已经第一次使用PyCharm,现在我正在测试与木星步步这个问题。我甚至从S. Raschka提供GitHub的版本库中复制完全的代码示例。但比我得到了同样的错误,这实际上是困惑我,因为这意味着它可能不只是一个错字。
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
class Perceptron(object):
""" Perzeptron Klassifizierer
Parameter
---------
eta : float
Lernrate (zwischen 0.0 und 1.0)
n_iter : int
Durchläufe der Trainningsdatenmenge
Attribute
---------
w_ : 1d-array
Gewichtugen nach Anpassungen
errors_ : list
Anzahl der Fehlerklassifizerungen pro Epoche
"""
def __init__(self, eta=0.01, n_iter=10):
self.eta = eta
self.n_iter = n_iter
def fit(self, X, y):
""" Anpassungen and die Trainingsdaten
Parameter
---------
X : {array-like}, shape = [n_samples, n_features]
Trainingsvektoren, n_samples ist
die Anzahl der Objekte und
n_features ist die Anzahl der Merkmale
y : array-like, shape = [n_samples]
Zielwerte
Rückgabewert
------------
self : object
"""
self.w_ = np.zeros(1 + X.shape[1])
self.errors_ = []
for _ in range(self.n_iter):
errors = 0
for xi, target in zip(X, y):
update = self.eta * (target - self.predict(xi))
self.w_[1:] += update * xi
self.w_[0] += update
errors += int(update != 0.0)
self.errors_.append(errors)
return self
def net_input(self, X):
""" Nettoeingabe berechnen"""
return np.dot(X, self.w_[1:]) + self.w_[0]
def predict(self, X):
"""Klassenbezeichnung zurückgeben"""
return np.where(self.net_input(X) >= 0.0, 1, -1)
df = pd.read_csv('https://archive.ics.uci.edu/ml/machine-learning- databases/iris/iris.data', header=None)
df.tail()
# Expected result:
# A table with given numbers will be shown
# Now we are plotting everything and will see a given chart:
y = df.iloc[0:100, 4].values
y = np.where(y == 'Iris-setosa', -1, 1)
X = df.iloc[0:100, [0, 2]].values
plt.scatter(X[:50, 0], X[:50, 1], color='red', marker='o', label='setosa')
plt.scatter(X[50:100, 0], X[50:100, 1], color='blue', marker='x', label='versicolor')
plt.xlabel('Länge des Kelchblatts [cm]')
plt.ylabel('Länge des Blütenblatts [cm]')
plt.legend(loc='upper left')
plt.show()
#Error appears here:
ppn = Perceptron(eta=0.1, n_iter=10)
ppn.fit(X, y)
plt.plot(range(1, len(ppn.errors_) + 1), ppn_errors_,
marker='o')
plt.xlabel('Epochen')
plt.ylabel('Anzahl der Updates')
plt.show()
The given Error tells me the following"
---------------------------------------------------------------------------
TypeError Traceback (most recent call >>last)
<ipython-input-29-abc085daeef7> in <module>
----> 1 ppn = Perceptron(eta=0.1, n_iter=10)
2 ppn.fit(X, y)
3 plt.plot(range(1, len(ppn.errors_) + 1), ppn_errors_,
4 marker='o')
5 plt.xlabel('Epochen')
TypeError: object() takes no parameters
------------------------------------------------------------------------
如上所示,该代码工作,直到最后几行并取决于与部分“PPN =感知器(ETA ...)等。”我预期另一个情节,一个图,其中假分类器在相反的历元的量的量。难道我忘了任何库?我只是不明白这一点......非常感谢
您定义的类Perzeptron
但创建Perceptron
的一个实例(C,而不是Z)。好像你刚才在您的IPython会话中定义Perceptron
没有确定__init__
方法取两个参数。
我还是不知道答案,但我已经进入了相同的代码在我的PyCharm版本我的Windows环境中,它的工作。所以,我不知道如何关闭的问题。可能是管理员之一是能够关闭这个线程?还是让我知道如何自己做。