numpy和神经网络中数据处理相关的一些总结

1 查看数据类型-type()

list = [1,2,3]
type(list)

结果:list

import numpy as np
arr = np.array([1,2,3])
type(arr)

结果:numpy.ndarray

2 查看数组的一些属性type、dtype、size、shape、ndim

import numpy as np  
 
arr = np.array([1,2,3,4],dtype=np.complex128)   
print("数据类型:",type(arr))           
print("数组元素数据类型:",arr.dtype) 
print("数组元素总数:",arr.size)        
print("数组形状:",arr.shape)         
print("数组的维度数目",arr.ndim)      

结果:
数据类型: <class ‘numpy.ndarray’>
数组元素数据类型: complex128
数组元素总数: 4
数组形状: (4,)
数组的维度数目 1

3 输出数组全部内容(不省略)

import numpy as np
np.set_printoptions(threshold=np.inf)

加入上一句,就可以查看有些数组省略的内容。

4 数据集和测试集分配比例train_size

# 建立训练数据和测试数据(训练数据越多,模型越准确)(分配是随机的,不是按照顺序的)
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(exam_x,  # 样本特征
                                               exam_y,  # 样本标签
                                               train_size=0.8)  # 训练数据占比

例子:

import numpy as np
from sklearn.model_selection import train_test_split
data = np.array([[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6]])
label = np.array([0,0,1,1,1,0])
x_train,x_test,y_train,y_test=train_test_split(data,  # 样本特征
                                               label,  # 样本标签
                                               train_size=0.8)  # 训练数据占比

print(x_train)
print(y_train)
print("*************************")
print(x_test)
print(y_test)

结果:

[[6 6 6]
 [4 4 4]
 [2 2 2]
 [1 1 1]]
[0 1 0 0]
*************************
[[3 3 3]
 [5 5 5]]
[1 1]

5 查看history属性

#history属性
history_dict = history.history
history_dict.keys()

结果:[]:dict_keys([‘loss’, ‘accuracy’, ‘val_loss’, ‘val_accuracy’])

6 shuffle(洗牌);不改变对应关系,打乱多个列表/数组

state = np.random.get_state()
np.random.shuffle(train)
np.random.set_state(state)
np.random.shuffle(label)

列表例子:

import numpy as np
data = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6]]
label = [0,0,1,1,1,0]

state = np.random.get_state()
np.random.shuffle(data)
np.random.set_state(state)
np.random.shuffle(label)

print(data)
print(label)

结果:

[[6, 6, 6], [2, 2, 2], [4, 4, 4], [3, 3, 3], [5, 5, 5], [1, 1, 1]]
[0, 0, 1, 1, 1, 0]

数组例子:

import numpy as np
data_arr = [[1,1,1],[2,2,2],[3,3,3],[4,4,4],[5,5,5],[6,6,6]]
data_arr = np.array(data_arr)
label_arr = np.array([0,0,1,1,1,0])

state1 = np.random.get_state()
np.random.shuffle(data_arr)
np.random.set_state(state1)
np.random.shuffle(label_arr)

print(data_arr)
print(label_arr)

结果:

[[1 1 1]
 [4 4 4]
 [2 2 2]
 [5 5 5]
 [6 6 6]
 [3 3 3]]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值