具有 python 的感知器 - 出现类型错误,但为什么呢?

Perceptron with python - TypeError appears but why?

我正在尝试在 python3 中编写感知器算法。我正在关注 Sebastian Raschka 的一本书示例。他的代码可以在这里找到:(https://github.com/rasbt/python-machine-learning-book-2nd-edition).

不幸的是我无法弄清楚错误的原因: TypeError: object() 没有参数 出现以及如何处理。

我首先使用了 PyCharm,现在我正在逐步使用 Jupiter 测试该问题。我什至从 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 = Perceptron(eta...) etc." 的部分.我忘了任何图书馆吗?我只是不明白... 非常感谢

您定义了 class Perzeptron 但创建了 Perceptron 的实例(c 而不是 z)。似乎您在 ipython 会话中早些时候定义了 Perceptron 而没有定义采用两个参数的 __init__ 方法。

我仍然不知道答案,但我在我的 windows 环境中的 PyCharm 版本中输入了相同的代码并且它起作用了。 所以,我不确定如何关闭问题。可能是其中一位管理员能够关闭此线程?或者让我知道如何自己做。