2021-09-03作业2


import numpy as np
a1 = np.random.choice(7,5) # 从0~7中随机选择5个数组成一维数组





    array([2, 0, 5, 0, 6])

a2 = np.random.choice([0,1,2,3,4,5,6],5) # 从给定list中随机选择5个数组成一维数组





    array([0, 6, 1, 6, 6])


a3 = np.random.choice(np.array([0,1,2,3,4,5,6]),5) # 将list换成array数组依然可以运行,效果一致






    array([0, 5, 4, 2, 1])





a4 = np.random.choice([0,1,2,3,4,5,6],5,replace=False) # 上述均有重复,将replace设置为False,即可按要求没有重复的选取

array([3, 0, 5, 4, 1])

a5 = np.random.choice(np.array([0,1,2,3,4,5,6]),5,p=[0.1,0.1,0.1,0.1,0.1,0.1,0.4])





    array([6, 3, 3, 4, 3])







import numpy as np
a = np.array([[1,1,1],[2,2,2],[0,3,6]])

array([[1, 1, 1],
       [2, 2, 2],
       [0, 3, 6]])
b1 = np.argmax(a) # 将数组a拉平,最大值索引为9(初始索引为0)


b2 = np.argmax(a, axis=0) # 按列选取最大值的索引

array([1, 2, 2], dtype=int64)

b3 = np.argmax(a, axis=1) # 按行选取最大值的索引

array([0, 0, 2], dtype=int64)

#np.zeros_like()构造全零矩阵,无需指定大小
import numpy as np
x = np.array([[1,2,3],[4,5,6]])
np.zeros_like(x)





    array([[0, 0, 0],
           [0, 0, 0]])

#numpy基本加减和取行操作
import numpy as np

A = np.arange(1, 13).reshape(6, 2)


B = np.vsplit(A, 3)

C = A.T

D = np.hsplit(C, 3)
print(A, B, C, D)
[[ 1  2]
 [ 3  4]
 [ 5  6]
 [ 7  8]
 [ 9 10]
 [11 12]] [array([[1, 2],
       [3, 4]]), array([[5, 6],
       [7, 8]]), array([[ 9, 10],
       [11, 12]])] [[ 1  3  5  7  9 11]
 [ 2  4  6  8 10 12]] [array([[1, 3],
       [2, 4]]), array([[5, 7],
       [6, 8]]), array([[ 9, 11],
       [10, 12]])]

#numpy取整


import numpy as np

a=np.array([0.125,0.568,5.688])

np.round(a)

array([0., 1., 6.])

np.round(a,decimals=2)

array([0.12, 0.57, 5.69])










array([0., 0., 5.])

np.ceil(a)

array([1., 1., 6.])

#numpy中的矩阵copy问题

import numpy as np
x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])

array([[ 1,  2,  3],
       [-3,  2,  4],
       [ 5, -2,  9]])

x1 = x.copy()
x1[x1 > 0] = 0

array([[ 0,  0,  0],
       [-3,  0,  0],
       [ 0, -2,  0]])








array([[ 1,  2,  3],
       [-3,  2,  4],
       [ 5, -2,  9]])

x2 = x

array([[ 1,  2,  3],
       [-3,  2,  4],
       [ 5, -2,  9]])

x2[x2>0] = 0

array([[ 0,  0,  0],
       [-3,  0,  0],
       [ 0, -2,  0]])

x

array([[ 0,  0,  0],
       [-3,  0,  0],
       [ 0, -2,  0]])

x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])
x3 = x[2]
x3

array([ 5, -2,  9])

x3[2] = 100
x

array([[  1,   2,   3],
       [ -3,   2,   4],
       [  5,  -2, 100]])

import numpy as np
a=np.array([[1,2,3],[4,5,6]])
a=np.array([[1,2,3,6],[4,5,6,6]])
a1=a.reshape((1,2,4))
a1
array([[[1, 2, 3, 6],
        [4, 5, 6, 6]]])
b=np.array([[3,4,5,6],[1,2,3,4],[4,5,5,5]])
b
array([[3, 4, 5, 6],
       [1, 2, 3, 4],
       [4, 5, 5, 5]])
b1=b.reshape((1,3,4)).transpose((1,0,2))
b1
array([[[3, 4, 5, 6]],

       [[1, 2, 3, 4]],

       [[4, 5, 5, 5]]])
a1
array([[[1, 2, 3, 6],
        [4, 5, 6, 6]]])
