使用SciKit Learn进行Logisitc回归

时间:2016-03-04 02:34:49

标签: python scikit-learn regression

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

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)

1 个答案:

答案 0 :(得分:5)

而不是

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)

试图说。