返回顶部
把博客园图标替换成自己的图标
把博客园图标替换成自己的图标end

Numpy_array

创建数组

#创建数组
import numpy as np
np.array([1,2,3,4])
array([1, 2, 3, 4])
np.array([[1,2,3],[4,5,6]])
array([[1, 2, 3],
       [4, 5, 6]])
np.arange(1,10,2)
array([1, 3, 5, 7, 9])
np.linspace(0,10,11,endpoint=False)
array([0.        , 0.90909091, 1.81818182, 2.72727273, 3.63636364,
       4.54545455, 5.45454545, 6.36363636, 7.27272727, 8.18181818,
       9.09090909])
np.logspace(1,6,5,base=2)
array([ 2.        ,  4.75682846, 11.3137085 , 26.90868529, 64.        ])
np.zeros(3)
array([0., 0., 0.])
np.ones(3)
array([1., 1., 1.])
np.zeros((3,3))
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])
np.identity(2)
array([[1., 0.],
       [0., 1.]])
np.empty((2,2))
array([[1., 0.],
       [0., 1.]])
np.hamming(20)
array([0.08      , 0.10492407, 0.17699537, 0.28840385, 0.42707668,
       0.5779865 , 0.7247799 , 0.85154952, 0.94455793, 0.9937262 ,
       0.9937262 , 0.94455793, 0.85154952, 0.7247799 , 0.5779865 ,
       0.42707668, 0.28840385, 0.17699537, 0.10492407, 0.08      ])
np.random.randint(0,50,(3,5))
array([[44, 20, 34, 48, 19],
       [10,  2, 33,  6,  4],
       [ 6, 41, 47, 13, 11]])
np.random.rand(10)
array([0.72563713, 0.21629557, 0.76766319, 0.84473172, 0.71750629,
       0.33606398, 0.56846908, 0.5127654 , 0.43672247, 0.4612996 ])
np.random.standard_normal(size=(3,4,2))
array([[[ 1.80177685, -0.77487526],
        [ 0.5529492 , -0.23111535],
        [ 2.4712188 ,  1.66871868],
        [-0.77256533, -1.57496315]],

       [[ 0.47400785,  0.63653893],
        [ 1.59311551, -0.15445912],
        [ 0.31625486, -0.72760209],
        [-0.97383249, -0.01773442]],

       [[-0.44467751,  0.63540944],
        [-0.99232554,  0.34250728],
        [-0.07851672, -0.59771674],
        [ 0.0743997 , -2.17189712]]])
np.diag([1,2,3])
array([[1, 0, 0],
       [0, 2, 0],
       [0, 0, 3]])

测试两个数组的对应元素是否接近

#测试两个数组的对应元素是否接近
x = np.array([1,2,3,4.001,5])
y = np.array([1,1.999,3,4.01,5.1])
print(np.allclose(x,y))
print(np.allclose(x,y,rtol=0.2)) #相对误差
print(np.allclose(x,y,atol=0.2)) #绝对误差
print(np.isclose(x,y,))
print(np.isclose(x,y,atol=0.2))
False
True
True
[ True False  True False False]
[ True  True  True  True  True]

修改数组中元素值

# 修改数组中元素值
x = np.arange(8)
x 
array([0, 1, 2, 3, 4, 5, 6, 7])
np.append(x,8) #添加一个元素
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
np.append(x,[9,10]) # 添加多个元素
array([ 0,  1,  2,  3,  4,  5,  6,  7,  9, 10])
 np.insert(x,1,8) # 在指定位置插入新元素
array([0, 8, 1, 2, 3, 4, 5, 6, 7])
x #不影响原来的数组,用下标方式修改会修改数组原来的值
array([0, 1, 2, 3, 4, 5, 6, 7])

数组与标量的运算

#数组与标量的运算
x = np.array([[1,2,3],[4,5,6],[7,8,9]])
x[0,2] = 4 #修改第一行第三列的元素值
x
array([[1, 2, 4],
       [4, 5, 6],
       [7, 8, 9]])
x[1:,1:] = 1 #切片,把行标列标大于等于1的元素值 设为 1
x
array([[1, 2, 4],
       [4, 1, 1],
       [7, 1, 1]])
x[1:,1:] = [1,2]
x
array([[1, 2, 4],
       [4, 1, 2],
       [7, 1, 2]])
