z = np.random.random((5,5))
print(z)
np.set_printoptions(precision=2)
z
z = np.random.random([5,5])
print(z)
z/1e3
z = np.arange(15)
print(z)
np.percentile(z, q=[25,50,75])
z = np.random.rand(10,10)
z[np.random.randint(10,size=5),np.random.randint(10,size=5)] = np.nan
print(z)
print('缺失值总数:\n', np.isnan(z).sum())
print('缺失值索引:\n',np.where(np.isnan(z)))
z[np.sum(np.isnan(z),axis=1) == 0]
z = np.random.randint(0,100,25).reshape(5,5)
print(z)
np.unique(z, return_counts=True)
z = np.random.randint(1,4,10)
print(z)
label_map = {1:'汽车',2:'公交车',3:'火车'}
[label_map[x] for x in z]
z1 = np.arange(3)
z2 = np.arange(3,7)
z3 = np.arange(7,10)
z = np.array([z1,z2,z3])
print(z)
np.concatenate(z)
z = np.random.randint(100, size=10)
print('array:',z)
z.argsort()
z = np.random.randint(1,100,[5,5])
print(z)
np.amax(z,axis=1)
z = np.random.randint(1,100,[5,5])
print(z)
np.apply_along_axis(np.min, arr=z, axis=1)
a = np.array([1, 2])
b = np.array([7, 8])
np.linalg.norm(b-a)
z = np.array([1 + 2j, 3 + 4j, 5 + 6j])
print('实部:',z.real)
print('虚部:',z.imag)
matrix = np.array([[1., 2.], [3., 4.]])
inverse_matrix = np.linalg.inv(matrix)
assert np.allclose(np.dot(matrix, inverse_matrix), np.eye(2))
inverse_matrix
Z-Score 标准化公式:
Z = X − m e a n ( X ) s d ( X ) Z = \frac{X-\mathrm{mean}(X)}{\mathrm{sd}(X)} Z=sd(X)X−mean(X)
def zscore(x, axis = None):
xmean = x.mean(axis=axis, keepdims=True)
xstd = np.std(x, axis=axis, keepdims=True)
zscore = (x-xmean)/xstd
return zscore
z = np.random.randint(10, size=(5,5))
print(z)
zscore(z)
Min-Max 标准化公式:
Y = Z − min ( Z ) max ( Z ) − min ( Z ) Y = \frac{Z-\min(Z)}{\max(Z)-\min(Z)} Y=max(Z)−min(Z)Z−min(Z)
def min_max(x, axis=None):
min = x.min(axis=axis, keepdims=True)
max = x.max(axis=axis, keepdims=True)
result = (x-min)/(max-min)
return result
z = np.random.randint(10, size=(5,5))
print(z)
min_max(z)
L2 范数计算公式:
L 2 = x 1 2 + x 2 2 + … + x i 2 L_2 = \sqrt{x_1^2 + x_2^2 + \ldots + x_i^2} L2=x12+x22+…+xi2
def l2_normalize(x, axis=-1, order=2):
l2 = np.linalg.norm(x, ord = order, axis=axis, keepdims=True)
l2[l2==0] = 1
return x/l2
z = np.random.randint(10, size=(5,5))
print(z)
l2_normalize(z)
Z = np.array([
[1, 2, 1, 9, 10, 3, 2, 6, 7], # 特征 A
[2, 1, 8, 3, 7, 5, 10, 7, 2], # 特征 B
[2, 1, 1, 8, 9, 4, 3, 5, 7]]) # 特征 C
np.corrcoef(Z)
M = np.matrix([[1,2,3],[4,5,6],[7,8,9]])
w,v = np.linalg.eig(M)
w,v #w为特征值,v特征向量
z = np.random.randint(1,10,10)
print(z)
print(np.diff(z, n=1)) #计算z两相邻元素差值
print(np.diff(z, n=2)) #重复计算2次
print(np.diff(z, n=3)) #重复计算3次
z = np.random.randint(1,10,10)
print(z)
"""
[第一个元素, 第一个元素 + 第二个元素, 第一个元素 + 第二个元素 + 第三个元素, ...]
"""
np.cumsum(z)
z1 = np.array([1,2,3])
z2 = np.array([4,5,6])
np.c_[z1, z2]
z1 = np.array([1,2,3])
z2 = np.array([4,5,6])
np.r_[z1, z2]
np.fromfunction(lambda i,j: (i + 1) * (j + 1), (9, 9))