代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
import numpy as np
RANDOM_SEED = 20200309
def dense_to_one_hot(labels_dense, num_classes):
"""Convert class labels from scalars to one-hot vectors."""
num_labels = labels_dense.shape[0]
# print("num_labels:",num_labels)
index_offset = np.arange(num_labels) * num_classes
# print("index_offset",index_offset)
labels_one_hot = np.zeros((num_labels, num_classes), dtype=int)
# print("labels_one_hot",labels_one_hot)
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
# print("输出:",labels_one_hot)
return labels_one_hot
def get_data(test_fold, feat):
"""load feature for train and test"""
# load feature
data = np.load('./data/esc10/feature/esc10_{}_fold{}.npz'.format(feat, test_fold))
# data = np.load('./data/esc10/feature/esc10_logmel_fold{}.npz'.format(feat, test_fold))
train_x = np.expand_dims(data['train_x'], axis=-1)
train_y = data['train_y']
test_x = np.expand_dims(data['test_x'], axis=-1)
test_y = data['test_y']
# one-hot encode
train_y = dense_to_one_hot(train_y, 10)
test_y = dense_to_one_hot(test_y, 10)
# z-score normalization
mean = np.mean(train_x)
std = np.std(train_x)
train_x = (train_x - mean) / std
test_x = (test_x - mean) / std
# shuffle
np.random.seed(RANDOM_SEED)
np.random.shuffle(train_x)
np.random.seed(RANDOM_SEED)
np.random.shuffle(train_y)
print('Audio Feature: ', feat)
print('Training Set Shape: ', train_x.shape)
print('Test Set Shape: ', test_x.shape)
return train_x, train_y, test_x, test_y
if __name__ == '__main__':
data = np.load('./data/esc10/feature/esc10_logmel_fold1.npz')
train_y = data['train_y']
print("train_y",train_y.shape)
train_y = dense_to_one_hot(train_y, 10)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。