我目前正在学习Python中机器学习的逻辑回归。这是我的代码。
import pandas as pdfrom sklearn import linear_modelimport numpy as npfrom sklearn.utils import column_or_1dlogistic = 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之前的版本。
并不是你的答案错了,你只是得到了关于数据格式的信息。关于这个主题的其他问题,请参见这里和这里。Python的关于Unicode的指南也可能有所帮助。