Python 获取预测值的百分比
使用基本逻辑回归,我预测了0和2个值 数据帧具有下一个结构:Python 获取预测值的百分比,python,pandas,Python,Pandas,使用基本逻辑回归,我预测了0和2个值 数据帧具有下一个结构: Duration | y 12.45 | 0 123.66 | 0 0.34 | 2 14.69 | 2 逻辑回归: x = DATA.Duration.values.reshape(-1,1) y = DATA.y.values.reshape(-1,1) lgr = LogisticRegression(max_iter = 200) lgr.fit(x,y) lgr.score(x,y) X =
Duration | y
12.45 | 0
123.66 | 0
0.34 | 2
14.69 | 2
逻辑回归:
x = DATA.Duration.values.reshape(-1,1)
y = DATA.y.values.reshape(-1,1)
lgr = LogisticRegression(max_iter = 200)
lgr.fit(x,y)
lgr.score(x,y)
X = np.arange(0,200,10).reshape(-1,1)
Y = lgr.predict(X)
如果要绘制结果,我会得到如下图片:我的目标是计算线(预测)分别“覆盖”的红色和蓝色圆点的数量 我采用了下一种方法:
(np.count_nonzero(Y == 0)*100)/np.count_nonzero(y == 0)
(np.count_nonzero(Y == 2)*100)/np.count_nonzero(y == 2)
但它给出了奇怪的结果。获得所需百分比的正确方法是什么?使用sklearn:
是的
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression().fit(X, y)
clf.predict(X)