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)
<ipython-input-6-a709f157a302> in <module>()
----> 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
机器学习模型调用错误:TypeError: fit() missing 1 required positional argument: 'y'
在使用机器学习模型时,如果在函数调用或实例化时不正确地添加参数,可能会遇到TypeError。例如,`clf().fit(x,y)`会导致缺少参数'y'的错误。确保正确地传递所有必需的参数来避免这类问题。"
134812975,9640242,Python模块学习:从定义到内置函数,"['Python', '编程基础', '模块化', '内置模块']
1114

被折叠的 条评论
为什么被折叠?



