【问题标题】:Pandas Dataframe used for Train_Test_Split用于 Train_Test_Split 的 Pandas 数据框
【发布时间】:2018-02-26 09:49:37
【问题描述】:

我有以下数据集,我想使用 Pandas 和 sklearn 使用 K-Nearest-Neighbor 进行分析:

https://archive.ics.uci.edu/ml/datasets/Credit+Approval

首先,我将数据集加载到数据框中,每列都有标题(A1 到 A16):

df =  pd.read_csv('crx.csv').dropna().reset_index(drop = True)

我的问题是,如何使用train_test_split 函数来:

a) 拆分测试中的数据并训练和

b) 还将列 A1 到 A15 标记为特征,将 A16 标记为标签?

我想要类似下面的代码,这当然不能像我想要的那样工作:

X_train, X_test, y_train, y_test = train_test_split(df[0:16], df['A16'], random_state=0)

X_train 将有 75% 的数据从列 A1 到 A15,X_test 其余 25%,y_train 相同的 75% 的数据,但只有列 A16(目标)和y_test 剩下的 25%。

我打算稍后将KNeighborsClassifier.fit 与训练数据一起使用。

【问题讨论】:

    标签: python-3.x pandas scikit-learn


    【解决方案1】:

    改用df.iloc[:,0:16](或df[['A1', 'A2', 'A3', ...]],但在你的情况下更麻烦):

    train_test_tuple = train_test_split(df.iloc[:,0:16], df['A16'], random_state=0)
    

    请参阅documentation for iloc。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      • 2014-10-10
      • 1970-01-01
      • 1970-01-01
      • 2014-12-15
      • 2021-11-28
      相关资源
      最近更新 更多