切分数据集和测试集之前没有注意的事情

分享一下今天遇到的事情


#先将数据集分成训练集和测试集
train_data, test_data = train_test_split(data, test_size=0.2, random_state=7)
'''
剔除掉训练集和测试集中的索引
'''
train_data = train_data.reset_index(drop=True)
test_data = test_data.reset_index(drop=True)


X_train = train_data[['t','ph','pp','re','wc','bd','cc','bc','sc','rp']]
'''
先对训练集进行fit,然后再对训练集进行transform。后面再利用训练集的fit,对测试集进行transform,这样归一化的测试集不会产生数据泄露。
'''
scaler = StandardScaler().fit(X_train)
x_tran=pd.DataFrame(scaler.transform(X_train),columns=['t','ph','pp','re','wc','bd','cc','bc','sc','rp'])
                        

#one-hotting
x_train_ct = pd.get_dummies(train_data['CT'])
x_train_class = pd.get_dummies(train_data['class'])


x_train = pd.concat([x_tran,x_train_ct,x_train_class],axis=1)

y_train = train_data['dmax']
'''
test
'''
X_test = test_data[['t','ph','pp','re','wc','bd','cc','bc','sc','rp']]

x_test_tran=pd.DataFrame(scaler.transform(X_test),columns=['t','ph','pp','re','wc','bd','cc','bc','sc','rp'])
'''
不知道什么原因, pd.get_dummies产生的是false和true,不是0和1,用.astype(int)进行转换
'''
#one-hotting
x_test_ct = pd.get_dummies(test_data['CT']).astype(int)
x_test_class = pd.get_dummies(test_data['class']).astype(int)


x_test = pd.concat([x_test_tran,x_test_ct,x_test_class],axis=1)
y_test = test_data['dmax']

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值