python - 拆分为具有特定行属性值的训练集和测试集
问题描述
我的输入文件采用以下形式:
gold,Attribute1,Attribute2
T,1,1
T,1,2
T,1,1
N,1,2
N,2,1
T,2,1
T,2,2
N,2,2
T,3,1
N,3,2
N,3,1
T,3,2
N,3,3
N,3,3
我正在尝试使用第二列和第三列来预测第一列。我想将此输入数据随机拆分为训练集和测试集,以便具有 <attribute1, attribute2> 值的特定组合的所有行都属于测试集或训练集。例如,所有值为 <1,1>、<1,2>、<2,1> 的行都应落入训练集中,所有值为 <2,2>、<3,1>、< 3,2>, <3,3> 应该在测试集中。这必须随机制作,这只是一个例子。我怎么能做出这样的分裂?
解决方案
一种简单的拆分方法是通过条件而不是预定义的方法。
代码 :-
import numpy as np
import pandas as pd
df = pd.DataFrame(pd.read_csv('test.csv'))
print(df.head())
print(df.describe())
print(type(df['Attribute1']))
#For only getting values where both are less than 2 or equal to 2
df_Condition1 = df[df['Attribute1'] <= 2]
Train_Set = df_Condition1[df_Condition1['Attribute2'] <= 2]
#to subract the remaining elements
Test_Set = df[ df.isin(Train_Set) == False]
Test_Set =Test_Set.dropna()
print(Train_Set)
print(Test_Set)
输出 :
gold Attribute1 Attribute2
0 T 1 1
1 T 1 2
2 T 1 1
3 N 1 2
4 N 2 1
Attribute1 Attribute2
count 14.000000 14.000000
mean 2.142857 1.714286
std 0.864438 0.726273
min 1.000000 1.000000
25% 1.250000 1.000000
50% 2.000000 2.000000
75% 3.000000 2.000000
max 3.000000 3.000000
<class 'pandas.core.series.Series'>
gold Attribute1 Attribute2
0 T 1 1
1 T 1 2
2 T 1 1
3 N 1 2
4 N 2 1
5 T 2 1
6 T 2 2
7 N 2 2
gold Attribute1 Attribute2
8 T 3.0 1.0
9 N 3.0 2.0
10 N 3.0 1.0
11 T 3.0 2.0
12 N 3.0 3.0
13 N 3.0 3.0
推荐阅读
- javascript - 表格数据提交
- ruby - 如何使用 google api ruby gem 查询 google analytics api?
- c++ - 在库中使用 CMake 的 Abseil
- amazon-s3 - 从 S3 提取巨大的 tar.gz 档案而不将档案复制到本地系统
- graphql-js - 如何为 MongoDB 的 collection.find() 函数中的 MongoDB 排序属性实现 GraphQL Scalar?
- java - 在java中通过SOAP发送附件
- python - 在 python 中访问/迭代复杂字典对象的问题
- node.js - 加载资源失败:服务器响应状态为 431 (Request Header Fields Too Large)
- sequence - 关于 Raku 中的数组生成顺序的问题
- python - 我尝试从 Google 获取足球比赛日程,但出现此错误