关于np.zeros的一段测试代码

测试代码:

#test code
import numpy as np

a = np.zeros(2, dtype=np.int32)
print(a)

b = np.zeros((4, 4), dtype=np.int32)
print(b)

b[a[0], a[1]] = 1
print(b)

输出:

[0 0]
[[0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]
[[1 0 0 0]
 [0 0 0 0]
 [0 0 0 0]
 [0 0 0 0]]

def make_datasets(org_samples): '''输入10*120*2048的原始样本,输出带标签的训练集(占75%)和测试集(占25%)''' train_x=np.zeros(shape=(10,90,2048)) train_y=np.zeros(shape=(10,90,10)) test_x=np.zeros(shape=(10,30,2048)) test_y=np.zeros(shape=(10,30,10)) for i in range(10): s=org_samples[i] # 打乱顺序 index_s = [a for a in range(len(s))] shuffle(index_s) s=s[index_s] # 对每种类型都划分训练集和测试集 train_x[i]=s[:90] test_x[i]=s[90:120] # 填写标签 label = np.zeros(shape=(10,)) label[i] = 1 train_y[i, :] = label test_y[i, :] = label #将十种类型的训练集和测试集分别合并并打乱 x1 = train_x[0] y1 = train_y[0] x2 = test_x[0] y2 = test_y[0] for i in range(9): x1 = np.row_stack((x1, train_x[i + 1])) x2 = np.row_stack((x2, test_x[i + 1])) y1 = np.row_stack((y1, train_y[i + 1])) y2 = np.row_stack((y2, test_y[i + 1])) index_x1= [i for i in range(len(x1))] index_x2= [i for i in range(len(x2))] shuffle(index_x1) shuffle(index_x2) x1=x1[index_x1] y1=y1[index_x1] x2=x2[index_x2] y2=y2[index_x2] return x1, y1, x2, y2 #分别代表:训练集样本,训练集标签,测试集样本,测试集标签 def get_timesteps(samples): ''' get timesteps of train_x and test_X to 10*120*31*128 :param samples : a matrix need cut to 31*128 ''' s1 = np.zeros(shape=(31, 128)) s2 = np.zeros(shape=(len(samples), 31, 128)) for i in range(len(samples)): sample = samples[i] for a in range(31): s1[a]= sample[64*a:128+64*a] s2[i]=s1 return s2解释下每段代码含义
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值