import numpy as np
c = np.arange(1,15).reshape(7,2);
print(c)
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]
[13 14]]
np.vsplit(c,3)
[array([[1, 2],
[3, 4]]),
array([[5, 6],
[7, 8]]),
array([[ 9, 10],
[11, 12]])]
d = c.T
d
array([[ 1, 3, 5, 7, 9, 11],
[ 2, 4, 6, 8, 10, 12]])
np.hsplit(d,3)
[array([[1, 3],
[2, 4]]),
array([[5, 7],
[6, 8]]),
array([[ 9, 11],
[10, 12]])]
import numpy as np
a = np.array((1,2,4,6))
b = np.array((7,8,9,20))
e = np.dstack((a,b))
e
array([[[ 1, 7],
[ 2, 8],
[ 4, 9],
[ 6, 20]]])
np.dsplit(e,2)
[array([[[1],
[2],
[4],
[6]]]),
array([[[ 7],
[ 8],
[ 9],
[20]]])]
inistate =np.array([1,2,3,4])
pre_inistate = inistate[0:3]
pre_inistate
array([1, 2, 3])
import numpy as np
a = np.array([1,1,1,1])
b = np.array([[1],[1],[1],[1]])
a+b
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
c = np.array([[1,1,1,1]])
c+b
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
W = np.array([[1,1,1],[2,2,2]])
W[:,1]
array([1, 2])
W[1]
array([2, 2, 2])
W[:,1] = np.array([3,3])
W
array([[1, 3, 1],
[2, 3, 2]])
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)
[[ 1 2 3 4]
[ 9 10 11 12]]
2 = np.delete(matrix, 1, 1)
print('p2\n',p2)
[[ 1 3 4]
[ 5 7 8]
[ 9 11 12]]
p3 = np.delete(matrix, 1)
print('p3\n',p3)
[ 1 3 4 5 6 7 8 9 10 11 12]
p4 = np.delete(matrix, [0,1], 1)
print('p4\n',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)
[[ 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('q2\n',q2)
[[ 1 1 2 3 4]
[ 1 5 6 7 8]
[ 1 9 10 11 12]]
q3 = np.insert(matrix, 2, [1,2,3], 1) # 第1维度(列)第2行添加[1,2,3]
print('q3\n',q3)
[[ 1 2 1 3 4]
[ 5 6 2 7 8]
[ 9 10 3 11 12]]
import numpy as np
c = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
m1 = np.append(c,[[2,2,2,2]],axis=0)
print('m1----\n',m1)
m1----
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 2 2 2 2]]
m2 = np.append(matrix,[[1],[1],[100]],axis=1)
print('m2-----\n',m2)
m2-----
[[ 1 2 3 4 1]
[ 5 6 7 8 1]
[ 9 10 11 12 100]]
m3 = np.append(matrix,[1,1,1,1])
print('m3-----\n',m3)
m3-----
[ 1 2 3 4 5 6 7 8 9 10 11 12 1 1 1 1]
import numpy as np
a = np.random.choice(8,4)
a
array([4, 5, 7, 7])
b = np.random.choice([0,1,2,23,34,45,56,89,7,55],4)
b
array([34, 56, 1, 7])
c = np.random.choice(np.array([0,1,2,3,4,5,6,7,8,9,10]),4)
c
array([0, 0, 1, 5])
d = np.random.choice([0,1,2,3,4,75,6,55,45,89],5,replace=False)
d
array([89, 2, 55, 1, 6])
f = 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])
f
array([0, 3, 6, 2, 5])
import numpy as np
a = np.array([[1,1,1],[2,2,2],[5,3,6],[25,5,4]])
a
array([[ 1, 1, 1],
[ 2, 2, 2],
[ 5, 3, 6],
[25, 5, 4]])
b1 = np.argmax(a)
b1
9
b2 = np.argmax(a, axis=0)
b2
array([3, 3, 2], dtype=int64)
b3 = np.argmax(a, axis=1)
b3
array([0, 0, 2, 0], dtype=int64)
import numpy as np
y1 = np.linspace(-5.0,5.0)
y1
array([-5. , -4.79591837, -4.59183673, -4.3877551 , -4.18367347,
-3.97959184, -3.7755102 , -3.57142857, -3.36734694, -3.16326531,
-2.95918367, -2.75510204, -2.55102041, -2.34693878, -2.14285714,
-1.93877551, -1.73469388, -1.53061224, -1.32653061, -1.12244898,
-0.91836735, -0.71428571, -0.51020408, -0.30612245, -0.10204082,
0.10204082, 0.30612245, 0.51020408, 0.71428571, 0.91836735,
1.12244898, 1.32653061, 1.53061224, 1.73469388, 1.93877551,
2.14285714, 2.34693878, 2.55102041, 2.75510204, 2.95918367,
3.16326531, 3.36734694, 3.57142857, 3.7755102 , 3.97959184,
4.18367347, 4.3877551 , 4.59183673, 4.79591837, 5. ])
y2 = np.linspace(1,9,7)
y2
array([1. , 2.33333333, 3.66666667, 5. , 6.33333333,
7.66666667, 9. ])
y3 = np.linspace(1,10,7,endpoint=False)
y3
array([1. , 2.28571429, 3.57142857, 4.85714286, 6.14285714,
7.42857143, 8.71428571])
y4= np.linspace(1, 10, 6, retstep=True
y4
(array([ 1. , 2.8, 4.6, 6.4, 8.2, 10. ]), 1.8)
import numpy as np
x = np.array([[1,2,3],[4,5,6],[1,2,3]])
x.flatten()
array([1, 2, 3, 4, 5, 6, 1, 2, 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[None,:]
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])
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(2,x)
y1
array([[2, 2, 3],
[2, 2, 4],
[5, 2, 9]])
y2 = np.minimum(3,x)
y2
array([[ 1, 2, 3],
[-3, 2, 3],
[ 3, -2, 3]])
x1 = x.copy()
x1
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x1[x1 < 2] = 0
x1
array([[0, 2, 3],
[0, 2, 4],
[5, 0, 9]])
x2 = x.copy()
x2[x2 > 3] = 2
x2
array([[ 1, 2, 3],
[-3, 2, 2],
[ 2, -2, 2]])
import numpy as np
x = np.array([[1,2,3],[-3,1,4],[1,-2,9]])
x
array([[ 1, 2, 3],
[-3, 1, 4],
[ 1, -2, 9]])