前言
通常为了满足计算要求,我们会对数组进行形状变化。本模块会用到 numpy
模块,本中 numpy
全部用 np
代替,即 import numpy as np
。
一、更改数组形状
numpy.ndarray.shape()
x = np. array( [ 1 , 2 , 3 , 4 ] )
print ( x. shape)
x. shape = [ 2 , 2 ]
print ( x)
numpy.ndarray.flat()
该函数可把数组转换为一维的迭代器,可通过 for 循环输出。此处生成的是视图,故修改一维迭代器的值时,原数组对应位置的值也会改变。
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
y = x. flat
print ( y)
for i in y:
print ( i, end= ' ' )
y[ 3 ] = 0
print ( x)
numpy.ndarray.flatten()
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
y1 = x. flatten( order= 'C' )
print ( y1)
y2 = x. flatten( order= 'F' )
print ( y2)
y1[ 3 ] = 0
print ( x)
numpy.ravel()
该函数可把数组转换成一维数组,即可返回视图,也可返回副本。
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
y = np. ravel( x)
print ( y)
y[ 3 ] = 0
print ( x)
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
y2 = np. ravel( x, order= 'F' )
print ( y2)
y2[ 3 ] = 0
print ( x)
numpy.reshape(a, newshape[ ])
x = np. arange( 12 )
y = np. reshape( x, [ 3 , 4 ] )
print ( y)
y2 = np. reshape( x, [ 3 , - 1 ] )
print ( y2)
y3 = np. reshape( x, [ - 1 , 3 ] )
print ( y3)
y[ 0 , 1 ] = 10
print ( x)
x2 = np. random. randint( 12 , size= [ 2 , 2 , 3 ] )
print ( x2)
y4 = np. reshape( x2, - 1 )
print ( y4)
二、数组转置
numpy.transpose(x)
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
y = np. transpose( x)
print ( y)
numpy.ndarray.T
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
y = x. T
print ( y)
三、更改维度
numpy.newaxis
很多工具包在进行计算时都会先判断输入数据的维度是否满足要求,如果输入数据达不到指定的维度时,可以使用newaxis
参数来增加一个维度。
x = np. array( [ 1 , 2 , 3 , 4 , 5 ] )
print ( x. shape)
print ( x)
y = x[ np. newaxis, : ]
print ( y. shape)
print ( y)
y = x[ : , np. newaxis]
print ( y. shape)
print ( y)
numpy.squeeze(a, axis=None)
从数组的形状中删除单维度条目,即把shape中为1的维度去掉。 axis
用于指定需要删除的维度,但是指定的维度必须为单维度,否则将会报错。
x = np. arange( 10 )
print ( x. shape)
x = x[ np. newaxis, : ]
print ( x. shape)
y = np. squeeze( x)
print ( y. shape)
x2 = np. array( [ [ [ 0 ] , [ 1 ] , [ 2 ] ] ] )
print ( x2. shape)
y2 = np. squeeze( x2)
print ( y2. shape)
print ( y2)
y = np. squeeze( x, axis= 0 )
print ( y. shape)
print ( y)
y = np. squeeze( x, axis= 2 )
print ( y. shape)
print ( y)
y = np. squeeze( x, axis= 1 )
四、数组组合
numpy.concatenate()
x = np. array( [ 1 , 2 , 3 ] )
y = np. array( [ 7 , 8 , 9 ] )
z = np. concatenate( [ x, y] )
print ( z)
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)
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] )
y = np. array( [ [ 10 , 11 , 12 ] , [ 13 , 14 , 15 ] , [ 16 , 17 , 18 ] ] )
z = np. concatenate( [ x, y] )
print ( z)
z = np. concatenate( [ x, y] , axis= 0 )
print ( z)
z = np. concatenate( [ x, y] , axis= 1 )
print ( z)
五、数组拆分
numpy.vsplit()
x = np. array( [ [ 11 , 12 , 13 , 14 ] ,
[ 16 , 17 , 18 , 19 ] ,
[ 21 , 22 , 23 , 24 ] ,
[ 11 , 12 , 13 , 14 ] ] )
y = np. vsplit( x, 4 )
print ( y)
y2 = np. vsplit( x, 2 )
print ( y2)
numpy.hsplit()
x = np. array( [ [ 11 , 12 , 13 , 14 ] ,
[ 16 , 17 , 18 , 19 ] ,
[ 21 , 22 , 23 , 24 ] ,
[ 11 , 12 , 13 , 14 ] ] )
y = np. hsplit( x, 4 )
print ( y)
y2 = np. hsplit( x, 2 )
print ( y2)
六、数组平铺
numpy.tile(A, reps)
x = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
print ( x)
y = np. tile( x, ( 1 , 2 ) )
print ( y)
y = np. tile( x, ( 2 , 1 ) )
print ( y)
y = np. tile( x, ( 2 , 2 ) )
print ( y)
numpy.repeat()
a = np. repeat( 3 , 4 )
print ( a)
x = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] )
print ( x)
y = np. repeat( x, 2 )
print ( y)
y = np. repeat( x, 2 , axis= 0 )
print ( y)
y = np. repeat( x, [ 1 , 2 , 2 ] , axis= 0 )
print ( y)
y = np. repeat( x, [ 2 , 3 ] , axis= 1 )
print ( y)
七、删除重复元素
numpy.unique()
对于数组或者列表,unique函数去除其中重复的元素,并按元素由大到小的顺序返回一个新的无元素重复的元组或者列表.
A = [ 3 , 2 , 3 , 2 , 1 , 2 , 2 , 5 , 4 , 3 ]
a = np. unique( A)
B= ( 1 , 1 , 2 , 5 , 3 , 4 , 3 )
b= np. unique( B)
C= [ 'fgfh' , 'asd' , 'fgfh' , 'asdfds' , 'wrh' ]
c= np. unique( C)
D = np. array( [ [ 3 , 2 , 3 , 2 ] , [ 1 , 3 , 1 , 3 ] ] )
d = np. unique( D)
print ( a)
print ( b)
print ( c)
print ( d)