2016-05-12 79 views
0

我使用LogisticRegression模擬kaggle.com的泰坦尼克號問題。 我想使用多個變量,如年齡,性別等來建模我的sigmoid函數。 如果只有1像性別變量使用相同的方法工作正常,但它拋出多變量用於如果出現以下錯誤將特徵向量傳遞給sklearn的Logistic迴歸函數

TypeError: float() argument must be a string or a number, not 'method'

我的猜測是,我沒有正確使用方法重塑。 PS:我是Python和sklearn庫的初學者。請在我身上輕鬆一點。

import pandas as pd 
from sklearn.linear_model import LogisticRegression 
import numpy as np 


df = pd.read_csv(r'C:\Users\abhi\Downloads\train.csv') 

df.Age = df.Age.fillna(df.Age.mean) 
df.Embarked = df.Embarked.fillna(df.Embarked.median) 
x1 = df.Pclass 
x2 = df.Sex 
for i in range(len(x2)): 
    if x2[i]=='male': 
     x2[i]=1 
    else: 
     x2[i]=0 
#female,male 0,1 

x3 = df.Age 
x4 = df.SibSp 
x5 = df.Parch 
x6 = df.Ticket 
x7 = df.Fare 
x9 = df.Embarked 
for i in range(len(x9)): 
    if x9[i]=='C': 
     x9[i]=0 
    elif x9[i]=='Q': 
     x9[i]=1 
    else :x9[i]=2 

# C,Q,S = 0,1,2 
# Creating a feature vector of multiple vectors 

i2 = pd.DataFrame() 
i2['Pclass'] = x1 
i2['Sex'] = x2 
i2['Age'] = x3 
i2['SibSp'] = x4 
i2['Parch'] = x5 
i2['Fare'] = x7 
i2['Embarked'] = x9 
i2 = np.array(i2) 
i2 = i2.reshape(-1,1) 

ytrain = df.Survived 
ytrain = np.array(ytrain) 
ytrain = ytrain.reshape(-1,1) 
c1 = LogisticRegression(penalty='l2',solver='liblinear') 
c1.fit(i2,ytrain,sample_weight=None) 
c1.score(i2,ytrain,sample_weight=None) 
+0

你可以看看http://hamelg.blogspot.in/2015/11/python-for-data-analysis-part-28.html –

回答

0

你可以運行你的代碼刪除這一行嗎? i2 = i2.reshape(-1,1)

整形i2(-1,1)將改變與所述i2元素的總數的長度i2到一維陣列。這可能不是你想要做的。

+0

刪除該行時,我收到此錯誤: TypeError: float()參數必須是字符串或數字,而不是'方法' –