码迷,mamicode.com
首页 > 其他好文 > 详细

Numpy 基础运算2

时间:2019-02-11 17:08:38      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:sum   最大值   mergesort   numpy   元素   中位数   返回   code   .com   

# -*- 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]]
‘‘‘

 

Numpy 基础运算2

标签:sum   最大值   mergesort   numpy   元素   中位数   返回   code   .com   

原文地址:https://www.cnblogs.com/ldq1996/p/10362212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!