【发布时间】:2018-11-15 17:12:53
【问题描述】:
我正在研究 Kaggle 的泰坦尼克号机器问题 - 初学者。
我正在用python写我的代码,模型类型是K-NN。
我收到错误“输入包含 NaN、无穷大或对于 dtype('float64') 而言太大的值”,但是,我已经彻底检查了我的数据。没有无限值,没有 NaN 值,也没有大值。该错误不是在我的训练集上引发的,而是在测试集上引发的——它们的值没有区别(内容明显不同,但值的类型相同)。 这是我的代码:
import numpy as np
import pandas as pd
test_dataset = pd.read_csv('test.csv')
X_classt = test_dataset.iloc[:, 1].values.reshape((1,-1))
X_faret = test_dataset.iloc[:,8].values.reshape((1,-1))
X_Stpt = test_dataset.iloc[:,3:7]
X_embarkedt = test_dataset.iloc[:,10].values.reshape((-1,1))
X_onet = np.concatenate((X_classt,X_faret))
X_onet = np.matrix.transpose(X_onet)
X_twot = np.concatenate((X_Stpt,X_embarkedt),axis=1)
Xt = np.concatenate((X_onet,X_twot),axis=1)
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN',strategy ='mean', axis = 0)
imputer = imputer.fit(Xt[:,3:5])
Xt[:,3:5] = imputer.transform(Xt[:,3:5])
Xt_one = np.array(Xt[:,0:2],dtype = np.float)
ColThreet = Xt[:,2]
Xt_two = np.array(Xt[:,3:6],dtype=np.float)
ColSevent = Xt[:,6]
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
lett = LabelEncoder()
Xt[:,2] = lett.fit_transform(ColThreet)
lest = LabelEncoder()
Xt[:,6] = lest.fit_transform(Xt[:,6])
#This is where the error is thrown
ohct = OneHotEncoder(categorical_features=[6])
Xt = ohct.fit_transform(Xt).toarray()
感谢您提供的任何帮助。我意识到我的命名约定很奇怪,但这是因为我使用了与训练代码基本相同的变量,所以我在每个变量的末尾添加了一个“t”来“重用”测试集代码的名称.
提前致谢。
【问题讨论】:
-
请发布您导入的模块以获得可重现的代码。还有一个小数据样本。
标签: python pandas numpy scikit-learn knn