我正在使用LogisticRegression对kaggle.com的泰坦尼克号问题进行建模。我想使用多个变量,如年龄,性别等来模拟我的S型函数。如果只与Sex这样的一个变量一起使用,同样的方法也可以很好地工作,但当与多个变量一起使用时,它会抛出以下错误
TypeError:foat()参数必须是字符串或数字,而不是"method"
我的猜测是我没有正确使用整形方法。附言:我是python和sklearn库的初学者。请对我宽容一点。
import pandas as pd
from sklearn.linear_model import LogisticRegression
import numpy as np
df = pd.read_csv(r'C:UsersabhiDownloadstrain.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)
您能在删除此行的情况下运行代码吗?i2 = i2.reshape(-1,1)
将i2
重塑为(-1,1)
将i2
变换为具有i2
中的元素总数的长度的一维阵列。这可能不是你想要做的。