有人可以帮我调试这段代码吗?谢谢!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn import linear_model
data = pd.read_csv('Mower.csv')
data = data.values
y = data[:,2]
x = data[:,:2]
y_train = y[:int(0.3*len(y))]
x_train = x[:int(0.3*len(y)),:]
y_validate = y[int(0.3*len((y))):]
x_validate = x[int(0.3*len((y))):,:]
clf = linear_model.LogisticRegression
clf.fit(x_train,y_train)
y_hat = clf.predict(x_validate)
给我以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-77-a0a54feba3ef> in <module>()
1 clf = linear_model.LogisticRegression
----> 2 clf.fit(x_train,y_train)
3 y_hat = clf.predict(x_validate)
TypeError: unbound method fit() must be called with LogisticRegression instance as first argument (got ndarray instance instead)
而不是
clf = linear_model.LogisticRegression
你想要
clf = linear_model.LogisticRegression()
在第一种情况下,clf
设置为等于类linear_model.LogisticRegression
,但在第二种情况下,它设置为等于类linear_model.LogisticRegression
的一个实例。
当您调用clf.fit(...)
时,它期望类linear_model.LogisticRegression
的实例作为第一个参数。 如果clf
是一个类,那么它不会自动传递到第一个参数中,因此 fit
方法会x_train
查找类ndarray
的实例。 然后它抱怨,因为它期待类linear_model.LogisticRegression
的实例。
这就是这个
unbound method fit() must be called with LogisticRegression instance as first argument (got ndarray instance instead)
是想说的。