import numpy as np import pandas as pd from sklearn.model_selection import train_test_split df = pd.read_csv("data/train.csv", encoding='utf8') df_X = df[['PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']] df_Y = df['Survived'] # train_X, test_X, train_Y, test_Y train_test_split(df_X.values, df_Y.values, test_size=0.2, random_state=1314) print("train_X.count:{}, test_X.count:{}, train_Y.count:{}, test_Y.count:{}").format(len(train_X), len(test_X), len(train_Y), len(test_X))怎么改
时间: 2023-08-08 17:04:52 浏览: 147
随机森林预测数据
你需要把 `train_test_split` 函数的返回值赋值给四个变量,分别是训练集的特征、测试集的特征、训练集的标签、测试集的标签。同时,你需要使用 `print` 函数的格式化字符串功能,将括号中的内容改为 f-string 的形式,即在字符串前面加上 `f`,然后用花括号 `{}` 包裹变量名。具体代码如下:
```
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.read_csv("data/train.csv", encoding='utf8')
df_X = df[['PassengerId', 'Pclass', 'Name', 'Sex', 'Age', 'SibSp', 'Parch', 'Ticket', 'Fare', 'Cabin', 'Embarked']]
df_Y = df['Survived']
# train_X, test_X, train_Y, test_Y
train_X, test_X, train_Y, test_Y = train_test_split(df_X.values, df_Y.values, test_size=0.2, random_state=1314)
print(f"train_X.count:{len(train_X)}, test_X.count:{len(test_X)}, train_Y.count:{len(train_Y)}, test_Y.count:{len(test_X)}")
```
这样就可以正确输出训练集和测试集的大小了。
阅读全文