[自用] label添加到feature模板

from sklearn.model_selection import StratifiedShuffleSplit
import numpy as np
X = np.array([[111] , [112] , [113] ,[114],  [222] , [221] , [223] ,[224], [331] , [332] , [333] ,[334] , [441] , [442] , [443], [444]])
y = np.array([1, 1, 1,1 , 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]) #label
y = y.reshape(-1,1)#要给它转换成列向量
data_withlabel = np.hstack((X, y))#这是把y加到X的右边
train_indices = []
test_indices = []

split = StratifiedShuffleSplit(n_splits=1, test_size=0.25, random_state=0)


for train_index, test_index in split.split(data_withlabel, data_withlabel[:,-1]):
    train_indices.extend(train_index)
    test_indices.extend(test_index)
print(test_index.size)
# 根据索引创建训练和测试数据集
X_train = X[train_indices]
X_test = X[test_indices]
y_train = y[train_indices]
y_test = y[test_indices]
train_dataset = data_withlabel[train_indices]
test_dataset = data_withlabel[test_indices]

print(train_dataset[:, -1])
print(test_dataset)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值