Numpy是以矩阵为基础的数学计算模块,纯数学存储和处理大型矩阵,可以表示向量和矩阵的多维数组数据结构,支持大量的维度数组和矩阵运算,对数组运算提供了大量的数学函数库!
Python已有列表类型,为什么需要一个数组对象(类型)?
- 数组对象可以去掉元素间运算所需的循环,使一维向量更像单个数据
- 设置专门的数组对象,经过优化,可以提升这类应用的运算速度
eg.计算A+B,其中,A和B是一维数组:
def npSum():
a = np.array([0,1,2,3,4])
b = np.array([9,8,7,6,5])
c = a**2 + b**3
return c
npSum()
#array([729, 513, 347, 225, 141], dtype=int32)导入Numpy模块:import numpy as np
1、np.array() ---生成一个ndarray数组
a = np.array([[0,1,2,3,4],[9,8,7,6,5]])
a
#二维数组
#array([[0, 1, 2, 3, 4],
# [9, 8, 7, 6, 5]])2、ndarray对象的属性
.ndim:秩,即轴的数量或者维度的数量
a = np.array([[0,1,2,3,4],[9,8,7,6,5]])
a.ndim
#2.shape:ndarray对象的尺度,对于矩阵,n行m列
a = np.array([[0,1,2,3,4],[9,8,7,6,5]])
a.shape
#2行5列
#(2, 5).size:ndarray对象元素的个数,相当于.shape中n*m的值
a = np.array([[0,1,2,3,4],[9,8,7,6,5]])
a.size
#10.dtype:ndarry对象的元素类型
a = np.array([[0,1,2,3,4],[9,8,7,6,5]])
a.dtype
#dtype('int32').itemsize:ndarray对象中每个元素的大小,以字节为单位
a = np.array([[0,1,2,3,4],[9,8,7,6,5]])
#1字节 = 八位
a.itemsize
#43、数组的创建
#随机生成一个数组
a = np.floor(10*np.random.random((3,4)))
print (a)
#floor是向下取整,random.random((3,4))是生成一个3行4列的元素在0-1之间的数组,floor是向下取整
*************************
[[7. 8. 4. 5.]
[8. 3. 7. 0.]
[7. 2. 5. 9.]]
*************************
print (a.ravel())
#ravel()是把一个数组拉成一个向量
*************************
[6. 4. 2. 8. 9. 3. 7. 1. 6. 6. 1. 4.]
*************************
a.shape = (6, 2)
a
*************
[[0. 9.]
[7. 5.]
[2. 3.]
[1. 7.]
[1. 6.]
[7. 5.]]
*************
a.T
*************************
[[8. 1. 6. 4. 8. 9.]
[7. 8. 3. 2. 3. 0.]]
*************************
import numpy
#文件的读取,genfromtxt读取txt文件,其中","为分隔符,分割之后的元素类型为字符串
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str)
print(type(world_alcohol))
print(world_alcohol)
arange函数:类似python的range函数,通过指定开始值、终值和步长来 创建一个一维数组,注意:最终创建的数组不包含终值
arr2 = np.arange(1,10,2) #arange只能创建一维数组
arr2
#array([1, 3, 5, 7, 9])zeros函数:创建指定长度或者形状的全零数组。
arr3 = np.zeros((2,3,4)) #可以创建多维数组(2维3行4列)
arr3
*************************
array([[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]],
[[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]]])
*************************ones函数:创建指定长度或者形状的全1数组
arr4 = np.one((2,3),dtype=np.int32),(2,3)是一个字典类型,指定元素为int类型
arr4
************
[[1. 1. 1.]
[1. 1. 1.]]
************full函数:创建一个指定指定形状,指定元素的数组
arr5 = np.full((2,3,4),100)
arr5
*****************************
array([[[100, 100, 100, 100],
[100, 100, 100, 100],
[100, 100, 100, 100]],
[[100, 100, 100, 100],
[100, 100, 100, 100],
[100, 100, 100, 100]]])
******************************eye函数:创建一个正方的n*n单位矩阵,对角线为1,其余为0
arr6 = np.eye(4)
arr6
*****************************
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
*****************************np.ones_like(a):根据数组a的形状生成一个全1的数组
np.ones_like(arr4)
*****************************
array([[1., 1., 1.],
[1., 1., 1.]])
*****************************np.zeros_like(a):根据数组a的形状生成一个全0的数组
np.zeros_like(arr4)
*****************************
array([[0., 0., 0.],
[0., 0., 0.]])
*****************************np.full_like(a,val):根据数组a的形状生成一个数组,每个元素的值都是val
np.full_like(arr4,100)
*****************************
array([[100., 100., 100.],
[100., 100., 100.]])
*****************************linspace:返回指定区间,指定个数元素的数组
a = np.linspace(1,10,4,endpoint=True)
a
#array([ 1., 4., 7., 10.])
b = np.linspace(1,10,4,endpoint=False) #False不包含结尾
b
#array([1. , 3.25, 5.5 , 7.75])concatenate:数组拼接
c = np.concatenate((a,b),axis=0)#axis=0 行拼接,一般默认不写
c
#array([ 1. , 4. , 7. , 10. , 1. , 3.25, 5.5 , 7.75])
a=np.array([[1,2,3],[4,5,6]])
b=np.array([[11,21,31],[7,8,9]])
np.concatenate((a,b),axis=1) #axis=1对应行拼接
#array([[ 1, 2, 3, 11, 21, 31],
# [ 4, 5, 6, 7, 8, 9]])4、数组的变换
reshape:在原数组的shape保持不变的前提下,改变尺寸形成新数组
arr6
********************************
array([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
********************************
arr9 = arr6.reshape(4,2,-1)#当指定某一个轴为-1的时候,表示将根据数组元素的数量自动计算该轴的 长度值。
arr9
********************************
array([[[1., 0.],
[0., 0.]],
[[0., 1.],
[0., 0.]],
[[0., 0.],
[1., 0.]],
[[0., 0.],
[0., 1.]]])
********************************修改指定位置元素值
arr9[1][0][1]=100
arr9
**************************
array([[[ 1., 0.],
[ 0., 0.]],
[[ 0., 100.],
[ 0., 0.]],
[[ 0., 0.],
[ 1., 0.]],
[[ 0., 0.],
[ 0., 1.]]])
**************************arr9源自于arr6,arr9重新赋值之后,arr6也会发生改变
arr6
#array([ 1., 0., 0., 0., 0., 100., 0., 0., 0., 0., 1.,
0., 0., 0., 0., 1.])降维
arr6.shape = (-1,) #降维
arr6
#array([ 1., 0., 0., 0., 0., 100., 0., 0., 0., 0., 1.,
0., 0., 0., 0., 1.])更改数组元素类型
arr6
array([ 1., 0., 0., 0., 0., 100., 0., 0., 0., 0., 1.,
0., 0., 0., 0., 1.])
#浮点型转化成整型
arr6.astype(np.int)
array([ 1, 0, 0, 0, 0, 100, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1])数组转化成列表
arr6.tolist()
********
[1.0,
0.0,
0.0,
0.0,
0.0,
100.0,
0.0,
0.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
1.0]
********数组的索引和切片
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[:,0:2])
#类似于Python中列表取数,先取横,后取竖
a = np.arange(24).reshape(2,3,4)
a
************************
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]]])
************************
a[:,1,-3]
#array([ 5, 17])
a[:,1:3,:]
************************
array([[[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[16, 17, 18, 19],
[20, 21, 22, 23]]])
*************************
a[:,:,::2]
******************
array([[[ 0, 2],
[ 4, 6],
[ 8, 10]],
[[12, 14],
[16, 18],
[20, 22]]])
********************
花式索引
arr10 = np.random.randint(0,40,(4,4)) #随机整数数组,(4,4)是所产生数组的shape
arr10
*************************
array([[15, 20, 26, 25],
[11, 24, 28, 19],
[15, 13, 30, 28],
[ 4, 1, 16, 4]])
**************************
arr10[[0,3]] #要行下索引为0和3的元素
**************************
array([[15, 20, 26, 25],
[ 4, 1, 16, 4]])
**************************
arr10[[1,2],[2,3]]#要1行2列和2行3列的两个元素
**************************
array([28, 28])
**************************
arr10[np.ix_([0,3],[1,3])]#要第0行和第3行,第1列和第3列的数据
******************
array([[20, 25],
[ 1, 4]])
******************
--------------------------------------------------------------------------------------
arr = np.arange(32).reshape(8,4)
arr
*************************
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]])
*************************
arr[[0,3,5],[0,2,3]] #获取(0,0),(3,2),(5,3)的数据
# array([ 0, 14, 23])
arr[[0,3,5]] #获取0,3,5行数据
*************************
array([[ 0, 1, 2, 3],
[12, 13, 14, 15],
[20, 21, 22, 23]])
**************************
arr[np.ix_([0,3,5],[0,2,3])]# 获取0、3、5行,0、2、3列数据
*************************
array([[ 0, 2, 3],
[12, 14, 15],
[20, 22, 23]])
*************************
arr[np.ix_([0,3,5],[0,2,3])]
****************************
array([[ 0, 2, 3],
[12, 14, 15],
[20, 22, 23]])
****************************布尔索引
arr11 = np.random.random((4,4))
arr11
*************************************************************
array([[0.57006061, 0.6400715 , 0.1466771 , 0.75634831],
[0.48848161, 0.23073786, 0.222856 , 0.03639575],
[0.11081246, 0.27271055, 0.47278399, 0.96284317],
[0.71150518, 0.48641716, 0.5161454 , 0.62008656]])
*************************************************************
arr12 = arr11 < 0.5
arr12
******************************************
array([[False, False, True, False],
[ True, True, True, True],
[ True, True, True, False],
[False, True, False, False]])
******************************************
arr11[arr12]
*******************************************************************
array([0.1466771 , 0.48848161, 0.23073786, 0.222856 , 0.03639575,
0.11081246, 0.27271055, 0.47278399, 0.48641716])
*******************************************************************
-------------------------------------------------------------------------------
#练习
names = np.array(['joe','tom','anne'])
classes = np.array(["数学","英语","语文"])
scores = np.array([
[70,80,90],
[77,88,91],
[80,90,70]
])
names == 'joe'
# array([ True, False, False])
#1.joe的成绩
scores[names =='joe']
# array([[70, 80, 90]])
#2.joe的数学成绩
scores[names == 'joe',classes =='数学'
# array([70])
#3,joe和anna的成绩
scores[(names =='joe') | (names =='anne')]
************************
array([[70, 80, 90],
[80, 90, 70]])
************************
#4,非joe和anne的成绩
scores[(names !='joe') & (names !='anne')]
# array([[77, 88, 91]])数组的运算
#求和
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=1)
#横的求和
# array([ 30, 75, 120])
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=0)
#竖的求和
#array([60, 75, 90])
#numpy数组运算
arr12 = np.arange(24).reshape(2,3,4)
arr12
***************************
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]]])
***************************
np.square(arr12) #平方
******************************
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]]])
******************************
arr13 = np.sqrt(arr12)
arr13
************************************************************
array([[[0. , 1. , 1.41421356, 1.73205081],
[2. , 2.23606798, 2.44948974, 2.64575131],
[2.82842712, 3. , 3.16227766, 3.31662479]],
[[3.46410162, 3.60555128, 3.74165739, 3.87298335],
[4. , 4.12310563, 4.24264069, 4.35889894],
[4.47213595, 4.58257569, 4.69041576, 4.79583152]]])
************************************************************
np.maximum(arr12,arr13)
*********************************
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.]]])
**********************************
arr12>arr13 #返回bool类型的数组
*************************************
array([[[False, False, True, True],
[ True, True, True, True],
[ True, True, True, True]],
[[ True, True, True, True],
[ True, True, True, True],
[ True, True, True, True]]])
*************************************
np.rint(arr13)#四舍五入
*************************************
array([[[0., 1., 1., 2.],
[2., 2., 2., 3.],
[3., 3., 3., 3.]],
[[3., 4., 4., 4.],
[4., 4., 4., 4.],
[4., 5., 5., 5.]]])
*************************************
np.modf(arr13)# 返回arr13的整数部分和小数部分
***********************************************************
(array([[[0. , 0. , 0.41421356, 0.73205081],
[0. , 0.23606798, 0.44948974, 0.64575131],
[0.82842712, 0. , 0.16227766, 0.31662479]],
[[0.46410162, 0.60555128, 0.74165739, 0.87298335],
[0. , 0.12310563, 0.24264069, 0.35889894],
[0.47213595, 0.58257569, 0.69041576, 0.79583152]]]),
array([[[0., 1., 1., 1.],
[2., 2., 2., 2.],
[2., 3., 3., 3.]],
[[3., 3., 3., 3.],
[4., 4., 4., 4.],
[4., 4., 4., 4.]]]))
***********************************************************
np.exp(arr12)#e的n次幂
************************************************************************
array([[[1.00000000e+00, 2.71828183e+00, 7.38905610e+00, 2.00855369e+01],
[5.45981500e+01, 1.48413159e+02, 4.03428793e+02, 1.09663316e+03],
[2.98095799e+03, 8.10308393e+03, 2.20264658e+04, 5.98741417e+04]],
[[1.62754791e+05, 4.42413392e+05, 1.20260428e+06, 3.26901737e+06],
[8.88611052e+06, 2.41549528e+07, 6.56599691e+07, 1.78482301e+08],
[4.85165195e+08, 1.31881573e+09, 3.58491285e+09, 9.74480345e+09]]])
************************************************************************numpy常用函数
#堆叠函数
#vstack:用来堆叠生成竖直的单个数组
arr14 = np.array([[1,2],[3,4]])
arr14
****************
array([[1, 2],
[3, 4]])
****************
arr15 = np.array([[7,8],[9,0]])
arr15
*********************
array([[7, 8],
[9, 0]])
*********************
np.vstack((arr14,arr15))
********************
array([[1, 2],
[3, 4],
[7, 8],
[9, 0]])
********************
#hstack :通过堆叠来生成水平的单个数组
np.hstack((arr14,arr15))
************************
array([[1, 2, 7, 8],
[3, 4, 9, 0]])
************************
# unique():去重冰沙
arr16 = np.array(['雪碧','可乐','咖啡','奶茶','冰沙','雪碧','牛奶','咖啡','冰沙','牛奶','可乐'])
arr16
#array(['雪碧', '可乐', '咖啡', '奶茶', '冰沙', '雪碧', '牛奶', '咖啡', '冰沙', '牛奶', '可乐'],
dtype='<U2')
np.unique(arr16)
#array(['冰沙', '可乐', '咖啡', '奶茶', '牛奶', '雪碧'], dtype='<U2')
# where(): 返回输入数组中满足给定条件的元素索引
arr17 = np.arange(1,10).reshape(3,-1)
arr17
********************
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
********************
# where(): 返回输入数组中满足给定条件的元素索引
arr17 = np.arange(1,10).reshape(3,-1)
arr17
**********************
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
**********************
condition = np.where(arr17>5)
condition
(array([1, 2, 2, 2], dtype=int64), array([2, 0, 1, 2], dtype=int64))
arr17[condition]
array([6, 7, 8, 9])
np.where(arr17>5,"通过","不通过")
*****************************************
array([['不通过', '不通过', '不通过'],
['不通过', '不通过', '通过'],
['通过', '通过', '通过']], dtype='<U3')
******************************************



被折叠的 条评论
为什么被折叠?



