代码引用《python数据分析师修炼之道》清华大学出版社
代码引用:代码均用jupyter notebook编写:
numpy导入
import numpy as np
一、创建数组
1、从列表中创建数组
用np.array函数,将列表转换为数组,type(对象名)查看该对象类型。
(1)创建一维数组:
import numpy as np
distances = [10,15,17,26,20]
times = [0.3,0.47,0.55,1.20,1.0]
product_quantities = [13,5,6,10,11]
prices = [1.2,6.5,1.0,4.8,5.0]
distances = np.array(distances)
times = np.array(times)
product_quantities = np.array(product_quantities)
prices = np.array(prices)
print(distances)
print(type(distances))
print(times)
print(type(times))
# 结果为:
[10 15 17 26 20]
<class 'numpy.ndarray'>
[0.3 0.47 0.55 1.2 1. ]
<class 'numpy.ndarray'>
(2)创建多维函数
创建二维函数:np,array() (即列表的列表)
import numpy as np
A = np.array([[1,2],[3,4]])
print(A)
# 结果:
[[1 2]
[3 4]]
2、用内置Numpy函数创建数组
(1)np.zeroes()
import numpy as np
My_array = np.zeros(10,dtype=int)
print(array)
# 结果:
[0 0 0 0 0 0 0 0 0 0]
(2)np.ones()
import numpy as np
My_array = np.ones(shape=(3,5),dtype=float) # 定义 3*5的矩阵
print(My_array)
# 结果为:
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
(3)np.arange()
import numpy as np
My_array = np.arange(start=0,stop=20,step=2) # 起始点,结束点,每次迭代值step,默认值为1
print(My_array)
# 结果为:
[ 0 2 4 6 8 10 12 14 16 18]
(4)np.linspace()
import numpy as np
My_array = np.linspace(0,1,20) # 上限,下限,二者均匀间隔额数值的数量
print(My_array) # 中间所有值都是等距的
# 结果为:
[0. 0.05263158 0.10526316 0.15789474 0.21052632 0.26315789
0.31578947 0.36842105 0.42105263 0.47368421 0.52631579 0.57894737
0.63157895 0.68421053 0.73684211 0.78947368 0.84210526 0.89473684
0.94736842 1. ]
3、数组的属性
A,ndim 查看数组维度
A.shape 获取每个维度中数值的数量
A.size 表示数组中元素的总体数量
import numpy as np
My_array = np.ones(shape=(3,5),dtype=float)
print(My_array)
print(My_array.shape)
print(My_array.size)
print(My_array.ndim)
# 结果为:
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
(3, 5) #获取每个维度中数值的数量
15 # 表示数组中元素的总体数量
2 #查看数组维度
4、数组的数学计算
import numpy as np
My_array = np.arange(start=0,stop=20,step=2)
print(My_array)
print(My_array + 1)
print(My_array * 20)
print(np.sin(My_array)) # 求每个元素正弦值
print(np.exp(My_array)) # 求每个元素的指数
print(np.log(My_array))
print(np.sqrt(My_array))
结果为:
[ 0 2 4 6 8 10 12 14 16 18]
[ 1 3 5 7 9 11 13 15 17 19]
[ 0 40 80 120 160 200 240 280 320 360]
[ 0. 0.90929743 -0.7568025 -0.2794155 0.98935825 -0.54402111
-0.53657292 0.99060736 -0.28790332 -0.75098725]
[1.00000000e+00 7.38905610e+00 5.45981500e+01 4.03428793e+02
2.98095799e+03 2.20264658e+04 1.62754791e+05 1.20260428e+06
8.88611052e+06 6.56599691e+07]
[ -inf 0.69314718 1.38629436 1.79175947 2.07944154 2.30258509
2.48490665 2.63905733 2.77258872 2.89037176]
[0. 1.41421356 2. 2.44948974 2.82842712 3.16227766
3.46410162 3.74165739 4. 4.24264069]
5、索引、切片、重构
**索引:**常用于获取、设置数组元素值。
import numpy as np
one_dim = np.linspace(-0.5,0.6,12)
print(one_dim)
print(one_dim[0]) # 获取其中一个值
one_dim[0]=1 # 修改索引为0的值
print(one_dim)
# 结果为:
[-0.5 -0.4 -0.3 -0.2 -0.1 0. 0.1 0.2 0.3 0.4 0.5 0.6]
-0.5
[ 1. -0.4 -0.3 -0.2 -0.1 0. 0.1 0.2 0.3 0.4 0.5 0.6]
切片:在较大的数组中获取或设置一个较小的数组
import numpy as np
one_dim = np.linspace(-0.5,0.6,12)
print(one_dim)
print(one_dim[2:5]) # 下标为2-4
print(one_dim[:5]) # 正数5个
print(one_dim[-5:]) # 倒数5个
# 结果为:
[-0.5 -0.4 -0.3 -0.2 -0.1 0. 0.1 0.2 0.3 0.4 0.5 0.6]
[-0.3 -0.2 -0.1]
[-0.5 -0.4 -0.3 -0.2 -0.1]
[0.2 0.3 0.4 0.5 0.6]
# 二维数组切片
import numpy as np
two_dim=np.array([[-1,2,3,4],[5,6,7,8],[9,0,2,3]])
print(two_dim)
print(two_dim[:,1:3])
结果:
[[-1 2 3 4]
[ 5 6 7 8]
[ 9 0 2 3]]
[[2 3]
[6 7]
[0 2]]
重构:将数组从一个维度调整至另一个维度
import numpy as np
two_dim=np.array([[-1,2,3,4],[5,6,7,8],[9,0,2,3]])
print(two_dim.flatten()) # 二维转一维
print(two_dim.reshape(4,3)) # 一维转多维
# 结果:[-1 2 3 4 5 6 7 8 9 0 2 3]
[-1 2 3 4 5 6 7 8 9 0 2 3]
[[-1 2 3]
[ 4 5 6]
[ 7 8 9]
[ 0 2 3]]