【问题标题】:Seperate Dataframe by label (convert dataframe into numpy array)按标签分隔数据框(将数据框转换为 numpy 数组)
【发布时间】:2020-10-05 22:24:30
【问题描述】:

我有一个数据框,我想根据它们的标签将它们分成不同的数组,我不确定如何通过它的索引过滤它。不确定这是否正确完成:

数据集示例(df)

Cancer_Type  | Variable | Data Split | Target
Cancer1         43        Train        Good
Cancer5         34        Train        Bad
Cancer2         34        Test         Good
Cancer3         23        Test         Bad
Cancer4         25        Test         Good

可能会做这样的事情吗?

#initial split into train/test data
train = df['split'] == 'train'
print("train")
print(train)
test = df['split'] == 'test'
print("valid")
print(test)

X_test = test.values[-1, :-1]
y_test = test.values[-1, -1]

# Get the remaining dataset
X = train.values[:-1, :-1]
y = train.values[:-1, -1]

print("X")
#print(type(X))
#print(X)

print("y")
#print(type(y))
#print(y)

# Split the remaining dataset into train and calibration sets.
X_train, X_cal, y_train, y_cal = train_test_split(X, y)
 
print(X_train.shape, y_train.shape)
print(X_cal.shape, y_cal.shape)

希望按行。

【问题讨论】:

    标签: python arrays pandas numpy scikit-learn


    【解决方案1】:

    据我了解,您希望根据观察值的 Data Split 值将数据拆分为训练集和测试集。之后,您将再次将训练集拆分为训练和校准。标准数据预处理方法包括创建我们的特征 X 和我们的目标 y

    # Get dataframes of train and test features
    X_train = df[df['Data Split'] == 'Train'].drop(columns = ['Target']).to_numpy()
    X_test = df[df['Data Split'] == 'Test'].drop(columns = ['Target']).to_numpy()
    
    # Get arrays of train and test targets
    y_train = df[(df['Data Split'] == 'Train')]["Target"].to_numpy()
    y_test = df[(df['Data Split'] == 'Test')]["Target"].to_numpy()
    
    # Split the train dataset further into train and validation/calibration sets.
    X_train, X_cal, y_train, y_cal = train_test_split(X_train, y_train)
    

    您现在拥有数组形式的训练、验证/校准和测试集。

    如果您希望保留 Target 变量,只需

    train = df[df['Data Split'] == 'Train'].to_numpy()
    test = df[df['Data Split'] == 'Test'].to_numpy()
    

    【讨论】:

    • (不错的用户名顺便说一句)这正是这个想法。 1. 将测试集与整个数据集分离 2. 然后使用剩余的数据集,将其拆分为训练和校准数据。
    • 第一行代码出现语法错误X_test = df[df['split'] == 'valid'].drop(columns = ['output'].to_numpy()
    • @bioinformatics_student 在指定要删除的列时,我错过了一个右括号。请检查更新的代码。 X_test = df[df['split'] == 'valid'].drop(columns = ['output']).to_numpy()
    • 感谢更新,现在没有给出问题 :) 我想知道是否可以在不使用 scikit 助手的情况下手动将数据拆分为训练和测试数据(在第二步中)?
    • @MrDoLoSoLo 抱歉我对此一无所知,我仍然对 ML 的整个主题很熟悉。
    猜你喜欢
    • 2017-11-09
    • 2020-06-20
    • 1970-01-01
    • 2020-03-13
    • 2021-03-26
    • 2018-03-04
    • 2019-07-25
    • 2017-05-09
    相关资源
    最近更新 更多