machine-learning - 根据两个参数预测值

标签 machine-learning classification logistic-regression

我正在尝试学习数值分析。我正在关注这篇文章 - http://scikit-learn.org/stable/auto_examples/linear_model/plot_iris_logistic.html

我的数据如下所示:

date    hr_of_day   vals
2014-05-01  0   72
2014-05-01  1   127
2014-05-01  2   277
2014-05-01  3   411
2014-05-01  4   666
2014-05-01  5   912
2014-05-01  6   1164
2014-05-01  7   1119
2014-05-01  8   951
2014-05-01  9   929
2014-05-01  10  942
2014-05-01  11  968
2014-05-01  12  856
2014-05-01  13  835
2014-05-01  14  885
2014-05-01  15  945
2014-05-01  16  924
2014-05-01  17  914
2014-05-01  18  744
2014-05-01  19  377
2014-05-01  20  219
2014-05-01  21  106
2014-05-01  22  56
2014-05-01  23  43
2014-05-02  0   61

对于给定的日期和小时,我想预测vals并识别模式。

我写了这段代码:

import pandas as pd
from sklearn import datasets
from sklearn import metrics
from sklearn.linear_model import LogisticRegression

# read the data in
Train = pd.read_csv("data_scientist_assignment.tsv")
#print df.head()
x1=["date", "hr_of_day", "vals"]
#print x1
#print df[x1]
test=pd.read_csv("test.tsv")


model = LogisticRegression()
model.fit(Train[x1], Train["vals"])
print(model)
print model.score(Train[x1], Train["vals"])

print model.predict_proba(test[x1])

我收到此错误:

KeyError: "['date' 'hr_of_day' 'vals'] not in index"

这是什么问题。有没有更好的方法来做到这一点?

测试文件格式:

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

全部错误赌注:

Traceback (most recent call last):
  File "socratis.py", line 16, in <module>
    model.fit(Train[x1], Train["vals"])
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 1986, in __getitem__
    return self._getitem_array(key)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2030, in _getitem_array
    indexer = self.ix._convert_to_indexer(key, axis=1)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexing.py", line 1210, in _convert_to_indexer
    raise KeyError('%s not in index' % objarr[mask])
KeyError: "['date' 'hr_of_day' 'vals'] not in index"

最佳答案

我建议提供sep='\t' parameter读取 TSV 时:

Train = pd.read_csv("data_scientist_assignment.tsv", sep='\t') # use TAB as column separator

修复此问题后,队列中存在另一个问题:ValueError: Could not conversion string to float: '2014-09-13' 这是因为线性回归需要数字特征,而列 date 是字符串类型。

您可以通过将日期转换为时间戳(自纪元以来的秒数)来引入新列timestamp并将其用作功能:

Train['timestamp'] = pd.to_datetime(Train['date']).apply(lambda a: a.timestamp())
x1=["timestamp", "hr_of_day", "vals"]

从机器学习的角度来看,您不应使用目标值 vals 作为输入特征。您还应该考虑将日期表示为单独的特征:日、月、年;或星期几,这取决于您想要建模的内容。

关于machine-learning - 根据两个参数预测值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39422736/

相关文章:

java - 在Android上使用朴素贝叶斯分类

machine-learning - 结合回归和分类的多输出神经网络

machine-learning - 使用朴素贝叶斯获取类别概率

machine-learning - sklearn Logistic回归概率

machine-learning - Vowpalwabbit 奇怪的特征计数

python - 对短语使用 word2vec

machine-learning - 机器学习中频率论观点的总结

python - 将 sklearn LogisticRegression 系数链接到稀疏矩阵中的项,并获得统计显着性/C.I

python - 使用自定义管道进行交叉验证 scikit-learn

machine-learning - libsvm 中的 Holdout 与 K 折交叉验证