1. 程式人生 > >Numpy 基礎運算2

Numpy 基礎運算2

spa 兩個 average 最小值 merge copyright 中位數 diff nco

# -*- encoding:utf-8 -*-
# Copyright (c) 2015 Shiye Inc.
# All rights reserved.
#
# Author: ldq <[email protected]>
# 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