2016-11-29 149 views
0

我已經在這裏待了幾個小時,現在感覺真的很困難。Python Logistic迴歸

我想在csv「ScoreBuckets.csv」中使用一堆列來預測另一個名爲「Score_Bucket」的csv列。我想在csv中使用多列來預測列Score_Bucket。我遇到的問題是我的結果根本沒有任何意義,我不知道如何使用多列來預測列Score_Bucket。我是數據挖掘的新手,所以我不太熟悉代碼/語法。

這裏是我的代碼至今:

import pandas as pd 
import numpy as np 
from sklearn import metrics 
from sklearn.linear_model import LogisticRegression 
from sklearn.cross_validation import KFold, cross_val_score 

dataset = pd.read_csv('ScoreBuckets.csv') 

CV = (dataset.Score_Bucket.reshape((len(dataset.Score_Bucket), 1))).ravel() 
data = (dataset.ix[:,'CourseLoad_RelativeStudy':'Sleep_Sex'].values).reshape(
      (len(dataset.Score_Bucket), 2)) 


# Create a KNN object 
LogReg = LogisticRegression() 

# Train the model using the training sets 
LogReg.fit(data, CV) 

# the model 
print('Coefficients (m): \n', LogReg.coef_) 
print('Intercept (b): \n', LogReg.intercept_) 

#predict the class for each data point 
predicted = LogReg.predict(data) 
print("Predictions: \n", np.array([predicted]).T) 

# predict the probability/likelihood of the prediction 
print("Probability of prediction: \n",LogReg.predict_proba(data)) 
modelAccuracy = LogReg.score(data,CV) 
print("Accuracy score for the model: \n", LogReg.score(data,CV)) 

print(metrics.confusion_matrix(CV, predicted, labels=["Yes","No"])) 

# Calculating 5 fold cross validation results 
LogReg = LogisticRegression() 
kf = KFold(len(CV), n_folds=5) 
scores = cross_val_score(LogReg, data, CV, cv=kf) 
print("Accuracy of every fold in 5 fold cross validation: ", abs(scores)) 
print("Mean of the 5 fold cross-validation: %0.2f" % abs(scores.mean())) 

print("The accuracy difference between model and KFold is: ", 
     abs(abs(scores.mean())-modelAccuracy)) 

ScoreBuckets.csv:

Score_Bucket,Healthy,Course_Load,Miss_Class,Relative_Study,Faculty,Sleep,Relation_Status,Sex,Relative_Stress,Res_Gym?,Tuition_Awareness,Satisfaction,Healthy_TuitionAwareness,Healthy_TuitionAwareness_MissClass,Healthy_MissClass_Sex,Sleep_Faculty_RelativeStress,TuitionAwareness_ResGym,CourseLoad_RelativeStudy,Sleep_Sex 
5,0.5,1,0,1,0.4,0.33,1,0,0.5,1,0,0,0.75,0.5,0.17,0.41,0.5,1,0.17 
2,1,1,0.33,0.5,0.4,0.33,0,0,1,0,0,0,0.5,0.44,0.44,0.58,0,0.75,0.17 
5,0.5,1,0,0.5,0.4,0.33,1,0,0.5,0,1,0,0.75,0.5,0.17,0.41,0.5,0.75,0.17 
4,0.5,1,0,0,0.4,0.33,0,0,0.5,0,1,0,0.25,0.17,0.17,0.41,0.5,0.5,0.17 
5,0.5,1,0.33,0.5,0.4,0,1,1,1,0,1,0,0.75,0.61,0.61,0.47,0.5,0.75,0.5 
5,0.5,1,0,1,0.4,0.33,1,1,1,1,1,1,0.75,0.5,0.5,0.58,1,1,0.67 
5,0.5,1,0,0,0.4,0.33,0,0,0.5,0,1,0,0.25,0.17,0.17,0.41,0.5,0.5,0.17 
2,0.5,1,0.67,0.5,0.4,0,1,1,0.5,0,0,0,0.75,0.72,0.72,0.3,0,0.75,0.5 
5,0.5,1,0,1,0.4,0.33,0,1,1,0,1,1,0.25,0.17,0.5,0.58,0.5,1,0.67 
5,1,1,0,0.5,0.4,0.33,0,1,0.5,0,1,1,0.5,0.33,0.67,0.41,0.5,0.75,0.67 
0,0.5,1,0,1,0.4,0.33,0,0,0.5,0,0,0,0.25,0.17,0.17,0.41,0,1,0.17 
2,0.5,1,0,0.5,0.4,0.33,1,1,1,0,0,0,0.75,0.5,0.5,0.58,0,0.75,0.67 
5,0.5,1,0,1,0.4,0.33,0,0,1,1,1,0,0.25,0.17,0.17,0.58,1,1,0.17 
0,0.5,1,0.33,0.5,0.4,0.33,1,1,0.5,0,1,0,0.75,0.61,0.61,0.41,0.5,0.75,0.67 
5,0.5,1,0,0.5,0.4,0.33,0,0,0.5,0,1,1,0.25,0.17,0.17,0.41,0.5,0.75,0.17 
4,0,1,0.67,0.5,0.4,0.67,1,0,0.5,1,0,0,0.5,0.56,0.22,0.52,0.5,0.75,0.34 
2,0.5,1,0.33,1,0.4,0.33,0,0,0.5,0,1,0,0.25,0.28,0.28,0.41,0.5,1,0.17 
5,0.5,1,0.33,0.5,0.4,0.33,0,1,1,0,1,0,0.25,0.28,0.61,0.58,0.5,0.75,0.67 
5,0.5,1,0,1,0.4,0.33,0,0,0.5,1,1,0,0.25,0.17,0.17,0.41,1,1,0.17 
5,0.5,1,0.33,0.5,0.4,0.33,1,1,1,0,1,0,0.75,0.61,0.61,0.58,0.5,0.75,0.67 

