python中的逻辑回归
我目前正在为python进行机器学习中的Logistic回归。这是我写的代码。python中的逻辑回归
import pandas as pd
from sklearn import linear_model
import numpy as np
from sklearn.utils import column_or_1d
logistic = linear_model.LogisticRegression()
data = pd.read_excel('/home/mick/PycharmProjects/project1/excel/Ron95_Price_Class.xlsx')
X = data[['Date']]
y = data[['Ron95_RM']]
y = np.ravel(y)
logistic.fit(X, y)
price = logistic.predict(42491)
print "The price for Ron95 in next month will be RM", np.array_str(price,1)
这是代码
The price for Ron95 in next month will be RM [ u'B']
没有错误的输出,但我的问题是字符输出RM后应该是“B”或其它字符。我不知道是因为我错误地执行了代码还是只是numpy数组的格式问题。
因为我今天基本上刚刚开始使用Python,抱歉,如果我只是犯了一个愚蠢的错误。
如果我没有错误'u'只是表示字符串是一个Unicode字符串。我不知道你是如何运行你的代码,但是当我在IPython的笔记本电脑或Windows测试命令提示符我得到以下输出:
The price for Ron95 in next month will be RM [ 'B']
这也许是因为我跑这在Python 3.5,而它看来你还在使用python < 3.0。
这并不是说你的答案是错误的,你只是获得有关数据格式的信息。有关此主题的其他问题,请参阅here和here。 python how-to on unicode也可能有帮助。
我认为这会更容易,当你从Ron95_Price_Class.xlsx发布一些数据时
现在我看到,你不是从列车数据中删除目标变量(y)。你可以做到这一点
X = data['Date'] #you can use only one bracket if choose only
y = data['Ron95_RM'] #column
X = data.drop('Ron95_RM')
ValueError:labels ['Ron95_RM']不包含在轴 – Mick
糟糕,抱歉,是我的错。对,列车数据中没有'Ron95_RM'。我必须去睡觉:) 你可以发布一些来自Ron95_Price_Class.xlsx的行,或者打印(X,y)? – LinearLeopard
scikit-learn文档中提到的预测方法http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression.predict提到预测方法的返回是array,shape = [n_samples]。所以你的形状是1x1阵列。为了得到想要的输出,你可以试试“price [0]”。
什么是42491和打印价格的结果是什么 –
您可以给出xlsx中的数据样本吗? – DJanssens
如果只打印价格,那么:[u'B'] – Mick