数组操作

更改形状¶
在对数组进行操作时,为了满足格式和计算的要求通常会改变其形状。
numpy.ndarray.shape表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
1.numpy创建的数组类型为: numpy.ndarray

2.创建方式:

(1)array方法:
        np.array( [1,2,4,5,6] )
        np.array( [ [1,2,3] , [3,4,5] ] )  #2*3二维数组
        np.array( (1,2,3) )   #传入元祖
        np.array( ((1,2,3), (4,5,6) )) #2*3
        np.array( [ (1,3,3) , (1,2,3) ] )

(2)zeros,ones,empty方法:
       zeros返回全0数组
        ones返回全1数组
        empty返回全0数组
        np.zeros( 6 )  #返回1*6
        np.zeros( (2,4) ) #传入元祖,返回2*4数组
        np.zeros( (2,4,5) )  #2*4*5
        # ones,empty方法使用方式类似
    (3)arange方法:
        np.arange(5)  #默认从0开始,到4的整数数组,默认步长为1
        np.array(1,5)   #从1开始到4
        np.array(1,10,3) #从1到9,步长为3
        np.array(10,3,-2) #从10到2,步长为2
    (4)linespace,logspace方法:
           np.linespace(1,10,5) #从1到10的等差数列,5个数
           np.logspace(1,2,5,base=10) #从10**1到10**2的等比数列,5个数,不设base默认以e为底
    (5)random模块:
            np.random.random( (2,3) ) #传入元祖,2*3数组,元素是0-1随机浮点数,返回类型是列表;
            np.random.rand(2,3) #不传元祖,元素是0-1随机浮点数,2*3,
            np.random.randn( 2,3 ) #不传元祖 ,元素是服从标准正态分布的随机样本,大部分在[-2,2]之间,2*3
            np.random.randint(1,10,(2,3)) #1到9之间的随机整数,2*3
            通过修改shap来修改属性

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = x.flat
print(y)

<numpy.flatiter object at 0x0000020F9BA10C60>

for i in y:
print(i, end=’ ')

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

y[3] = 0
print(end=’\n’)
print(x)

[[11 12 13 0 15]

[16 17 18 19 20]

[21 22 23 24 25]

[26 27 28 29 30]

[31 32 33 34 35]]

(3)矩阵积:

                arr01.dot(arr02)
        4.索引:
        (1)
        arr09 = np.random.randint(1,9,(3,3,4)) 
    [[[3, 2, 4, 5],
    [3, 2, 4, 2],
    [2, 1, 8, 8]],
   [[8, 1, 8, 8],
    [6, 1, 6, 2],
    [7, 7, 8, 5]],
   [[7, 6, 8, 6],
    [8, 7, 4, 4],
    [1, 6, 5, 5]]]
    arr09[:,:,:] #全部,和arr09一样
    arr09[1]   #第二个3*4数组;
    [[8 1 8 8]
  [6 1 6 2]
  [7 7 8 5]]
    arr09[:,1]  #3*4
    [[3 2 4 2]
  [6 1 6 2]
  [8 7 4 4]]
    arr09[:,:,1]  #3*3
    [[2 2 1]
  [1 1 7]
  [6 7 6]]
    arr09[0,0,0] 
    #  3
    (2)arr10[:,:] = 100  #直接修改arr10数组元素的值;
    (3)切片:
        arr11 = np.ones((2,6))  #
            [[ 1. 1. 1. 1. 1. 1.]
         [ 1.  1.  1.  1.  1.  1.]]
        arr11[:1]   #
             [[ 1. 1. 1. 1. 1. 1.]]
      (4)花式索引:
            arr12 = np.arange(32).reshape(8,4)
            [[ 0, 1, 2, 3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31]]
        arr12[0,3] #标准索引,返回3
        arr12[ [0,3,5] ]  #返回第0,3,5行
              [[ 0 1 2 3]
         [12 13 14 15]
         [20 21 22 23]]
        arr12[[0,3,5],[0,1,2]]  #返回第0行第0列,第3行第1列,第5行第2列的那个数组成的数组;
            [ 0 13 22]
        arr12[np.ix_( [0,3,5] , [0,1,2] ) ]  #有索引器,返回3*3,第0行第0列,第3行第1列,第5行第2列
            [[ 0, 1, 2],
       [12, 13, 14],
       [20, 21, 22]]

数组操作
更改形状
在对数组进行操作时,为了满足格式和计算的要求通常会改变其形状。

numpy.ndarray.shape表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。
【例】通过修改 shap 属性来改变数组的形状。

import numpy as np

x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape) # (8,)
x.shape = [2, 4]
print(x)

[[1 2 9 4]

[5 6 7 8]]

numpy.ndarray.flat 将数组转换为一维的迭代器,可以用for访问数组每一个元素。
【例】

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = x.flat
print(y)

<numpy.flatiter object at 0x0000020F9BA10C60>

for i in y:
print(i, end=’ ')

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

y[3] = 0
print(end=’\n’)
print(x)

[[11 12 13 0 15]

[16 17 18 19 20]

[21 22 23 24 25]

[26 27 28 29 30]

[31 32 33 34 35]]

numpy.ndarray.flatten([order=‘C’]) 将数组的副本转换为一维数组,并返回。
order:‘C’ – 按行,‘F’ – 按列,‘A’ – 原顺序,‘k’ – 元素在内存中的出现顺序。(简记)
order:{'C / F,'A,K},可选使用此索引顺序读取a的元素。'C’意味着以行大的C风格顺序对元素进行索引,最后一个轴索引会更改F表示以列大的Fortran样式顺序索引元素,其中第一个索引变化最快,最后一个索引变化最快。请注意,'C’和’F’选项不考虑基础数组的内存布局,仅引用轴索引的顺序.A’表示如果a为Fortran,则以类似Fortran的索引顺序读取元素在内存中连续,否则类似C的顺序。“ K”表示按照步序在内存中的顺序读取元素,但步幅为负时反转数据除外。默认情况下,使用Cindex顺序。
【例】flatten()函数返回的是拷贝。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = x.flatten()
print(y)

[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

35]

y[3] = 0
print(x)

[[11 12 13 14 15]

[16 17 18 19 20]

[21 22 23 24 25]

[26 27 28 29 30]

[31 32 33 34 35]]

x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])

y = x.flatten(order=‘F’)
print(y)

[11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30

35]

y[3] = 0
print(x)

[[11 12 13 14 15]

[16 17 18 19 20]

[21 22 23 24 25]

[26 27 28 29 30]

[31 32 33 34 35]]

numpy.ravel(a, order=‘C’)Return a contiguous flattened array.
【例】ravel()返回的是视图。

import numpy as np

x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
y = np.ravel(x)
print(y)

[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

35]

y[3] = 0
print(x)

[[11 12 13 0 15]

[16 17 18 19 20]

[21 22 23 24 25]

[26 27 28 29 30]

[31 32 33 34 35]]

【例】order=F 就是拷贝

x = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])

y = np.ravel(x, order=‘F’)
print(y)

[11 16 21 26 31 12 17 22 27 32 13 18 23 28 33 14 19 24 29 34 15 20 25 30

35]

y[3] = 0
print(x)

[[11 12 13 14 15]

[16 17 18 19 20]

[21 22 23 24 25]

[26 27 28 29 30]

[31 32 33 34 35]]

numpy.reshape(a, newshape[, order=‘C’])在不更改数据的情况下为数组赋予新的形状。
【例】reshape()函数当参数newshape = [rows,-1]时,将根据行数自动确定列数。

import numpy as np

x = np.arange(12)
y = np.reshape(x, [3, 4])
print(y.dtype) # int32
print(y)

[[ 0 1 2 3]

[ 4 5 6 7]

[ 8 9 10 11]]

y = np.reshape(x, [3, -1])
print(y)

[[ 0 1 2 3]

[ 4 5 6 7]

[ 8 9 10 11]]

y = np.reshape(x,[-1,3])
print(y)

[[ 0 1 2]

[ 3 4 5]

[ 6 7 8]

[ 9 10 11]]

y[0, 1] = 10
print(x)

[ 0 10 2 3 4 5 6 7 8 9 10 11](改变x去reshape后y中的值,x对应元素也改变)

【例】reshape()函数当参数newshape = -1时,表示将数组降为一维。

import numpy as np

x = np.random.randint(12, size=[2, 2, 3])
print(x)

[[[11 9 1]

[ 1 10 3]]

[[ 0 6 1]

[ 4 11 3]]]

y = np.reshape(x, -1)
print(y)

[11 9 1 1 10 3 0 6 1 4 11 3]

数组转置
numpy.transpose(a, axes=None) Permute the dimensions of an array.
numpy.ndarray.T Same as self.transpose(), except that self is returned if self.ndim < 2.
【例】

import numpy as np

x = np.random.rand(5, 5) * 10
x = np.around(x, 2)
print(x)

[[6.74 8.46 6.74 5.45 1.25]

[3.54 3.49 8.62 1.94 9.92]

[5.03 7.22 1.6 8.7 0.43]

[7.5 7.31 5.69 9.67 7.65]

[1.8 9.52 2.78 5.87 4.14]]

y = x.T
print(y)

[[6.74 3.54 5.03 7.5 1.8 ]

[8.46 3.49 7.22 7.31 9.52]

[6.74 8.62 1.6 5.69 2.78]

[5.45 1.94 8.7 9.67 5.87]

[1.25 9.92 0.43 7.65 4.14]]

y = np.transpose(x)
print(y)

[[6.74 3.54 5.03 7.5 1.8 ]

[8.46 3.49 7.22 7.31 9.52]

[6.74 8.62 1.6 5.69 2.78]

[5.45 1.94 8.7 9.67 5.87]

[1.25 9.92 0.43 7.65 4.14]]

更改维度
当创建一个数组之后,还可以给它增加一个维度,这在矩阵计算中经常会用到。