x[1:,1:] = [[1,2],[3,4]] #修改多值
x
array([[1, 2, 4],
       [4, 1, 2],
       [7, 3, 4]])
x = np.array((1,2,3,4,5))
x
array([1, 2, 3, 4, 5])
2/x #数值除数组
array([2.        , 1.        , 0.66666667, 0.5       , 0.4       ])
63//x #数值除数组
array([63, 31, 21, 15, 12], dtype=int32)

数组与数组的运算

np.array([1,2,3,4]) + np.array([4,3,2,1]) #等长相加
array([5, 5, 5, 5])
np.array([1,2,3,4]) + np.array([4]) #每个元素值加 4
array([5, 6, 7, 8])
a = np.array((1,2,3))
a**a  #等长可以进行 + — * / 操作
array([ 1,  4, 27], dtype=int32)
b = np.array(([1,2,3],[4,5,6],[7,8,9]))
c = a*b  #不同维度数组乘法 广播 a中每个元素乘以 b 中对应列元素 
c
array([[ 1,  4,  9],
       [ 4, 10, 18],
       [ 7, 16, 27]])
a+b #a中每个元素加 b中的对应元素
array([[ 2,  4,  6],
       [ 5,  7,  9],
       [ 8, 10, 12]])

数组排序

x = np.array([3,1,2])
np.argsort(x) # 返回的数组值是x数组排序大小后对应的下标
array([1, 2, 0], dtype=int64)
x[_]
array([1, 2, 3])
x = np.array([3,1,2,4])
x.argmax() , x.argmin() #最大值最小值下标
(3, 1)
x.sort() #原地排序
x
array([1, 2, 3, 4])
x = np.random.randint(1,10,(2,5))
x
array([[9, 8, 5, 1, 2],
       [8, 6, 5, 5, 1]])
x.sort()
x
array([[1, 2, 5, 8, 9],
       [1, 5, 5, 6, 8]])
x.sort(axis=1) #横向排序
x
array([[1, 2, 5, 8, 9],
       [1, 5, 5, 6, 8]])

内积运算

# np.dot(x,y) 内积运算对应相乘在相加

访问数组中的元素

b = np.array(([1,2,3],[4,5,6],[7,8,9]))
b
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
b[[0,2]] #输出第一三行
array([[1, 2, 3],
       [7, 8, 9]])
b [[0,2,1],[2,1,0]] #第一个列表表示行下标 ,第二行表示列下标
array([3, 8, 4])
a = np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a[::-1]
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
a[4:] #从第四个元素取到末尾
array([4, 5, 6, 7, 8, 9])
a[6::-1]
array([6, 5, 4, 3, 2, 1, 0])
c =np.arange(25)
c
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])
c.shape = 5,5 #分成5行5列
c
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]])
c[0,2:5]  #行标为 0 列标位于  [2,5)之间
array([2, 3, 4])
c[2:5,2:5] #行列标都位于[2,5)
array([[12, 13, 14],
       [17, 18, 19],
       [22, 23, 24]])
c[[1,3],[2,4]] #第二行第三列 第四行第五列的元素
array([ 7, 19])
c [[1,3],2:4] #第2行第4行的第3,4列
array([[ 7,  8],
       [17, 18]])
c[:,[2,3,4]] # :代表所有行 后面是对应列
c[:,2:5]  #等效
array([[ 2,  3,  4],
       [ 7,  8,  9],
       [12, 13, 14],
       [17, 18, 19],
       [22, 23, 24]])
c[:,3] #第四列元素横放
array([ 3,  8, 13, 18, 23])
c[[1,3]] #第二四行
array([[ 5,  6,  7,  8,  9],
       [15, 16, 17, 18, 19]])
c[[1,3]][:,[2,4]] #在第2,4行的基础上的3,5列
array([[ 7,  9],
       [17, 19]])

数组对函数运算

x = np.arange(0,100,10,dtype=np.float)
print(x)
[ 0. 10. 20. 30. 40. 50. 60. 70. 80. 90.]
print(np.sin(x))
[ 0.         -0.54402111  0.91294525 -0.98803162  0.74511316 -0.26237485
 -0.30481062  0.77389068 -0.99388865  0.89399666]
np.cos(x) #二维数组同样适用
array([ 1.        , -0.83907153,  0.40808206,  0.15425145, -0.66693806,
        0.96496603, -0.95241298,  0.6333192 , -0.11038724, -0.44807362])
