1、数组的创建
import numpy as np;
a=np.array([[1,2,3],[4,5,6]])
2、查看一个数组的属性
a.size #6 数组元素个数
a.shape#(2,3)2行3列
a.ndim # 2 2维
a.dtype# dtype('int32') 数据类型
3、改变数组的行数和列数
(a)reshape方法,不改变原始数组,返回一个新数组
a.reshape(3,2) # array([[1,2],
# [3,4],
#3 [5,6]]) 3行2列
(b)resize改变原数组
a.resize((1,2,3))
4、生成数组的方法
np.arange(10)
#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.linspace(0,10,10,endpoint=False) #使用linsapce函数创建数组,False表示不包括右边界
#使用logspace函数创建等比数列
np.logspace(0,10,10,endpoint=False,base=2)#起始、结束、个数、False不包括结束边界,以2为底(默认以10为底)
#array([ 1., 2., 4., 8., 16., 32., 64., 128., 256., 512.])
5、生成几类特殊的矩阵
1、零矩阵
np.zeros(2)#一维
np.zeros((2,3)) #二维
2、np.eye(3)
'''
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
'''
3、np.ones(2)
#array([1., 1.])
np.ones((2,3))
#array([[1., 1., 1.],
# [1., 1., 1.]])
6、生成随机数
1、无条件下生成随机数 0~1之间
np.random.random(size=4)#一维
np.random.random(size=(2,3))#二维
2、生成服从均匀分布的随机数
np.random.rand(2,3,2)#2个3行2列
'''array([[[0.89869258, 0.45656448],
[0.65988305, 0.73922904],
[0.43756435, 0.24842721]],
[[0.91124185, 0.8663921 ],
[0.19250349, 0.42307111],
[0.99609578, 0.54606501]]])'''
3、正太分布
np.random.randn(2,3,2)
'''
array([[[-0.8826734 , -0.81064659],
[-0.63064712, -0.66418838],
[ 0.03182761, 2.40069739]],
[[ 0.46734298, -0.03299986],
[ 1.40997849, 0.87403275],
[ 1.76658657, 2.0454799 ]]])
'''
np.random.randint(0,10,(2,3))#0到10之间,int类型,2行3列
'''
array([[0, 6, 1],
[1, 7, 8]])
'''
7、数组拼接,两种方式
a=np.arange(10).reshape(2,5)
b=np.linspace(0,1,endpoint=False,num=10).reshape(2,5)
#纵向
np.vstack((a,b))
np.concatenate((a,b),axis=0)
'''
array([[0. , 1. , 2. , 3. , 4. ],
[5. , 6. , 7. , 8. , 9. ],
[0. , 0.1, 0.2, 0.3, 0.4],
[0.5, 0.6, 0.7, 0.8, 0.9]])
'''
#横向
np. stack((a,b))
np.concatenate((a,b),axis=1)
'''
array([[0. , 1. , 2. , 3. , 4. , 0. , 0.1, 0.2, 0.3, 0.4],
[5. , 6. , 7. , 8. , 9. , 0.5, 0.6, 0.7, 0.8, 0.9]])
'''
8、数组切割,两种方法
np.hsplit(arr1,2)
np.split(arr,2,axis=1)#横向切割
np.vsplit(arr1,2)
np.split(arr,2,axis=0)#纵向切割
9、广播机制
import numpy as np
x1=np.array([1,2,3])
x2=np.array([4,5,6])
x1*2
'''
输出:
array([2,4,6])
'''
x1+x2
'''
array([5,7,9])
'''
如果两个数组维度不同,这里就触发了广播的两个法则:
a 让所有输入数组都向其中形状最长的数组看起,形状中不足的部分都通过在前面加1补齐
b 当输入数组的某个维度为1时,沿着此维度运算时都用此维度上的第一组值
import numpy as np
x1=np.array([[1,2,3],[4,5,6]])
x2=np.array([2,3,4])
#广播
x1-x2
'''
输出
array([[-1,-1,-1],[2,2,2]])
'''
10
numpy中如何进行数值舍入
import numpy as np
x1=np.array([1.45,6.36,5.32,6.98])
np.around(x1,1) #四舍五入,到小数点后1位,返回一个新数组
np.floor(x1)#向下取整,返回一个新数组
np.ceil(x1)#向上取整,返回一个新数组
11、对数组进行转置
1、transpose函数,转置后返回一个新数组
import numpy as np
x1=np.arrange(12).reshape(3,4)
np.transpose(x1)
'''
输出:
array([[0,4,8],
[1,5,9],
[2,6,10],
[3,7,11]])
原数组
array([[0,1,2,3],
[4,5,6,7],
[8,9,10,11]])
'''
2、T方法,同样返回一个新数组
x1,T
19、向数组中添加值
(1)append函数,向数组尾部追加,可以指定不同轴
import numpy as np
# 创建一个二维数组
x1 = np.array([[1,2,3],[4,5,6]])
# 直接向数组末尾添加元素,返回平铺的一维数组
np.append(x1,[7,8,9])
'''
输出:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
'''
# 沿轴 0 添加元素
np.append(x1, [[7,8,9]],axis = 0)
'''
输出:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
'''
# 沿轴 1 添加元素
np.append(x1, [[5,5,5],[7,8,9]],axis = 1)
'''
输出:
array([[1, 2, 3, 5, 5, 5],
[4, 5, 6, 7, 8, 9]])
'''
(2)
inset函数,可以沿指定轴,在数组中任意位置插入数据
import numpy as np
# 创建一个二维数组
x1 = np.array([[1,2,3],[4,5,6],[7,8,9]])
# 直接在指定位置插入元素,返回平铺的一维数组
np.insert(x1,2,[0,0,0])
'''
输出:
array([1, 2, 0, 0, 0, 3, 4, 5, 6, 7, 8, 9])
原数组:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
'''
# 指定位置,沿轴 0 插入元素
np.insert(x1,1,[0,0,0],axis=0)
'''
输出:
array([[1, 2, 3],
[0, 0, 0],
[4, 5, 6],
[7, 8, 9]])
'''
# 指定位置,沿轴 1插入元素
np.insert(x1,2,[0,0,0],axis=1)
'''
输出:
array([[1, 2, 0, 3],
[4, 5, 0, 6],
[7, 8, 0, 9]])
'''
20、对数组进进行去重
x1=np.array([2,3,5,1,3,8,1,0])
np.unique(x1)
'''
array([0,1,2,3,5,8])
'''