6

【Python】【numpy】ndarray

 3 years ago
source link: https://www.guofei.site/2017/07/05/ndarray.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【Python】【numpy】ndarray

2017年07月05日

Author: Guofei

文章归类: 1-2-Pandas与numpy ,文章编号: 200


版权声明:本文作者是郭飞。转载随意,但需要标明原文链接,并通知本人
原文链接:https://www.guofei.site/2017/07/05/ndarray.html

Edit

取数

  • 切片取数:共享内存(对于多维数组,如果每一维都用切片,也共享内存)
    a[3:5] # 含头不含尾
    a[:-1] # -1表示最后,仍然不含尾
    a[2,3]=7,8 # 切片用来改array
    b=a[2,3] # a和b共享内存空间
    a[1:-1:2] # 最后一个数是步长
    a[::2] # 省略表示头部
  • 用list取数:不共享内存(对于多维数组,可以用切片+list混用方式取数,只要有list就不共享内存)
    a=np.arange(1,10,1)
    b=a[[1,2,3]] # 修改b的元素值,不影响a
  • array取数:不共享内存
    a=np.arange(1,10,1)
    b=a[numpy.array([1,4,1])]
    
  • 布尔取数:不共享内存
    a=np.arange(0,5,1)
    b=a[np.array([True,False,True,False,True])]
    

一个实际案例:

a=numpy.random.rand(10)
a[a>0.5]

方法

a.shape

  • 返回a的维度
    • 如果a是一维数组,输出为(4,)
    • 二维数组,输出为(2, 1)
    • 如果某一维不规则,那么不显示这一维(2,) a.shape返回array的维度
      a.shape
      
  • 可以对a.shape赋值,效果是改变维度
    a=numpy.array([[1,2,3],[4,5,6]])
    a.shape=3,2
    
  • 在赋值时,用-1表示自动计算这一维的长度
    a=numpy.array([[1,2,3],[4,5,6]])
    a.shape=3,-1
    

reshape

  • reshape与shape的区别是,reshape保持原数组维度不变
  • 相同点是,内存是共享的
a=numpy.array([[1,2,3],[4,5,6]])
b=a.reshape((3,2))

这里生成的a和b维度不同,但是共享内存

比较而言,用shape,直接改了原array:

a=numpy.array([[1,2,3],[4,5,6]])
b=a
b.shape=3,-1

这里a,b的维度都改了

dtype

a.dtype#返回元素类型
a=numpy.array([1,2,3,4],dtype=numpy.float)#指定元素类型
a=numpy.array([1,2,3,4],dtype=numpy.complex)#指定元素类型

显示可用的dtype列表

set(numpy.typeDict.values())

统计方法

以下方法都可以对数组操作。

方法 说明 sum   mean   std,var   min,max   argmin,argmax 最小值和最大值的索引 cumsum   cumprod   sort 排序

常用方法

np.ceil
np.floor
np.rint
np.isnan

np.all
np.any

np.unique # 去重并排序

sort

import numpy as np
a=np.random.randn(10,3)
a.sort(axis=1) #直接改变a
np.sort(a) #不改变a,而是返回一个排序完毕的ndarray
  • axis=1,axis=1是对行降序,axis=0是对列降序
  • 只能降序排列,不过这无所谓,用切片就可以了
    a[:,::-1]
    

argsort

矩阵操作

常用方法

a=np.random.rand(3,4)
np.fliplr(a) # 左右翻转
np.flipud(a) # 上下翻转
np.rot90(a,k=3) # 逆时针转动k次

np.eye(N=4,M=3,k=2) # 生成一个对角阵,N*M大小,k是向右上角偏置量
np.diag(a,k=1) # 取出对角元素,放到一维数组中,返回一个 1darray
np.diag([1,2,3],k=2) # 生成对角矩阵(一维数组转二维数组)

np.triu(m=a,1) # 矩阵的上三角部分
np.tril(m=a,1) # 矩阵的下三角部分
np.tri(N=3,M=4,k=1) # 生成一个下三角矩阵,元素是0和1

np.vander(x=[1,2,3],N=5) # 生成一个 Vandermonde matrix,每一行是x中的元素,N列

(a>0.5).any(axis=0) # 每列是否有True
(a>0.5).any(axis=1) # 每行是否有True

矩阵拆拼

import numpy as np
a=np.random.rand(3,3)
b=np.random.rand(3,3)
np.concatenate([a,b],axis=0) # axis=0是竖着拼起来,axis=1是横着拼起来

scipy距离矩阵

https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html#scipy.spatial.distance.cdist

import numpy as np
from scipy import spatial

num_points = 8
points = range(num_points) # generate index of points
points_coordinate = np.random.rand(num_points, 2) # generate coordinate of points
distance_matrix = spatial.distance.cdist(points_coordinate, points_coordinate, metric='euclidean')

您的支持将鼓励我继续创作!

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK