python 中的逻辑回归
Logistic Regression in python
我目前正在为 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,如果我犯了一个愚蠢的错误,抱歉。
我认为这会更容易,当你 post 来自 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')
如果我没记错的话,'u' 只是表示该字符串是 unicode 字符串。我不确定你的代码如何 运行,但是当我在 ipython 笔记本或 windows 命令提示符中测试时,我得到以下输出:
The price for Ron95 in next month will be RM [ 'B']
这可能是因为我在 python 3.5 中 运行 这个,而您似乎仍在使用 python < 3.0。
并不是说您的答案有误,您只是在获取有关数据格式的信息。有关此主题的其他问题,请参阅 here and here. The python how-to on unicode 也可能有帮助。
scikit-learn 文档http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression.predict中提到的Predict 方法,提到predict 方法的return 是数组,shape = [n_samples]。所以对你来说形状是 1x1 数组。要获得所需的输出,您可以尝试 "price[0]".
我目前正在为 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,如果我犯了一个愚蠢的错误,抱歉。
我认为这会更容易,当你 post 来自 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')
如果我没记错的话,'u' 只是表示该字符串是 unicode 字符串。我不确定你的代码如何 运行,但是当我在 ipython 笔记本或 windows 命令提示符中测试时,我得到以下输出:
The price for Ron95 in next month will be RM [ 'B']
这可能是因为我在 python 3.5 中 运行 这个,而您似乎仍在使用 python < 3.0。
并不是说您的答案有误,您只是在获取有关数据格式的信息。有关此主题的其他问题,请参阅 here and here. The python how-to on unicode 也可能有帮助。
scikit-learn 文档http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression.predict中提到的Predict 方法,提到predict 方法的return 是数组,shape = [n_samples]。所以对你来说形状是 1x1 数组。要获得所需的输出,您可以尝试 "price[0]".