文章目录


数据分析.numpy.数组的创建、形状、计算基础

一、数组的创建

在python中,numpy(Numerical Python)提供了python对多维数组对象的支持:ndarray,具有矢量运算能力,快速、节省空间。numpy支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

创建ndarray数组函数:

1.ndarray:N维数组对象(矩阵),所有元素必须是相同类型


2.ndarray属性:

ndim        维度个数;
shape 各维度大小(数组的形状);
dtype 数据类型;
size 数组的大小,数组内元素总个数;
itemsize 数组内的元素占计算机的内存大小;
data 数组在内存中的地址,一般通过索引操作元素。

【Python】数据分析.numpy.数组的创建、形状、计算基础_数组


3.常用方法:

t102.astype()    # 调整数据类型
np.round() # 修改数组小数位

【Python】数据分析.numpy.数组的创建、形状、计算基础_返回顶部_02

array与arange的区别

import numpy as np
import random

t1 = np.array([1,2,3,4])
print(t1) #[1 2 3 4]
print(type(t1)) #该数组的类型 <class 'numpy.ndarray'>
print(t1.dtype) #该数组内元素的数据类型 int32 (32/64位)

t101 = np.array([1,2,3,4],dtype=float) #添加dtype属性,手动赋数据类型
print(t101) #[1. 2. 3. 4.]
print(t101.dtype) #float64

t102 = np.array([1,0,1,1,0],dtype=bool)
print(t102) #[ True False True True False]
print(t102.dtype) #bool
t103 = t102.astype("int8") #调整数据类型
print(t103) #[1 0 1 1 0]
print(t103.dtype) #int8

#arange生成的效果与array相似,可以快速的生成数组
t2 = np.array(range(10))
print(t2) #[0 1 2 3 4 5 6 7 8 9]
t3 = np.arange(10)
print(t3) #[0 1 2 3 4 5 6 7 8 9]
t4 = np.arange(1,10,2) #从1到10,步长为2
print(t4) #[1 3 5 7 9]

#numpy中取小数
t5 = np.array([random.random() for i in range(10)])
print(t5) #[0.43902813 0.3838124 0.57498559 0.83616761 0.71580552 0.3064713 0.47795043 0.17326671 0.38841184 0.7184412 ]
print(t5.dtype) #float64

#修改小数位
t6 = np.round(t5,2)
print(t6) #[0.64 0.72 0.73 0.63 0.52 0.53 0.75 0.84 0.48 0.11]
t7 = np.round([random.random()],3)
print(t7) #[0.825]

返回顶部


二、数组的形状

t1.reshape() # 修改数组t1的形状(维度) 元素数与原维度的元素数必须一致!!!

2.1 shape — 查看

#一维数组
t1 = np.arange(12)
print(t1) #[ 0 1 2 3 4 5 6 7 8 9 10 11]
print(t1.shape) #(12,) 表示该一维数组中的元素个数

#二维数组
t2 = np.array([[1,2,3],[4,5,6]])
print(t2) #[[1 2 3]
# [4 5 6]]
print(t2.shape) #(2, 3) 表示2行3列

#三维数组
t3 = t1.reshape((2,2,3))
print(t3)
# [[[ 0 1 2]
# [ 3 4 5]]
#
# [[ 6 7 8]
# [ 9 10 11]]]
print(t3.shape) #(2,2,3)

返回顶部


2.2 reshape()— 更改

import numpy as np
#修改数组形状

#低维到高维
#二维
t4 = t1.reshape((3,4))
print(t4) #[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
print(t4.shape) #(3, 4)
#三维 假设定义24个数据
t5 = np.arange(24).reshape((2,3,4))
print(t5)
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]

# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]
print(t4.shape) #(2, 3, 4) 2块,每块3行4列.

返回顶部


2.3 数组展平与恢复

#高维到低维
t7 = np.arange(24).reshape((2,4,3)) #t7是一个三维数组
t8 = t7.reshape((24,))
print(t8) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
t9 = t7.reshape((t7.size,))
print(t9) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
t10 = t7.flatten() #直接展开
print(t10) # [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
t100 = t7.ravel()
print(t100)# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
#如果是二维转一维,还可以用(行数*列数)计算元素个数

#对t5修改形状
t6 = t5.reshape((4,6))
print(t6)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
print(t5) #尽管通过t6对t5进行修改,但是t5本身不会变,它还是一个三维数组
# [[[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]
#
# [[12 13 14 15]
# [16 17 18 19]
# [20 21 22 23]]]

t5 = t5.reshape((4,6)) #但是如果对t5本身进行修改,那么将改变t5原有的样式
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]

返回顶部


三、数组的计算(以二维数组为例)

广播机制:可以应用到每一个元素上

3.1 数组与数字计算

import numpy as np

t1 = np.arange(24)

#二维数组计算
t2 = t1.reshape((4,6))
print(t2)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]

t3 = t2+2 #加
print(t2)
# [[ 2 3 4 5 6 7]
# [ 8 9 10 11 12 13]
# [14 15 16 17 18 19]
# [20 21 22 23 24 25]]

t4 = t2-2 #减
print(t4)
# [[-2 -1 0 1 2 3]
# [ 4 5 6 7 8 9]
# [10 11 12 13 14 15]
# [16 17 18 19 20 21]]

t5 = t2*2 #乘
print(t5)
# [[ 0 2 4 6 8 10]
# [12 14 16 18 20 22]
# [24 26 28 30 32 34]
# [36 38 40 42 44 46]]

