感知器类:系列的真值不明确

时间:2019-06-15 20:11:54

标签: python-3.x machine-learning perceptron

这是我的代码。尝试运行“适合”功能时出现错误。这是乳腺癌数据集。 x和y在代码中显示。我使用Train-test split创建了x_train和y_train。请注意,type(x_train)为“ numpy.ndarray”,type(y_train)为“ pandas.core.series.Series”。

我曾尝试将y_train转换为numpy.ndarray后运行代码,但仍然无法正常工作。

import numpy as np
import pandas as pd

breast_cancer = sklearn.datasets.load_breast_cancer()
data = pd.DataFrame(breast_cancer.data, columns = breast_cancer.feature_names)
data['class'] = breast_cancer.target

x = data.drop('class', axis = 1)
y = data['class']

class Perceptron:

  def __init__(self):
    self.w = None
    self.b = None

  def model(self, X):
    return 1 if (np.dot(self.w, X) >= self.b) else 0

  def predict(self, x):
    y = []
    for X in x:
      result = self.model(X)
      y.append(result)
    return np.array(y)     

  def fit(self, x, y):

    self.w = np.ones(x.shape[1])
    self.b = 0

    for X, Y in zip(x, y):
      y_pred = self.model(X)
      if y == 1 and y_pred == 0:
        self.w = self.w + x
        self.b = self.b + 1
      elif y == 0 and y_pred == 1:  
        self.w = self.w - x
        self.b = self.b - 1

percep = Perceptron()
percep.fit(x_train,y_train)```


percep.fit(x_train, y_train)

ValueError                                Traceback (most recent call last)

<ipython-input-67-5570c9a29c08> in <module>()
----> 1 percep.fit(x_train, y_train)

1 frames

/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in __nonzero__(self)
   1476         raise ValueError("The truth value of a {0} is ambiguous. "
   1477                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1478                          .format(self.__class__.__name__))
   1479 
   1480     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

0 个答案:

没有答案