numpy是python进行科学计算的基础包,它包括如下的内容:
- 一个强大的N维数组。
- 复杂的(广播)功能。
- 用于集成C/C++和fortran代码的工具。
- 有用的线性代数,傅里叶变换和随机数功能。
除了明显的科学用途外,numpy还可以用作通用数据的高效多维容器,可以定义任意数据类型,使得numpy可以高效无缝的与各种数据库集成,并且广泛用于以下任务: - 机器学习模型:在编写机器学习算法时,需要对矩阵进行各种数值计算,如矩阵乘法,换位,加法等,numpy提供了一个非常好的库,用于简单和快速的计算,numpy数组用于存储训练数据和几位学习模型的参数。
- 图像处理和计算机图形学:计算机中的图像表示为多维数字数组,numpy提供了一个优秀的函数来快速处理图像。
- 数字任务:numpy对于执行各种数学任务非常有用,如数值积分,微分,内插,外推等。
1.numpy中的数组
numpy提供的最重要的数据结构是一个称为numpy数组的强大对象,其配备了大量的函数和运算符。
1.1 numpy数组基本操作
import numpy as np
my_array = np.array([1,2,3,4,5])
print(my_array) #[1,2,3,4,5]
print(my_array.shape) #(5,)意思就是my_array是一个包含5个元素的数组
print(my_array[0]) #1
print(my_array[1]) #2
my_array[0]=[-1]
print(my_array) #[-1,2,3,4,5]
new_array = np.zeros((5))
print(new_array) #[0.,0.,0.,0.,0.]
new_array1 = np.random.random((5)) #生成[0.11232323 0.45464 0.34546 0.73334 0.835465] 0到1之间的随机数
new_array2 = np.zeros((2,3)) #[[0. 0. 0. 0.] [0. 0. 0.]] 生成两行三列的数组
my_array1 = np.array([[4,5],[6,1]])
print(my_array1[0][1]) #输出5
print(my_array1.shape) #输出(2,2)
my_array1_column2 = my_array1[:,1] #获取所有行的第二列
print(my_array1_column2) #输出 [5,1]
1.2 numpy中的基本运算
import numpy as np
a = ([[1.0,2.0],[3.0,4.0]])
b = ([[5.0,6.0],[7.0,8.0]])
summer = a+b
difference = a-b
product = a*b
puotient = a/b
print("sum= %f" %summer)
print("difference = %f" %difference)
print("product = %f" %product)
print("puotient = %f" %puotient)
输出:
sum = [[ 6. 8.] [10. 12.]]
difference = [[-4. -4.] [-4. -4.]]
product = [[ 5. 12.] [21. 32.]]
quotient = [[0.2 0.33333333] [0.42857143 0.5 ]]
1.3 数组特殊运算符
import numpy as np
a = np.arange(10)
print(a.sum()) # >>>45
print(a.min()) # >>>0
print(a.max()) # >>>9
print(a.cumsum()) # >>>[0 1 3 6 10 15 21 28 36 45]
sum(),min()和max()函数分别是相加,找出最小和最大元素。
cumsum()首先将第一个元素和第二个元素相加,并将计算结果存入一个列表中,然后将该结果和第三个元素相加,再将该结果存储在列表中,这将对数组中所有元素执行此操作,并返回作为列表的数组之和的运行总数。
1.4 多维数组切片
import numpy as np
a = np.array([[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]])
print(a[2,4]) #输出36
print(a[0, 1:4]) #输出[21 22 23]
print(a[:,1]) #输出[21 27 33 39 45]
print(a[::2,::2]) #输出[[20 22 24]
# [32 34 36]
# [44 46 48]]
1.5 迭代数组
可以使用最基本的nditer访问数组的每个元素,使用标准的pyrhon迭代器接口准个提供每个元素。
import numpy as np
a = np.arange(6).reshape(2,3)
for x in np.nditer(a):
print(x,end=' ')
输出:
0 1 2 3 4 5
以上实例不是使用标准的C或者Fortran顺序,选择的顺序是和数组内存布局保持一致的,这样做是为了提升访问的效率,默认行序优先。
这反映了在默认情况下只需访问每个元素,而无需考虑其特定顺序。我们可以通过迭代数组的转置来看:
import numpy as np
a = np.arange(6).reshape(2,3)
for x in np.nditer(a.T): #a的转置
print(x, end=",")
print('\n')
for x in np.nditer(a.T.copy(order='C')):
print(x,end=",")
print('\n')
0,1,2,3,4,5,
0,3,1,4,2,5
从上述例子可以看出,a和a.T的遍历顺序是一样的。
for x in np.nditer(a, order=‘F’):Fortran order,列序优先
for x in np.nditer(a.T, order=‘C’):Fortran order,行序优先
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)
print ('原始数组是:')
print (a)
print ('\n')
print ('原始数组的转置是:')
b = a.T
print (b)
print ('\n')
print ('以 C 风格顺序排序:')
c = b.copy(order='C')
print (c)
for x in np.nditer(c):
print (x, end=", " )
print ('\n')
print ('以 F 风格顺序排序:')
c = b.copy(order='F')
print (c)
for x in np.nditer(c):
print (x, end=", " )
输出结果:
原始数组是:
[[ 0 5 10 15]
[20 25 30 35]
[40 45 50 55]]
原始数组的转置是:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
以 C 风格顺序排序:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
0, 20, 40, 5, 25, 45, 10, 30, 50, 15, 35, 55,
以 F 风格顺序排序:
[[ 0 20 40]
[ 5 25 45]
[10 30 50]
[15 35 55]]
0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55,
1.6 拷贝和视图
当计算和操作数组时,有时会将数组复制到新数组中,有时则不会。
>>import numpy as np
>>a = np.arange(10)
>>b = a ##不会创建新的对象,只是改变了对象的引用
>>b is a ##true
>>b.shape = 3,4 ##改变的是a
>>a.shape ##(3,4)
>>c = a.view() ##a的一个视图或者是浅拷贝
>>c is a ##False
>>c.base is a ##ture,c是a的一个视图
>>c.shape = 2,6
>>a.shape ## (3,4) ,不会改变a
>>d = a.copy() ##深拷贝,一个新的数组对象被创建,此时d和a不共享任何信息
后期继续。。。。。。