2016-05-23 154 views
0

我目前正在爲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,抱歉,如果我只是犯了一個愚蠢的錯誤。

+0

什麼是42491和打印價格的結果是什麼 –

+0

您可以給出xlsx中的數據樣本嗎? – DJanssens

+0

如果只打印價格,那麼:[u'B'] – Mick

回答

0

如果我沒有錯誤'u'只是表示字符串是一個Unicode字符串。我不知道你是如何運行你的代碼,但是當我在IPython的筆記本電腦或Windows測試命令提示符我得到以下輸出:

The price for Ron95 in next month will be RM [ 'B'] 

這也許是因爲我跑這在Python 3.5,而它看來你還在使用python < 3.0。

這並不是說你的答案是錯誤的,你只是獲得有關數據格式的信息。有關此主題的其他問題,請參閱herehere。 python how-to on unicode也可能有幫助。

0

我認爲這會更容易,當你從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') 
+0

ValueError:labels ['Ron95_RM']不包含在軸 – Mick

+0

糟糕,抱歉,是我的錯。對,列車數據中沒有'Ron95_RM'。我必須去睡覺:) 你可以發佈一些來自Ron95_Price_Class.xlsx的行,或者打印(X,y)? – LinearLeopard

0

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]」。

+0

謝謝!價格[0]實際上起作用 – Mick

+0

很酷,如果您可以做+投票或選擇我的答案作爲正確答案,我將非常感激。謝謝。 – pmaniyan