gpt4 book ai didi

python - 使用逻辑回归来预测参数值

转载 作者:行者123 更新时间:2023-11-30 09:27:19 25 4
gpt4 key购买 nike

我已经使用逻辑回归编写了各种基本的 sklearn 代码来预测值。

训练数据看起来像 -

https://gist.github.com/anonymous/563591e0395e8d988277d3ce63d7438f

date    hr_of_day   vals
01/05/2014 9 929
01/05/2014 10 942
01/05/2014 11 968
01/05/2014 12 856
01/05/2014 13 835
01/05/2014 14 885
01/05/2014 15 945
01/05/2014 16 924
01/05/2014 17 914
01/05/2014 18 744
01/05/2014 19 377
01/05/2014 20 219
01/05/2014 21 106

我从训练数据中选择了前 8 项来验证分类器

我想预测vals的值,在测试数据中,我将其设置为0。这是正确的吗?

date    hr_of_day   vals
2014-05-01 0 0
2014-05-01 1 0
2014-05-01 2 0
2014-05-01 3 0
2014-05-01 4 0
2014-05-01 5 0
2014-05-01 6 0
2014-05-01 7 0

我的模型代码工作正常。但我的结果看起来很奇怪。我期望结果为 vals 值。相反,我得到的大型矩阵的所有元素值为 0.00030676

如果有人可以提供详细信息或帮助我更好地利用此结果,我将不胜感激。

import pandas as pd
from sklearn import datasets
from sklearn import metrics
from sklearn.linear_model import LogisticRegression
from datetime import datetime, date, timedelta
Train = pd.read_csv("data_scientist_assignment.tsv", sep='\t', parse_dates=['date'])
Train['timestamp'] = Train.date.values.astype(pd.np.int64)
x1=["timestamp", "hr_of_day"]
test=pd.read_csv("test.tsv", sep='\t', parse_dates=['date'])
test['timestamp'] = test.date.values.astype(pd.np.int64)
print(Train.columns)
print(test.columns)
model = LogisticRegression()
model.fit(Train[x1], Train["vals"])
print(model)
print model.score(Train[x1], Train["vals"])
print model.predict_proba(test[x1])

结果如下所示:

In [92]: print(model)
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,
penalty='l2', random_state=None, solver='liblinear', tol=0.0001,
verbose=0, warm_start=False)

In [93]: print model.score(Train[x1], Train["vals"])
0.00520833333333

In [94]:

In [94]: print model.predict_proba(test[x1])
[[ 0.00030676 0.00030676 0.00030676 ..., 0.00030889 0.00030885
0.00030902]
[ 0.00030676 0.00030676 0.00030676 ..., 0.00030889 0.00030885
0.00030902]
[ 0.00030676 0.00030676 0.00030676 ..., 0.00030889 0.00030885
0.00030902]
...,
[ 0.00030676 0.00030676 0.00030676 ..., 0.00030889 0.00030885
0.00030902]
[ 0.00030676 0.00030676 0.00030676 ..., 0.00030889 0.00030885
0.00030902]
[ 0.00030676 0.00030676 0.00030676 ..., 0.00030889 0.00030885
0.00030902]]

最佳答案

  1. 您正在使用 predict_proba() 来给出类概率,而不是您应该使用 predict()> 功能。
  2. 您使用的模型错误。数据中的目标变量具有连续数据,因此您必须使用线性回归逻辑回归实际上充当分类器,分类任务需要离散数据(如固定数量的标签)。

关于python - 使用逻辑回归来预测参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39444955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com