我无法重现相同的结果,事实上,我复制粘贴了您的代码,并没有像您描述的问题那样将它们全部归零,而是得到:
[0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0]
不过,在您的方法中,我注意到了一些您可能想知道的事情:
-
Pandas read_csv 中的默认分隔符是 ,,因此如果您的数据集变量由 tab 分隔(与我的相同),那么您应该像这样指定分隔符:
df = pd.read_csv('titanic.csv', sep='\t')
PassengerId 没有任何有用的信息可供您的模型学习以预测 Survived 人,它只是一个随着每个新乘客而增加的连续数字。一般来说,在分类中,您需要利用使您的模型从中学习的所有特征(当然,除非有冗余特征不会向模型添加任何信息),尤其是在您的数据集中,它是一个多变量的数据集。
没有必要对PassengerId 进行缩放,因为features scaling 通常在特征的大小、单位和范围(例如 5 公斤和 5000 克)和您的如前所述,它只是一个增量整数,对模型没有真实信息。
-
最后一件事,您应该将您的数据作为float 类型获取StandardScaler,以避免出现如下警告:
DataConversionWarning: Data with input dtype int64 was converted to float64 by StandardScaler.
所以你从一开始就这样转换:
x = df['PassengerId'].values.astype(float).reshape(-1,1)
最后,如果你仍然得到相同的结果,那么请添加一个指向你的数据集的链接。
更新
提供数据集后,结果你得到的结果是正确的,这又是因为我上面提到的原因号2(即PassengerId没有为模型提供有用的信息,所以它无法预测正确!)
您可以通过比较 log loss 在从数据集中添加更多功能之前和之后自行测试:
from sklearn.metrics import log_loss
df = pd.read_csv('train.csv', sep=',')
x = df['PassengerId'].values.reshape(-1,1)
y = df['Survived']
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.25,
random_state = 0)
classifier = LogisticRegression()
classifier.fit(x_train,y_train)
y_pred_train = classifier.predict(x_train)
# calculate and print the loss function using only the PassengerId
print(log_loss(y_train, y_pred_train))
#predicting the test set results
y_pred = classifier.predict(x_test)
print(y_pred)
输出
13.33982681120802
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0]
现在通过使用许多“可能有用的”信息:
from sklearn.metrics import log_loss
df = pd.read_csv('train.csv', sep=',')
# denote the words female and male as 0 and 1
df['Sex'].replace(['female','male'], [0,1], inplace=True)
# try three features that you think they are informative to the model
# so it can learn from them
x = df[['Fare', 'Pclass', 'Sex']].values.reshape(-1,3)
y = df['Survived']
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size = 0.25,
random_state = 0)
classifier = LogisticRegression()
classifier.fit(x_train,y_train)
y_pred_train = classifier.predict(x_train)
# calculate and print the loss function with the above 3 features
print(log_loss(y_train, y_pred_train))
#predicting the test set results
y_pred = classifier.predict(x_test)
print(y_pred)
输出
7.238735137632405
[0 0 0 1 1 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 1 0 0 1 1 0 1 1 1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0
0 1 1 0 0 0 0 0 1 0 0 1 1 1 1 0 0 0 0 1 1 0 1 0 0 0 0 0 0 0 1 1 1 1 0 1 0
1 0 1 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 1 1 1 0 1
1 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0
0 1 0 0 1 1 0 1 1 0 0 0 1 0 0 0 1 0 1 0 0 1 0 1 0 0 0 0 1 0 0 0 0 1 1 0 1
1]
结论:
如您所见,损失提供了更好的价值(比以前更小)并且预测现在更合理!