# -*- encoding:utf-8 -*-
# Copyright (c) 2015 Shiye Inc.
# All rights reserved.
#
# Author: ldq <liangduanqi@shiyejinrong.com>
# Date: 2019/2/11 14:57 import numpy as np a = np.arange(2, 14).reshape(3, 4)
'''
reshape矩阵变维
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
'''
a_arg_max = np.argmax(a)
'''
返回最大索引
11
'''
mean_a = np.mean(a)
'''
mean平均值
7.5
'''
avg_a = np.average(a)
'''
average加权平均值
7.5
'''
mean_x = np.mean(np.array([1,2,3,4,5]))
average_x = np.average(np.array([1,2,3,4,5]), weights=np.array([1,1,1,1,1]))
median_a = np.median(a)
'''
median中位数
7.5
'''
cumsum_a = np.cumsum(a)
'''
cumsum元素累加
[ 2 5 9 14 20 27 35 44 54 65 77 90]
'''
diff_a = np.diff(a, axis=0)
'''
数值差分
[[4 4 4 4]
[4 4 4 4]]
'''
nonzero_a = np.nonzero(a)
'''
nonzero这个函数将所有非零元素的行与列坐标分割开,重构成两个分别关于行和列的矩阵
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
'''
sort_a = np.sort(a,axis=-1,kind='mergesort') transpose_a = np.transpose(a)
transpose_a_2 = a.T
'''
矩阵的转置
transpose
'''
clip_a = np.clip(a,5,9)
'''
clip指定的元素转换为最小值或者最大值
[[5 5 5 5]
[6 7 8 9]
[9 9 9 9]]
'''