t6 = t2/2 #除
print(t6)
# [[ 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]]

说明:

t6 = t2/0  #除
print(t6)
#[[nan inf inf inf inf inf]
# [inf inf inf inf inf inf]
# [inf inf inf inf inf inf]
# [inf inf inf inf inf inf]]

当被除数为0时,按照正常的是不可以的,系统也会出现警告:​​

【Python】数据分析.numpy.数组的创建、形状、计算基础_numpy_03


​​同时会出现以上注释里的结果,这里强调一下下:​​

​​nan ---> not an number ​​

​​inf --->infinity(无穷大)

返回顶部


3.2 数组与数组计算

相同形状数组与数组计算,对应位置相互计算!!!

t5 = np.arange(24).reshape(4,6)
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
t6 = np.arange(100,124).reshape(4,6)
print(t6)
# [[100 101 102 103 104 105]
# [106 107 108 109 110 111]
# [112 113 114 115 116 117]
# [118 119 120 121 122 123]]

print(t5+t6) #加
# [[100 102 104 106 108 110]
# [112 114 116 118 120 122]
# [124 126 128 130 132 134]
# [136 138 140 142 144 146]]
print(t5-t6) #减
# [[-100 -100 -100 -100 -100 -100]
# [-100 -100 -100 -100 -100 -100]
# [-100 -100 -100 -100 -100 -100]
# [-100 -100 -100 -100 -100 -100]]
print(t5*t6) #乘
# [[ 0 101 204 309 416 525]
# [ 636 749 864 981 1100 1221]
# [1344 1469 1596 1725 1856 1989]
# [2124 2261 2400 2541 2684 2829]]
print(t5/t6) #除
# [[0. 0.00990099 0.01960784 0.02912621 0.03846154 0.04761905]
# [0.05660377 0.06542056 0.07407407 0.08256881 0.09090909 0.0990991 ]
# [0.10714286 0.11504425 0.12280702 0.13043478 0.13793103 0.14529915]
# [0.15254237 0.15966387 0.16666667 0.17355372 0.18032787 0.18699187]]

相同列数,不同行数,进行每一行的计算

t5 = np.arange(24).reshape(4,6)
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
t7 = np.arange(6)
print(t7)
#[0 1 2 3 4 5]

print(t5+t7) #加
# [[ 0 2 4 6 8 10]
# [ 6 8 10 12 14 16]
# [12 14 16 18 20 22]
# [18 20 22 24 26 28]]

print(t5-t7) #减
# [[ 0 0 0 0 0 0]
# [ 6 6 6 6 6 6]
# [12 12 12 12 12 12]
# [18 18 18 18 18 18]]

print(t5*t7) #乘
# [[ 0 1 4 9 16 25]
# [ 0 7 16 27 40 55]
# [ 0 13 28 45 64 85]
# [ 0 19 40 63 88 115]]

print(t5/t7) #除
# [[ nan 1. 1. 1. 1. 1. ]
# [ inf 7. 4. 3. 2.5 2.2]
# [ inf 13. 7. 5. 4. 3.4]
# [ inf 19. 10. 7. 5.5 4.6]]

相同行数,不同列数,进行每一列的计算

t5 = np.arange(24).reshape(4,6)
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
t8 = np.arange(4).reshape(4,1)
print(t8)
# [[0]
# [1]
# [2]
# [3]]

print(t5+t8) #加
# [[ 0 1 2 3 4 5]
# [ 7 8 9 10 11 12]
# [14 15 16 17 18 19]
# [21 22 23 24 25 26]]

print(t5-t8) #减
# [[ 0 1 2 3 4 5]
# [ 5 6 7 8 9 10]
# [10 11 12 13 14 15]
# [15 16 17 18 19 20]]

print(t5*t8) #乘
# [[ 0 0 0 0 0 0]
# [ 6 7 8 9 10 11]
# [24 26 28 30 32 34]
# [54 57 60 63 66 69]]

print(t5/t8) #除
# [[ nan inf inf inf inf inf]
# [ 6. 7. 8. 9. 10. 11. ]
# [ 6. 6.5 7. 7.5 8. 8.5 ]
# [ 6. 6.33333333 6.66666667 7. 7.33333333 7.66666667]]

行数、列数都不一样时,不可计算!!!

t5 = np.arange(24).reshape(4,6)
print(t5)
# [[ 0 1 2 3 4 5]
# [ 6 7 8 9 10 11]
# [12 13 14 15 16 17]
# [18 19 20 21 22 23]]
t9 = np.arange(6).reshape(2,3)
print(t9)
# [[0 1 2]
# [3 4 5]]

print(t5+t9) #加
print(t5-t9) #减
print(t5*t9) #乘
print(t5/t9) #除

报错:
​​ValueError: operands could not be broadcast together with shapes (4,6) (2,3)

返回顶部


四、说明

1.广播原则
如果两个数组的后缘维度(从末尾开始算起的维度)的轴长度相符或其中一方的长度为1,则认为它们是广播兼容的。广播会在缺失和(或)长度为1的维度上进行。

2.轴

轴在numpy中可以理解为方向,使用数字0,1,2…表示,对于一个一维数组只有一个0轴,对于二维数组shape(2,2),有0轴和1轴,对于三维数组shape(2,2,3),有0,1,2轴。

【Python】数据分析.numpy.数组的创建、形状、计算基础_numpy_04


【Python】数据分析.numpy.数组的创建、形状、计算基础_数据分析_05

另见一位大佬的讲解,感谢大佬的文章~

返回顶部