numpy基础
import numpy
import numpy as np
numpy.array的性质
nparr = np.array([i for i in range(10)])
nparr
nparr.dtype
nparr = np.array([1, 2, 3.0,4])
nparr.dtype
nparr.ndim
nparr.shape
nparr.size
x = nparr.reshape(2,-1)
subx = x[:2, :1]
subx = x[:2, :1].copy()
subx[0][0] = 100
A = np.full(shape=(2,2), fill_value=100)
B = np.full(shape=(2,2), fill_value=10)
mix = np.concatenate([A,B])
mix = np.concatenate([A,B], axis = 1)
mix = np.vstack([A,B])
mix = np.hstack([A,b])
x = np.arange(10)
x1, x2, x3 = np.split(x, [3,7])
a = np.arange(16).reshape(4,4)
a1, a2 = np.split(a, [2], axis = 1)
a1, a2 = np.vsplit(a,[2])
a1, a2 = np.hsplit(a,[2])
numpy一些常用函数
np.zeros(10)
np.zeros(shape=(3,5), dtype=int)
np.full(shape=(3,5), fill_value=17)
np.arange(0,10,1)
np.arange(0,1,0.2)
np.linspace(0, 20, 10)
np.random.seed(666)
np.random.randint(4, 8, size = (3,5))
np.random.random((3,2))
np.random.normal(10,100)
np.random.normal(10,100,size=(3,3))
numpy.array中的运算
L = [i for i in range(10)]
2*L
L = np.arange(10)
2*L
X = np.arange(16).reshape(4,4)
X + 2
X * 2
X ** 2
X / 2
X // 2
1 / X
np.abs(X)
np.sin(X)
np.exp(X)
np.pow(3,X)
3**X
np.log(x)
np.log3(x)
A = np.arange(16).reshape(4,4)
B = np.full(shape=(4,4), fill_value=10)
A + B
A - B
A * B
A / B
A.dot(B)
A.T
v = np.arange(4)
v + A
v.dot(A)
A.dot(v)
invA = np.linalg.inv(A)
invA = np.linalg.pinv(A)
A.dot(invA)
numpy.array中的聚合
A = np.arange(16).reshape(4,-1)
np.sum(A)
np.max(A)
np.min(A)
np.sum(A, axis=0)
np.sum(A, axis=1)
np.prod(A+1)
np.mean(A)
np.median(A)
np.percentile(A, q = 50)
np.var(X)
np.std(X)
nump.array中的索引
X = np.random.normal(0, 1, size=100000)
np.mean(X)
np.std(X)
np.min(X)
np.argmin(X)
x = np.arange(16)
np.random.shuffle(x)
np.sort(x)
x.sort()
A = np.random.randint(10, size=(4,4))
np.sort(A)
np.sort(A, axis = 1)
np.sort(A, axis = 0)
np.argsort(x)
np.partition(x, 4)
np.argpartition(x,4)
fancy index
x = np.arange(16)
ind = [3,5,8]
x[ind]
ind = np.array([[0,2],
[1,3]])
x[ind]
X = x.reshape(4,4)
row = np.array([0,1,2])
col = np.array([1,2,3])
X[row, col]
X[0, col]
X[:2,col]
col = [True, False, True, True]
X[1:3, col]
numpy.array的比较
x = np.arange(16)
x < 3
x == 3
x != 3
2 * x == 24 - 4 * x
np.sum(x <= 3)
np.count_nonzero(x <= 3)
np.any(x == 0)
np.any(x >= 0)
np.sum((x > 3) & (x <= 10))
np.sum(~(x == 0))
np.sum(A % 2 == 0 , axis=1)
筛选子序列
x[x<5]
x[x % 2 == 0]
A[A[:,3]%3 == 0, :]
matplotlib 基础
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
折线图
x = np.linspace(0, 10, 100)
siny = np.sin(x)
cosy = np.cos(x)
plt.plot(x, siny, label='sin(x)')
plt.plot(x, cosy, color = 'red', label = 'cos(x)')
plt.axis([-1, 11, -2, 2])
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.legend()
plt.title('title')
plt.show()
散点图
plt.scatter(x, siny, alpha=0.5)
plt.show()