numpy.newaxis = None None的别名,对索引数组很有用。
【例】很多工具包在进行计算时都会先判断输入数据的维度是否满足要求,如果输入数据达不到指定的维度时,可以使用newaxis参数来增加一个维度。

import numpy as np

x = np.array([1, 2, 9, 4, 5, 6, 7, 8])
print(x.shape) # (8,)
print(x) # [1 2 9 4 5 6 7 8]

y = x[np.newaxis, :]
print(y.shape) # (1, 8)
print(y) # [[1 2 9 4 5 6 7 8]]

y = x[:, np.newaxis]
print(y.shape) # (8, 1)
print(y)

[[1]

[2]

[9]

[4]

[5]

[6]

[7]

[8]]

numpy.squeeze(a, axis=None) 从数组的形状中删除单维度条目,即把shape中为1的维度去掉。
a表示输入的数组;
axis用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错;
在机器学习和深度学习中,通常算法的结果是可以表示向量的数组(即包含两对或以上的方括号形式[[]]),如果直接利用这个数组进行画图可能显示界面为空(见后面的示例)。我们可以利用squeeze()函数将表示向量的数组转换为秩为1的数组,这样利用 matplotlib 库函数画图时,就可以正常的显示结果了。

【例】

import numpy as np

x = np.arange(10)
print(x.shape) # (10,)
x = x[np.newaxis, :]
print(x.shape) # (1, 10)
y = np.squeeze(x)
print(y.shape) # (10,)
【例】

import numpy as np

x = np.array([[[0], [1], [2]]])
print(x.shape) # (1, 3, 1)
print(x)

[[[0]

[1]

[2]]]

y = np.squeeze(x)
print(y.shape) # (3,)
print(y) # [0 1 2]

y = np.squeeze(x, axis=0)
print(y.shape) # (3, 1)
print(y)

[[0]

[1]

[2]]

y = np.squeeze(x, axis=2)
print(y.shape) # (1, 3)
print(y) # [[0 1 2]]

y = np.squeeze(x, axis=1)

ValueError: cannot select an axis to squeeze out which has size not equal to one

【例】

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[1, 4, 9, 16, 25]])
print(x.shape) # (1, 5)
plt.plot(x)
plt.show()

【例】

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[1, 4, 9, 16, 25]])
x = np.squeeze(x)
print(x.shape) # (5, )
plt.plot(x)
plt.show()

数组组合
如果要将两份数据组合到一起,就需要拼接操作。

numpy.concatenate((a1, a2, …), axis=0, out=None) Join a sequence of arrays along an existing axis.
【例】连接沿现有轴的数组序列(原来x,y都是一维的,拼接后的结果也是一维的)。

import numpy as np

x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.concatenate([x, y])
print(z)

[1 2 3 7 8 9]

z = np.concatenate([x, y], axis=0)
print(z)

[1 2 3 7 8 9]

【例】原来x,y都是二维的,拼接后的结果也是二维的。

import numpy as np

x = np.array([1, 2, 3]).reshape(1, 3)
y = np.array([7, 8, 9]).reshape(1, 3)
z = np.concatenate([x, y])
print(z)

[[ 1 2 3]

[ 7 8 9]]

z = np.concatenate([x, y], axis=0)
print(z)

[[ 1 2 3]

[ 7 8 9]]

z = np.concatenate([x, y], axis=1)
print(z)

[[ 1 2 3 7 8 9]]

【例】x,y在原来的维度上进行拼接。

import numpy as np

x = np.array([[1, 2, 3], [4, 5, 6]])
y = np.array([[7, 8, 9], [10, 11, 12]])
z = np.concatenate([x, y])
print(z)

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

z = np.concatenate([x, y], axis=0)
print(z)

[[ 1 2 3]

[ 4 5 6]

[ 7 8 9]

[10 11 12]]

z = np.concatenate([x, y], axis=1)
print(z)

[[ 1 2 3 7 8 9]

[ 4 5 6 10 11 12]]

numpy.stack(arrays, axis=0, out=None)Join a sequence of arrays along a new axis.
【例】沿着新的轴加入一系列数组(stack为增加维度的拼接)。

import numpy as np

x = np.array([1, 2, 3])
y = np.array([7, 8, 9])
z = np.stack([x, y])
print(z.shape) # (2, 3)
print(z)

[[1 2 3]

[7 8 9]]

z = np.stack([x, y], axis=1)
print(z.shape) # (3, 2)
print(z)

[[1 7]

[2 8]

[3 9]]

添加和删除元素¶
numpy.unique(ar, return_index=False, return_inverse=False,return_counts=False, axis=None) Find the unique elements of an array.
return_index:the indices of the input array that give the unique values
return_inverse:the indices of the unique array that reconstruct the input array
return_counts:the number of times each unique value comes up in the input array
【例】查找数组的唯一元素。

a=np.array([1,1,2,3,3,4,4])
b=np.unique(a,return_counts=True)
print(b[0][list(b[1]).index(1)])
#2
参考文献
参考笔记

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值