1.浮点型创建数组
2.整型创建数组
3.字符串创建数组
4.从头创建数组
5.生成范围数组对象,和range函数一样都可以选择步长
二维转一维
a8 = np.arange(12)
a2 = a8.reshape((2,6))
a1 = a2.reshape((12,))
a1
三维转一维
不管多少维度统一转化为一维(不影响原数组)
a14 = a.flatten()
a14
不管多少维度统一转化为一维(影响原数组)
a = np.arange(5)
a3 = np.arange(40).reshape((5,8)) 生成五行八列的二维数组
a3
array([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31],
[32, 33, 34, 35, 36, 37, 38, 39]])
a3<20 小于20的都变成True
array([[ True, True, True, True, True, True, True, True],
[ True, True, True, True, True, True, True, True],
[ True, True, True, True, False, False, False, False],
[False, False, False, False, False, False, False, False],
[False, False, False, False, False, False, False, False]]
a3[a3<20] 生成所有大于20的一维数组
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
a3[(a3 > 10) & (a3 < 20)] 大于10而且小于20的一维数组
array([11, 12, 13, 14, 15, 16, 17, 18, 19])
a3[(a3 > 10) | (a3 < 5)] 大于10或者小于5的一维数组
array([ 0, 1, 2, 3, 4, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39])
a3[0][0] = 100 替换第一行第一个数字为100
a3
array([[100, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[ 16, 17, 18, 19, 20, 21, 22, 23],
[ 24, 25, 26, 27, 28, 29, 30, 31],
[ 32, 33, 34, 35, 36, 37, 38, 39]])
a3[0] = 10 替换第一行都是10
a3
array([[10, 10, 10, 10, 10, 10, 10, 10],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31],
[32, 33, 34, 35, 36, 37, 38, 39]])
a3[0] = [1,2,4,5,6,7,8,55] 替换第一行
a3
array([[ 1, 2, 4, 5, 6, 7, 8, 55],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 31],
[32, 33, 34, 35, 36, 37, 38, 39]])
a3[a3>30] = 20 替换大于30的都变成20
a3
array([[ 1, 2, 4, 5, 6, 7, 8, 20],
[ 8, 9, 10, 11, 12, 13, 14, 15],
[16, 17, 18, 19, 20, 21, 22, 23],
[24, 25, 26, 27, 28, 29, 30, 20],
[20, 20, 20, 20, 20, 20, 20, 20]])