Bug: TypeError: fit() missing 1 required positional argument: 'y'

In [1]: from sklearn import svm

In [2]: from sklearn import datasets

In [3]: clf = svm.SVC

In [4]: iris = datasets.load_iris()

In [5]: x, y = iris.data, iris.target

In [6]: clf.fit(x,y)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
6-a709f157a302> in ()
----> 1 clf.fit(x,y)

TypeError: fit() missing 1 required positional argument: 'y'

机器学习模型函数调用普通函数时,需加“()”,或者实例化时加(),不然会报错。

In [7]: clf().fit(x,y)
Out[7]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape=’ovr’, degree=3, gamma=’auto’, kernel=’rbf’,
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)

In [26]: from sklearn import svm

In [27]: from sklearn import datasets

In [28]: clf = svm.SVC()

In [29]: iris = datasets.load_iris()

In [30]: x,y = iris.data, iris.target

In [31]: clf.fit(x,y)
Out[31]:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
  decision_function_shape='ovr', degree=3, gamma='auto', kernel='rbf',
  max_iter=-1, probability=False, random_state=None, shrinking=True,
  tol=0.001, verbose=False)

In [32]: import pickle

In [33]: s = pickle.dumps(clf)

In [34]: clf2 = pickle.loads(s)

In [35]: clf2.predict(x[0:1])
Out[35]: array([0])

In [36]: y[0]
Out[36]: 0

你可能感兴趣的:(python)