2017-07-26 50 views
0

我是scikit的新手,需要了解如何基於多個連續數據列預測值。在這裏我有幾個數據列,它們具有如下的連續數據。 (列名僅供參考)使用scikit預測連續數據的值

ColA, ColB, ColC, ColD, ColE 
8.0  307.0 130.0 3504.0 12.0 
15.0 350.0 165.0 3693.0 11.5 
18.0 318.0 150.0 3436.0 11.0 
16.0 304.0 150.0 3433.0 12.0 
17.0 302.0 140.0 3449.0 10.5 
15.0 429.0 198.0 4341.0 10.0 
14.0 454.0 220.0 4354.0 9.0 
14.0 440.0 215.0 4312.0 8.5 
.... 
.... 

我需要做的是預測ColA中基於通過饋入上述數據創建的模型的值。我只看到了對預測值進行分類的例子。如果ColB,ColC,ColD,ColE值中的任何一個/全部都給出,如何獲得實際值?

任何人都可以幫我在這個如何用scikit做到這一點?

+0

,你可以使用大量的像線性迴歸模型[見這裏](http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html)後使用數據擬合模型,您將使用predict()方法來獲得預測結果。你想讓我提供一個例子嗎?如果是,你可以上傳數據嗎? – sera

+0

謝謝Sera。如果您可以在此處放置一些代碼片段,對我來說會很有幫助。我知道如何將數據加載到表格結構中。我對傳遞給fit方法的參數以及對它進行編碼的正確方法感到困惑。假設我已將所有數據加載到名爲sample_data的結構中。應該通過ColB,ColC,ColD,ColE的子模型作爲X和ColA作爲y?如何通過價值預測? – IndikaU

+0

是的,這是正確的做法。我會在幾分鐘後發佈一些代碼。 X和y應該是數組。另外熊貓模塊對於加載/分割數據非常有用。我將使用您發佈的數據創建一個簡單示例。 – sera

回答

0

首先,我將數據轉換爲csv文件,以便我可以使用熊貓。 該CSV是here

實施例:

import pandas as pd 
from sklearn.linear_model import LinearRegression 
from sklearn.model_selection import train_test_split 

df = pd.read_csv('data.csv',header = None) 

#Fill the missing data with 0 and the '?' that you have with 0 
df = df.fillna(0) 
df= df.replace('?', 0) 

X = df.iloc[:,1:7] 

#I assume than the y is the first column of the dataset as you said 
y = df.iloc[:,0] 

#I split the data X, y into training and testing data 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42) 

#Convert pandas dataframes into numpy arrays (it is needed for the fitting) 
X_train = X_train.values 
X_test = X_test.values 
y_train = y_train.values 
y_test = y_test.values 

#Create and fit the model 
model = LinearRegression() 

#Fit the model using the training data 
model.fit(X_train,y_train) 

#Predict unseen data 
y_predicted =model.predict(X_test) 
scores = model.score(X_test, y_test) 

print(y_predicted) 
print(scores) 

第一打印的結果是預測值爲看不見(X_test)特徵。預測值對應於數據集的第1列。

第二次印刷的結果返回預測的確定係數R^2。

更多here

P.S:要解決的問題是過於籠統。

首先,您可以使用sklearn中的StandardScaler方法來縮放功能(X數組)。這通常很好,它可以提高性能,但它對你有用。更多詳細信息here

接下來,您可以使用其他方法拆分數據,而不是使用train_test_split

最後,您可以使用其他方法代替LinearRegression。

希望這有助於

+0

非常感謝。 – IndikaU

+0

@IndikaU很高興我能幫到你。你介意提高我的答案嗎? – sera

+0

我沒有點擊upvote。希望系統稍後會更新。 – IndikaU