将100个数改成10*10的形状
np1 = np.arange(0,100,1)
np1
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, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
使用shape 修改形状 可以指定行列,或者使用-1自动换行
数组自身改变。 (20列,行自动计算)
np1.shape = (-1,20)
np1
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],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
赋值给新的数组,原数组不改变。 (10行,列自动计算)
np2 = np1.reshape(10,-1)
np2
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], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
=== 数组运算 ===
1、矢量化运算
np2/2
array([[ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5], [ 5. , 5.5, 6. , 6.5, 7. , 7.5, 8. , 8.5, 9. , 9.5], [ 10. , 10.5, 11. , 11.5, 12. , 12.5, 13. , 13.5, 14. , 14.5], [ 15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5], [ 20. , 20.5, 21. , 21.5, 22. , 22.5, 23. , 23.5, 24. , 24.5], [ 25. , 25.5, 26. , 26.5, 27. , 27.5, 28. , 28.5, 29. , 29.5], [ 30. , 30.5, 31. , 31.5, 32. , 32.5, 33. , 33.5, 34. , 34.5], [ 35. , 35.5, 36. , 36.5, 37. , 37.5, 38. , 38.5, 39. , 39.5], [ 40. , 40.5, 41. , 41.5, 42. , 42.5, 43. , 43.5, 44. , 44.5], [ 45. , 45.5, 46. , 46.5, 47. , 47.5, 48. , 48.5, 49. , 49.5]])
2、 一维数组运算
如果让两个数组运算,那么会拿对应的位置做运算
Chinese = np.array([89,70,59,22])
Math = np.array([99,99,99,98])
Chinese+Math
array([188, 169, 158, 120])
3、 多维数组运算,加法
结果仍然是对应位置相加
ran1 = np.random.randint(0,100,[3,3])
print(ran1)
ran2 = np.random.randint(0,100,[3,3])
print(ran2)
ran1 + ran2
[[ 8 6 1] [ 7 21 6] [45 88 7]]
[[74 76 95] [97 88 96] [92 6 78]]
[[82, 82, 96], [104, 109, 102], [137, 94, 85]]
4、 数组广播
ran1 = np.random.randint(0,100,[3,3])
print(ran1)
addScore = (60,10,20)
ran1+addScore
[[84 25 38] [87 32 60] [41 14 76]]
[[144, 35, 58], [147, 42, 80], [101, 24, 96]]
addScore每列的元素都会加到ran1 对应列的所有元素上。
![img_c63371314f2c44c5a301f4838d71cf5c.png](https://yqfile.alicdn.com/img_c63371314f2c44c5a301f4838d71cf5c.png?x-oss-process=image/resize,w_1400/format,webp)
若要对行进行广播,则需要变换addScore数组
addScore = np.array([60,10,20]).reshape(3,1)
addScore
array([[60],
[10],
[20]])
注意:并非所有数组都能进行广播,如果addScore是2X2的数组,加到3X3的数组会发生错误。因为没有合适的广播方式了。
![img_34ada443682f8a7ae381b71088e2afa2.png](https://yqfile.alicdn.com/img_34ada443682f8a7ae381b71088e2afa2.png?x-oss-process=image/resize,w_1400/format,webp)
数组广播需要满足的条件:
a、如果第一个数组是3X3,那么广播的数组必须是1X3 或 3X1
b、广播数组的行列与源数组,要么相同,要么为1
c、多维数组的条件以此类推。