輸出:

Coefficients (m): 
[[-0.4012899 -0.51699939] 
[-0.72785212 -0.55622303] 
[-0.62116232 0.30564259] 
[ 0.04222459 -0.01672418]] 
Intercept (b): 
[-1.80383738 -1.5156701 -1.29452772 0.67672118] 
Predictions: 
[[5] 
[5] 
[5] 
[5] 
... 
[5] 
[5] 
[5] 
[5]] 
Probability of prediction: 
[[ 0.09302973 0.08929139 0.13621146 0.68146742] 
[ 0.09777325 0.10103782 0.14934111 0.65184782] 
[ 0.09777325 0.10103782 0.14934111 0.65184782] 
[ 0.10232068 0.11359509 0.16267645 0.62140778] 
... 
[ 0.07920945 0.08045552 0.17396476 0.66637027] 
[ 0.07920945 0.08045552 0.17396476 0.66637027] 
[ 0.07920945 0.08045552 0.17396476 0.66637027] 
[ 0.07346886 0.07417316 0.18264008 0.66971789]] 
Accuracy score for the model: 
0.671171171171 
[[0 0] 
[0 0]] 
Accuracy of every fold in 5 fold cross validation: 
    [ 0.64444444 0.73333333 0.68181818 0.63636364 0.65909091] 
Mean of the 5 fold cross-validation: 0.67 
The accuracy difference between model and KFold is: 0.00016107016107 

的原因,我說,不輸出有意義的原因有兩個: 1.無論我爲該列提供什麼數據,預測準確性cy保持不變,並且不應該發生,因爲某些列可以更好地預測Score_Buckets列。 2.它不會讓我使用多列來預測列Score_Buckets,因爲它表示它們必須具有相同的大小,但是如何確保多列顯然具有比列Score_Buckets更大的數組大小。

我在做什麼錯誤的預測?

回答

1

首先,仔細檢查您的問題是否真的可以被定義爲分類問題,或者應該將其定義爲迴歸問題。

假設您確實想將您的數據分類到Score_Bucket列中的四個唯一類中,爲什麼您認爲不能將多個列用作預測變量?實際上,您正在使用示例中的最後兩列。你可以讓你的代碼,如果你考慮到sklearn方法直接與熊貓DataFrames工作(無需轉換爲NumPy的陣列)有點更具可讀性:如果您要選擇多個列

X = dataset[["CourseLoad_RelativeStudy", "Sleep_Sex"]] 
y = dataset[["Score_Bucket"]] 
logreg = LogisticRegression() 
logreg.fit(X, y) 

,您可以使用loc方法:

X = dataset.loc[:, "Healthy":"Sleep_Sex"] 

您還可以通過索引選擇列:

X = dataset.iloc[:, 1:] 

關於你提到的第二個問題,我得到不同的交叉驗證程序的結果取決於我將哪些列用作特徵。請注意,您的樣本數量非常少(20),這使得您的估計預測相當多變。

+0

謝謝先生的幫助!我會嘗試將Score_Bucket列分成多個列並預測每個列。用你的上面的代碼,我得到了錯誤:residual_error = CV - 預測。 ValueError:傳遞的項數錯誤222,放置意味着1 – user2997307

+0

我不知道「將Score_Bucket」列分成多列的含義。目標'y'應該保留在一列中,所以不需要將它分成多列。此外,我懷疑你真的想回歸,因爲你正在計算殘差。因爲這是一個分類器,所以你不能用邏輯迴歸來做到這一點。 – cbrnr