a1+b1
array([[[ 4,  6,  8, 12],
        [ 7,  9, 11, 12]],

       [[ 2,  4,  6, 10],
        [ 5,  7,  9, 10]],

       [[ 5,  7,  8, 11],
        [ 8, 10, 11, 11]]])
#random.rand和random.rand和random.randint区别
```python
import numpy as np
n = np.random.rand(3,4)
n
array([[0.5003975 , 0.8162121 , 0.45193264, 0.82332347],
       [0.09752794, 0.42988813, 0.03935796, 0.8775246 ],
       [0.85181714, 0.52776459, 0.14274664, 0.95744951]])
import numpy as np
x = np.random.randn(2,3)
x
array([[ 0.02193741,  0.758389  , -1.43269265],
       [-0.85326325, -0.37004486, -0.41419479]])
y = np.multiply(0.1,np.random.randn(2,3))+0.5 
y
array([[0.67639181, 0.42187938, 0.53135641],
       [0.57425968, 0.31920886, 0.42239423]])
import numpy as np
z = np.random.randint(2,9,(2,3))
z
array([[2, 7, 2],
       [6, 5, 8]])
m = np.random.randint(9,size = (2,3))
m
array([[8, 0, 4],
       [4, 0, 0]])

#把矩阵大于或小于N的元素置M的技巧

import numpy as np
x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])
x
array([[ 1,  2,  3],
       [-3,  2,  4],
       [ 5, -2,  9]])
y1 = np.maximum(0,x) 
y1
array([[1, 2, 3],
       [0, 2, 4],
       [5, 0, 9]])
y2 = np.minimum(0,x) 
y2
array([[ 0,  0,  0],
       [-3,  0,  0],
       [ 0, -2,  0]])
x1 = x.copy()
x1
array([[ 1,  2,  3],
       [-3,  2,  4],
       [ 5, -2,  9]])
x1[x1 < 0] = 0 
x1
array([[1, 2, 3],
       [0, 2, 4],
       [5, 0, 9]])
x2 = x.copy()
x2[x2 > 0] = 0 
x2
array([[ 0,  0,  0],
       [-3,  0,  0],
       [ 0, -2,  0]])

#矩阵删除、插入、尾部添加操作(delete,insert,append) (1)

import numpy as np
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
p1=np.delete(matrix,1,0)
print('>>>>p1>>>>\n',p1)
>>>>p1>>>>
 [[ 1  2  3  4]
 [ 9 10 11 12]]
p2 = np.delete(matrix, 1, 1)
print('>>>>p2>>>>\n',p2)
>>>>p2>>>>
 [[ 1  3  4]
 [ 5  7  8]
 [ 9 11 12]]
p3 = np.delete(matrix, 1)
print('>>>>p3>>>>\n',p3)
>>>>p3>>>>
 [ 1  3  4  5  6  7  8  9 10 11 12]
p4 = np.delete(matrix, [0,1], 1) 
print('>>>>p4>>>>\n',p4)
>>>>p4>>>>
 [[ 3  4]
 [ 7  8]
 [11 12]]
import numpy as np
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
q1 = np.insert(matrix, 1, [1,1,1,1], 0)
print('>>>>q1>>>>\n',q1)
>>>>q1>>>>
 [[ 1  2  3  4]
 [ 1  1  1  1]
 [ 5  6  7  8]
 [ 9 10 11 12]]
q2 = np.insert(matrix, 0, [1,1,1], 1)
print('>>>>2>>>>\n',q2)
>>>>2>>>>
 [[ 1  1  2  3  4]
 [ 1  5  6  7  8]
 [ 1  9 10 11 12]]
q3 = np.insert(matrix, 3, [1,1,1,1], 0) 
print('>>>>3>>>>\n',q3)
>>>>3>>>>
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [ 1  1  1  1]]
mport numpy as np
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]

  File "<ipython-input-20-d202c3e7f41f>", line 1
    mport numpy as np
          ^
SyntaxError: invalid syntax
m1 = np.append(matrix,[[1,1,1,1]],axis=0)
print('>>>>m1>>>>\n',m1)
m2 = np.append(matrix,[[1],[1],[1]],axis=1)
>>>>m1>>>>
 [[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]
 [ 1  1  1  1]]
print('>>>>m2>>>>\n',m2)
m3 = np.append(matrix,[[1],[1],[1]],axis=1)
>>>>m2>>>>
 [[ 1  2  3  4  1]
 [ 5  6  7  8  1]
 [ 9 10 11 12  1]]
print('>>>>m3>>>>\n',m3)
>>>>m3>>>>
 [[ 1  2  3  4  1]
 [ 5  6  7  8  1]
 [ 9 10 11 12  1]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值