【发布时间】:2016-12-13 03:35:49
【问题描述】:
我正在尝试实现逻辑回归,但我收到了错误的绘图。
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn import cross_validation
from sklearn.linear_model import LogisticRegression
sns.set()
x = (np.random.randint(2000, size=400)).reshape((400,1))
y = (np.random.randint(2, size=400)).reshape((400,1)).ravel()
x_train, x_test, y_train, y_test = cross_validation.train_test_split(x, y, test_size=0.4, random_state=0)
logistic_regr = LogisticRegression()
logistic_regr.fit(x_train, y_train)
fig, ax = plt.subplots()
ax.set(xlabel='x', ylabel='y')
ax.plot(x_test, logistic_regr.predict_proba(x_test), label='Logistic regr')
#ax.plot(x_test,logistic_regr.predict(x_test), label='Logistic regr')
ax.legend()
我收到以下情节:
如果我使用:
ax.plot(x_test,logistic_regr.predict(x_test), label='Logistic regr')
我收到了:
【问题讨论】:
-
你的回归预测总是
0,这就是你有这个情节的原因。您的训练数据是完全随机的,您的目标仅由0和1组成,并且您希望它是线性回归。所以回归是一条线,它预测要么总是 0,要么总是 1。 -
@MMF:Hmm.Right!我的目标必须位于 [0,1] 之间,因为它是概率。如果我尝试作为目标
np.linspace(0,1,400).ravel()它会抛出Unknown label type -
但问题是您只有
0或1。不是介于两者之间的值。np.random.randint( )只返回整数 -
@MMF: 我更新了我的评论
-
@MMF: 使用
logistic_regr.predict_proba不应该找到 [0,1] 之间的概率吗?不管我的目标是什么?
标签: python scikit-learn