python 数据拆分成训练集和测试集

import numpy as np
from sklearn.model_selection import train_test_split
my_matrix = np.loadtxt(open("xxxxx.csv"),delimiter=",",skiprows=0)
X, y = my_matrix[:,:-1],my_matrix[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
train= np.column_stack((X_train,y_train))
np.savetxt('train_usual.csv',train, delimiter = ',')
test = np.column_stack((X_test, y_test))
np.savetxt('test_usual.csv', test, delimiter = ',')

 

运行程序,会发现报错:这个代码有缺陷,会把csv中的float类型读成str类型。

新建文件夹,运行如下代码:

import numpy as np

fr = open("sh.csv", 'r')  #原始数据集
frw = open("sh1.csv", 'w')

line = fr.readlines()
for L in line:
    string = L.strip("\n").split(",")
    a = np.float(string[0])
    b = np.float(string[1])
    c = np.float(string[2])
    d = np.float(string[3])
    e = np.float(string[4])
    f = np.float(string[5])
    g = np.float(string[6])

    str = '%f,%f,%f,%f,%f,%f,%f\n' % (a, b, c, d, e, f, g)  #根据你自己的数据集修改
    print(str)
    frw.write(str)
frw.close()
fr.close()


再将开始的代码运行一遍:

import numpy as np
from sklearn.model_selection import train_test_split
my_matrix = np.loadtxt(open("sh1.csv"),delimiter=",",skiprows=0)
X, y = my_matrix[:,:-1],my_matrix[:,-1]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
train= np.column_stack((X_train,y_train))
np.savetxt('train_usual.csv',train, delimiter = ',')
test = np.column_stack((X_test, y_test))
np.savetxt('test_usual.csv', test, delimiter = ',')

完成!!!


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值