前面这些是作业上半部分,不知道为什么传不上来图片。有需要可以找我要原md文档,我用typora把截图插进去了。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FlPLoxXy-1630656352650)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210902221941074.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ncy0Xbli-1630656352652)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210902222023498.png)][外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FASP3WrX-1630656352654)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210902222049171.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WJxLWc2B-1630656352656)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210902222114201.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PfxRVhoI-1630656352658)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210902213851792.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hPzdxvpp-1630656352659)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210902213917477.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RMYmb4sp-1630656352660)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210902213950117.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bR3yN0pE-1630656352661)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210902214012353.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Q6kGSWmM-1630656352662)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210902214102611.png)]
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)
p2 = np.delete(matrix,1,1)
print('>>>>p2>>>>\n',p2)
p3 = np.delete(matrix,1)
print('>>>>p3>>>>\n',p3)
p4 = np.delete(matrix,[0,1],1)
print('>>>>p2>>>>\n',p4)
>>>>p1>>>>
[[ 1 2 3 4]
[ 9 10 11 12]]
>>>>p2>>>>
[[ 1 3 4]
[ 5 7 8]
[ 9 11 12]]
>>>>p3>>>>
[ 1 3 4 5 6 7 8 9 10 11 12]
>>>>p2>>>>
[[ 3 4]
[ 7 8]
[11 12]]
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-1-bcda948f67ec> in <module>
32 a3 = np.random.choice(np.array([0,1,2,3,4,5,6]),5)
33
---> 34 a4 = np,random.choice([0,1,2,3,4,5,6],5,replace = false)
35
36 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])
NameError: name 'random' is not defined
a1 = np.random.choice(7,5)
a2 = np.random.choice(np.array([0,1,2,3,4,5,6]),5)
a3 = np.random.choice(np.array([0,1,2,3,4,5,6]),5)
a4 = np.random.choice([0,1,2,3,4,5,6],5,replace = False)
a5 = np.random.choice(np.array([0,1,2,3,4,5,6]),5,[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.4])
a1
array([0, 4, 4, 0, 0])
a2
array([0, 0, 2, 6, 4])
a3
array([5, 0, 4, 1, 5])
a4
array([0, 2, 1, 4, 6])
a5
array([4, 5, 2, 3, 4])
import numpy as np
a = np.array([[1,1,1],[2,2,2],[0,3,6]])
b1 = np.argmax(a)
b2 = np.argmax(a,axis = 0)
b3 = np.argmax(a,axis = 1)
a
array([[1, 1, 1],
[2, 2, 2],
[0, 3, 6]])
b1
8
b2
array([1, 2, 2], dtype=int64)
b3
array([0, 0, 2], dtype=int64)
y1 = np.linspace(-10.0,10.0)
y2 = np.linspace(1,10)
y3 = np.linspace(1,10,10,endpoint = False)
y4 = np.linspace(1,10,6,retstep = True)
y1
array([-10. , -9.59183673, -9.18367347, -8.7755102 ,
-8.36734694, -7.95918367, -7.55102041, -7.14285714,
-6.73469388, -6.32653061, -5.91836735, -5.51020408,
-5.10204082, -4.69387755, -4.28571429, -3.87755102,
-3.46938776, -3.06122449, -2.65306122, -2.24489796,
-1.83673469, -1.42857143, -1.02040816, -0.6122449 ,
-0.20408163, 0.20408163, 0.6122449 , 1.02040816,
1.42857143, 1.83673469, 2.24489796, 2.65306122,
3.06122449, 3.46938776, 3.87755102, 4.28571429,
4.69387755, 5.10204082, 5.51020408, 5.91836735,
6.32653061, 6.73469388, 7.14285714, 7.55102041,
7.95918367, 8.36734694, 8.7755102 , 9.18367347,
9.59183673, 10. ])
y2
array([ 1. , 1.18367347, 1.36734694, 1.55102041, 1.73469388,
1.91836735, 2.10204082, 2.28571429, 2.46938776, 2.65306122,
2.83673469, 3.02040816, 3.20408163, 3.3877551 , 3.57142857,
3.75510204, 3.93877551, 4.12244898, 4.30612245, 4.48979592,
4.67346939, 4.85714286, 5.04081633, 5.2244898 , 5.40816327,
5.59183673, 5.7755102 , 5.95918367, 6.14285714, 6.32653061,
6.51020408, 6.69387755, 6.87755102, 7.06122449, 7.24489796,
7.42857143, 7.6122449 , 7.79591837, 7.97959184, 8.16326531,
8.34693878, 8.53061224, 8.71428571, 8.89795918, 9.08163265,
9.26530612, 9.44897959, 9.63265306, 9.81632653, 10. ])
y3
array([1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1])
y4
(array([ 1. , 2.8, 4.6, 6.4, 8.2, 10. ]), 1.8)
x = np.array([[1,2,3],[4,5,6],[1,2,3]])
x.flatten()
array([1, 4, 1, 2, 5, 2, 3, 6, 3])
x.ravel()
array([1, 2, 3, 4, 5, 6, 1, 2, 3])
x.ravel('F')
array([1, 4, 1, 2, 5, 2, 3, 6, 3])
x.flatten('F')
array([1, 4, 1, 2, 5, 2, 3, 6, 3])
x.flatten()[1] = 20
x
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3]])
x.ravel()[1] = 20
x
array([[ 1, 20, 3],
[ 4, 5, 6],
[ 1, 2, 3]])
x.reshape(1,-1)
array([[ 1, 20, 3, 4, 5, 6, 1, 2, 3]])
x = np.array([1,2,3,6,7,8])
x
array([1, 2, 3, 6, 7, 8])
x[:,None]
array([[1],
[2],
[3],
[6],
[7],
[8]])
x[np.newaxis,:]
array([[1, 2, 3, 6, 7, 8]])
x = np.array([[1,2,3],[2,3,4]])
np.prod(x)
144
np.prod(x,axis = 1)
array([ 6, 24])
np.prod(x,axis = 0)
array([ 2, 6, 12])
x = np.array([[1,2,3],[-3,2,4],[5,-2,9]])
y1 = np.maximum(0,x)
y2 = np.minimum(0,x)
x1 = x.copy()
x1[x1 < 0] = 0
x2 = x.copy()
y1
array([[1, 2, 3],
[0, 2, 4],
[5, 0, 9]])
y2
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x1
array([[1, 2, 3],
[0, 2, 4],
[5, 0, 9]])
x2
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x1 = x.copy()
x1[x1 > 0] = 0
x1
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
y1 = np.maximum(0,x)
y1
y2 = np.minimum(0,x)
y2
x1 = x.copy()
x1[x1 < 0] = 0
x1
x2 = x.copy()
x2
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x2 = x
x2[x2 > 0] = 0
x
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x1 = x.copy()
x1[x1 > 0] = 0
x1
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]])
x = np.array([[1,2,3],[4,5,6]])
np.zeros_like(x)
array([[0, 0, 0],
[0, 0, 0]])
x
array([[1, 2, 3],
[4, 5, 6]])
n1 = np.random.rand(3,4)
n1
array([[0.51241305, 0.03112844, 0.67622601, 0.3310214 ],
[0.76477322, 0.90250874, 0.51841767, 0.93479377],
[0.058693 , 0.97071122, 0.78386746, 0.4092115 ]])
np.random.uniform(3,4)
3.7835562475449755
x = np.random.rand(2,3)
x
array([[0.92220626, 0.84053281, 0.63960289],
[0.09037621, 0.37967566, 0.30503169]])
y = np.multiply(0.1,np.random.rand(2,3)) + 0.5
y
array([[0.51343938, 0.52941544, 0.59208827],
[0.51808377, 0.58891011, 0.54387176]])
z = np.random.randint(2,9,(2,3))
z
array([[5, 8, 8],
[3, 8, 2]])
m = np.random.randint(9,size = (2,3))
m
array([[2, 4, 1],
[6, 8, 3]])
x = 'Y is a string'
tape(x)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-89-833b95e545d2> in <module>
1 x = 'Y is a string'
2
----> 3 tape(x)
4
NameError: name 'tape' is not defined
type(x)
str
assert type(x) == str, 'x is not a str'
x = [1,2,3]
type(x)
list
assert type(x) == str, 'x is not a str'
Traceback(most recent call last):
File"", line 1,in
File "<ipython-input-97-0fe65c45b98a>", line 3
Traceback(most recent call last):
^
SyntaxError: invalid syntax
A = np.arange(95,99).reshape(2,2)
A
array([[95, 96],
[97, 98]])
np.pad(A,((3,2),(2,3)),'constant',constant_values = (0,0))
array([[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 95, 96, 0, 0, 0],
[ 0, 0, 97, 98, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0]])
b = np.array([[[1,2],[3,4]],[[3,4],[7,8]],[[4,5],[1,2]]])
b
array([[[1, 2],
[3, 4]],
[[3, 4],
[7, 8]],
[[4, 5],
[1, 2]]])
np.pad(b,((0,0),(1,1),(1,1)),'constant',constant_values = 0)
array([[[0, 0, 0, 0],
[0, 1, 2, 0],
[0, 3, 4, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 3, 4, 0],
[0, 7, 8, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 4, 5, 0],
[0, 1, 2, 0],
[0, 0, 0, 0]]])
x = np.empty([3,2],dtype = int)
print(x)
[[0 0]
[1 1]
[1 1]]
import numpy as np
a = np.array([[1,2,3],[1,2,3]])
b = np.array([[1,2,3],[1,2,3]])
c = np.array([[1,4,3],[1,2,3]])
print((a == b).all())
print((a == c).all())
print((a == c).any())
True
False
True
c = np.array([[1,2],[3,4]])
c
array([[1, 2],
[3, 4]])
c.astype(np.float32)
array([[1., 2.],
[3., 4.]], dtype=float32)
x = np.array([1,3,5])
y = np.array([4,6])
XX,YY = np.meshgrid(x,y)
XX
array([[1, 3, 5],
[1, 3, 5]])
YY
array([[4, 4, 4],
[6, 6, 6]])
x = np.array([[3,4,5],[1,3,4]])
y = np.array([[1,1,1],[2,2,2]])
np.hstack((x,y))
array([[3, 4, 5, 1, 1, 1],
[1, 3, 4, 2, 2, 2]])
np.vstack((x,y))
array([[3, 4, 5],
[1, 3, 4],
[1, 1, 1],
[2, 2, 2]])
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])
np.floor(a)
array([0., 0., 5.])
np.ceil(a)
array([1., 1., 6.])
c = np.array([1,2,5,4])
c[:,np.newaxis]
array([[1],
[2],
[5],
[4]])
c[np.newaxis]
array([[1, 2, 5, 4]])
a = np.array([[1,2,3,6],[4,5,6,6]])
a
array([[1, 2, 3],
[4, 5, 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 + 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]]])
a1
array([[[1, 2, 3, 6],
[4, 5, 6, 6]]])
b1
array([[[3, 4, 5, 6]],
[[1, 2, 3, 4]],
[[4, 5, 5, 5]]])
c = np.array([[[1,2,5],[3,4,6]],[[4,5,6],[7,8,9]]])
c
array([[[1, 2, 5],
[3, 4, 6]],
[[4, 5, 6],
[7, 8, 9]]])
c.transpose(1,0,2)
array([[[1, 2, 5],
[4, 5, 6]],
[[3, 4, 6],
[7, 8, 9]]])
c.transpose(1,2,0)
array([[[1, 4],
[2, 5],
[5, 6]],
[[3, 7],
[4, 8],
[6, 9]]])
a = np.array([2,2,3,4,5,5,6,7])
a[0:7:2]
array([2, 3, 5, 6])
a
array([2, 2, 3, 4, 5, 5, 6, 7])
a[0::2]
array([2, 3, 5, 6])
a[::-1]
array([7, 6, 5, 5, 4, 3, 2, 2])
a
array([2, 2, 3, 4, 5, 5, 6, 7])
s = slice(0,7,2)
a[s]
array([2, 3, 5, 6])
|