np.round(np.cos(x)) #四舍五入
array([ 1., -1.,  0.,  0., -1.,  1., -1.,  1., -0., -0.])
x = np.array(([1,2,3],[4,5,6],[7,8,9]))
print(x/2,x.shape)
np.ceil(x/2) #向上取整

[[0.5 1.  1.5]
 [2.  2.5 3. ]
 [3.5 4.  4.5]] (3, 3)





array([[1., 1., 2.],
       [2., 3., 3.],
       [4., 4., 5.]])

改变数组形状

x = np.arange(1,11)
x
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
x.shape #查看数组形状
(10,)
x.size #查看数组的元素数量
10
x.shape = 5,-1 #-1为自动计算
x
array([[ 1,  2],
       [ 3,  4],
       [ 5,  6],
       [ 7,  8],
       [ 9, 10]])
x.reshape(1,10) #返回新数组 但是不能修改数组数量
array([[ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10]])
x = np.arange(5)
x
x.resize(1,10) #修改数组元素的个数

np.resize(x,(1,3))  #使用numpy数组的resize()返回新数组 不对原数组进行修改
array([[0, 1, 2]])

数组布尔运算

x = np.random.rand(10)
x
array([0.73383499, 0.8942851 , 0.27774296, 0.59977826, 0.74607903,
       0.7105536 , 0.82654297, 0.7011962 , 0.42712937, 0.7915964 ])
x>0.5
array([ True,  True, False,  True,  True,  True,  True,  True, False,
        True])
x[x>0.5] #返回数组中大于0.5的元素
array([0.73383499, 0.8942851 , 0.59977826, 0.74607903, 0.7105536 ,
       0.82654297, 0.7011962 , 0.7915964 ])
sum ((x>0.4) & (x<0.6)) #值大于0.4且小于0.6的元素个数
2
np.all(x<1) #全部元素小于1
True
np.any(x>0.8) #存在大于0.8的元素
True
x = np.arange(1,10)
x
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
x[(x%2==0) | (x>5)] #或运算 满足后输出值
array([2, 4, 6, 7, 8, 9])

分段函数

x = np.random.randint(0,10,size=(1,10))
x
array([[8, 7, 9, 0, 6, 0, 1, 3, 7, 3]])
np.where(x<5,0,1) #小于5 对应0 大于五对应1
array([[1, 1, 1, 0, 1, 0, 0, 0, 1, 0]])
x.resize((2,5))
x
array([[8, 7, 9, 0, 6],
       [0, 1, 3, 7, 3]])
np.piecewise(x,[x<4,x>7],[lambda x : x*2,lambda x:x*3]) # 小于4的元素*2 大于7的元素*3 其他变为 0
array([[24,  0, 27,  0,  0],
       [ 0,  2,  6,  0,  6]])

数组堆叠与合并

arr1 = np.array([1,2,3])
arr2 = np.array([4,5,6])
np.hstack((arr1,arr2)) #水平堆叠
array([1, 2, 3, 4, 5, 6])
np.vstack((arr1,arr2)) #垂直堆叠
array([[1, 2, 3],
       [4, 5, 6]])
arr3 = np.array([[1],[2],[3]])
arr4 = np.array([[4],[5],[6]])
arr3
array([[1],
       [2],
       [3]])
arr4
array([[4],
       [5],
       [6]])
np.hstack((arr3,arr4))
array([[1, 4],
       [2, 5],
       [3, 6]])
np.vstack((arr3,arr4))
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6]])
np.concatenate((arr3,arr4))
array([[1],
       [2],
       [3],
       [4],
       [5],
       [6]])
np.concatenate((arr3,arr4),axis=1)
array([[1, 4],
       [2, 5],
       [3, 6]])

本章习题

import numpy as np
np.random.randint(0,50,5)
array([12,  9,  4, 20, 47])
x = np.array([1,2,3])
y = np.array([[3],[4],[5]])
x*y

array([[ 3,  6,  9],
       [ 4,  8, 12],
       [ 5, 10, 15]])
x = np.array([1,2,3])
y = np.array(([1,2,3],[4,5,6],[7,8,9]))
x+y
array([[ 2,  4,  6],
       [ 5,  7,  9],
       [ 8, 10, 12]])
posted @ 2021-10-24 21:00  灵小禹  阅读(76)  评论(0)    收藏  举报
Live2D
浏览器标题切换
浏览器标题切换end