Numpy的学习1-创建数据基础

1 import numpy as np
2 
3 array = np.array([[1,2,3],[4,5,6]])
4 
5 print(array)
6 print('number of dim:',array.ndim)#维度
7 print('shape:',array.shape)
8 print('size:',array.size)
numpy基本

可以看到numpy中也有特定定义array的格式,对比tf中可以看到很多包与框架都有自己数据类型

创建特定的数据

 1 import numpy as np
 2 
 3 a = np.array([[1,2,3],[4,5,6]])
 4 print(a.dtype)
 5 #默认为int32
 6 
 7 a = np.array([2,23,4],dtype=np.float)
 8 print(a.dtype)
 9 # np.float默认为float64 ,np.float32指定为
10 
11 #创建全0的数组
12 a = np.zeros((3,4)) # 数据全为0,3行4列
13 
14 #创建全一数组, 同时也能指定这些特定数据的 dtype:
15 a = np.ones((3,4),dtype = np.int)   # 数据为1,3行4列
16 
17 #创建全空数组, 其实每个值都是接近于零的数
18 v = np.empty(shape=(3,4),dtype=np.float) # 数据为empty,3行4列
19 
20 """
21 array([[  0.00000000e+000,   4.94065646e-324,   9.88131292e-324,
22           1.48219694e-323],
23        [  1.97626258e-323,   2.47032823e-323,   2.96439388e-323,
24           3.45845952e-323],
25        [  3.95252517e-323,   4.44659081e-323,   4.94065646e-323,
26           5.43472210e-323]])
27 """
28 
29 #用 arange 创建连续数组:  arange means 安排,排列,整理
30 a = np.arange(10,20,2) # 10-19 的数据,2步长
31 """
32 array([10, 12, 14, 16, 18])
33 """
34 
35 #使用 reshape 改变数据的形状
36 a = np.arange(12).reshape((3,4))    # 3行4列,0到11
37 
38 """
39 array([[ 0,  1,  2,  3],
40        [ 4,  5,  6,  7],
41        [ 8,  9, 10, 11]])
42 """
43 
44 
45 # 用 linspace 创建线段型数据:
46 a = np.linspace(1,10,20)    # 开始端1,结束端10,且分割成20个数据,生成线段
47 """
48 array([  1.        ,   1.47368421,   1.94736842,   2.42105263,
49          2.89473684,   3.36842105,   3.84210526,   4.31578947,
50          4.78947368,   5.26315789,   5.73684211,   6.21052632,
51          6.68421053,   7.15789474,   7.63157895,   8.10526316,
52          8.57894737,   9.05263158,   9.52631579,  10.        ])
53 """
54 
55 # 同样也能进行 reshape 工作:
56 a = np.linspace(1,10,20).reshape((5,4)) # 更改shape
57 print(a)
58 """
59 array([[  1.        ,   1.47368421,   1.94736842,   2.42105263],
60        [  2.89473684,   3.36842105,   3.84210526,   4.31578947],
61        [  4.78947368,   5.26315789,   5.73684211,   6.21052632],
62        [  6.68421053,   7.15789474,   7.63157895,   8.10526316],
63        [  8.57894737,   9.05263158,   9.52631579,  10.        ]])
64 """
原文地址:https://www.cnblogs.com/simon-idea/p/9471383.html