python - 泰坦尼克号管道中的 ValueError

标签 python pipeline valueerror

我正在开发我的第一个管道,但无法让它在泰坦尼克号数据集上运行。有人可以解释一下我做错了什么以及如何解决它吗?

我从数据框中删除了一些特征,并使用 get dummies 来转换分类特征。

titanic_dummies = titanic.copy()
titanic_dummies = titanic_dummies.drop([ 'Name', 'Ticket','Cabin', "Fare"], axis=1)
titanic_dummies = pd.get_dummies(titanic_dummies, drop_first=True)

然后我尝试运行这个管道

X=titanic_dummies.drop(['Survived'], axis=1)
y=titanic_dummies['Survived']


 ****#setup the pipeline steps****
steps = [('scaler', StandardScaler()),
         ('imputation', SimpleImputer(missing_values='NaN', strategy='most_frequent')),
         ('logreg', LogisticRegression())]
          
*# Create the pipeline: pipeline*
pipeline = Pipeline(steps)

#Define hyperparameters and range of Grid Search
parameters = {"logreg__C": np.logspace(-5, 8, 15),
              "logreg__penalty": ['l1', 'l2']}

*# Create train and test sets*
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

*# run cross validation*
cv = GridSearchCV(pipeline, param_grid = parameters, cv=3)

*# Fit the pipeline to the training set:* 
cv.fit(X_train, y_train)

*# Predict the labels of the test set*
y_pred = cv.predict(X_test)

*# Compute and print metrics*
print("Accuracy: {}".format(cv.score(X_test, y_test)))
print(classification_report(y_test, y_pred))
print("Tuned Model Parameters: {}".format(cv.best_params_))

这是我收到的错误

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

从附图中您可能可以看出我的值的大小不是问题。也许我的估算出了问题?

Titanic Data

我真的很想听听您对如何解决这个问题的想法。

最佳答案

你可以改变

missing_values = “NaN”

missing_values = np.nan

这可能有用。

关于python - 泰坦尼克号管道中的 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62745831/

相关文章:

python - 如何使用树莓派检测Python中的按键

python - 用 Python re 替换为自定义函数

shell - 仅使用重定向和管道在 Unix shell 中是否可以实现无限循环?

c# - SSIS - PipelineComponent 中的 ProcessInput 被多次调用

python - ValueError: index必须是单调递增或单调递减的,同时包含index,column,ffill

java - 在Python中运行包含java命令的批处理文件

java - Python Server/Java客户端“连接被拒绝:连接”

python - 在Python脚本中使用光学字符识别

linux - 是否可以组合更多 tr 命令?

arrays - 在numpy中创建具有不同维度的数组数组