使用SciKit Learn进行Logisitc回归

jos*_*ine 0 python regression scikit-learn

有人可以帮我调试这段代码吗?谢谢!

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)
Run Code Online (Sandbox Code Playgroud)

给我以下错误:

---------------------------------------------------------------------------
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)
Run Code Online (Sandbox Code Playgroud)

Mat*_*ury 5

代替

clf = linear_model.LogisticRegression
Run Code Online (Sandbox Code Playgroud)

你要

clf = linear_model.LogisticRegression()
Run Code Online (Sandbox Code Playgroud)

在第一种情况下,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)
Run Code Online (Sandbox Code Playgroud)

试图说.