Machine learning 根据两个参数预测值

Machine learning 根据两个参数预测值,machine-learning,classification,logistic-regression,Machine Learning,Classification,Logistic Regression,我正在努力学习数值分析。我遵守本章程- 我的数据如下所示: 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

我正在努力学习数值分析。我遵守本章程-

我的数据如下所示:

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])
我收到thsi错误:

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"
回溯(最近一次呼叫最后一次):
文件“socratis.py”,第16行,在
模型拟合(序列[x1],序列[“VAL”])
文件“/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py”,第1986行,在__
返回self.\u getitem\u数组(键)
文件“/usr/local/lib/python2.7/dist packages/pandas/core/frame.py”,第2030行,在_getitem_数组中
索引器=self.ix.\u转换为索引器(键,轴=1)
文件“/usr/local/lib/python2.7/dist packages/pandas/core/index.py”,第1210行,在“convert\u to\u indexer”中
raise KeyError(“%s”不在索引“%objarr[mask]”中)
KeyError:“['date''hr\U of_day''VAL']不在索引中”
我建议在阅读TSV时提供:

Train = pd.read_csv("data_scientist_assignment.tsv", sep='\t') # use TAB as column separator
解决此问题时,队列中还有另一个问题:
ValueError:无法将字符串转换为浮点:“2014-09-13”
这是因为线性回归需要数字特征,而列
date
是字符串类型

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

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

从ML的角度来看,您不应该将目标值
VAL
用作输入特性。你也应该考虑将日期表示为单独的特征:天、蒙特、年;或者一周中的哪一天,这取决于你想建模什么。

@Merlin,这个问题包含代码,是关于编程错误,而不是关于统计本身。在我看来,这完全是本文的主题。请包括完整的错误堆栈跟踪,而不仅仅是错误名称。@cel:更新了它,直到在x1中输入VAL后得到相同的错误。测试文件格式是否正确?@user3449212尝试为
read\u csv
添加
sep='\t'
参数。在进一步操作之前,请确保已正确加载TSV文件<代码>打印(列)和
打印(列)
等。包括X和Y中的VAL意味着您希望使用VAL预测VAL,这毫无意义。但是从编程的角度来看,这没关系,它不应该产生任何错误。谢谢,我更新了代码。打印看起来像
索引([u'2014-05-01',u'0',u'72'],dtype='object')索引([u'date',u'hr'],dtype='object')
,但索引仍然错误persist@user3449212我重写了解决实际问题的答案:加载TSV时,必须告诉解析器使用TAB作为分隔符