垂直拆分 numpy.vsplit(数组,份数)
c = np. arange( 1 , 13 ) . reshape( 6 , 2 )
c
array([[ 1, 2],
[ 3, 4],
[ 5, 6],
[ 7, 8],
[ 9, 10],
[11, 12]])
np. vsplit( c, 3 )
[array([[1, 2],
[3, 4]]),
array([[5, 6],
[7, 8]]),
array([[ 9, 10],
[11, 12]])]
水平拆分numpy.hsplit(数组,份数)
d = c. T
d
array([[ 1, 3, 5, 7, 9, 11],
[ 2, 4, 6, 8, 10, 12]])
np. hsplit( d, 3 )
[array([[1, 3],
[2, 4]]),
array([[5, 7],
[6, 8]]),
array([[ 9, 11],
[10, 12]])]
numpy.dsplit(数组,份数)
a = np. arange( 11 , 20 ) . reshape( - 1 , 1 )
b = np. arange( 21 , 30 ) . reshape( - 1 , 1 )
e = np. dstack( ( a, b) )
e
array([[[11, 21]],
[[12, 22]],
[[13, 23]],
[[14, 24]],
[[15, 25]],
[[16, 26]],
[[17, 27]],
[[18, 28]],
[[19, 29]]])
np. dsplit( e, 2 )
[array([[[11]],
[[12]],
[[13]],
[[14]],
[[15]],
[[16]],
[[17]],
[[18]],
[[19]]]),
array([[[21]],
[[22]],
[[23]],
[[24]],
[[25]],
[[26]],
[[27]],
[[28]],
[[29]]])]
灵活选择引用
inistate = np. array( [ 1 , 2 , 3 , 4 ] )
pre_inistate = inistate[ 0 : 3 ]
pre_inistate
array( [ 0.18257419 , 0.36514837 , 0.54772256 ] )
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-35-76d38f75b0a2> in <module>
3 pre_inistate
4
----> 5 array([0.18257419,0.36514837,0.54772256])
NameError: name 'array' is not defined
numpy基本加减和取行操作
a = np. array( [ 1 , 1 , 1 , 1 ] )
b = np. array( [ [ 1 ] , [ 1 ] , [ 1 ] , [ 1 ] ] )
a+ b
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
c = np. array( [ [ 1 , 1 , 1 , 1 ] ] )
c+ b
array([[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]])
w = np. array( [ [ 1 , 1 , 1 ] , [ 2 , 2 , 2 ] ] )
w[ : , 1 ]
array([1, 2])
w[ 1 ]
array([2, 2, 2])
w[ : , 1 ] = np. array( [ 5 , 5 ] )
w
array([[1, 5, 1],
[2, 5, 2]])
矩阵删除、插入、尾部添加操作(delete,insert,append)
delete()函数
import numpy as np
matrix = [
[ 1 , 2 , 3 , 4 ] ,
[ 5 , 6 , 7 , 8 ] ,
[ 9 , 10 , 11 , 12 ]
]
p1 = np. delete( matrix, 1 , 0 )
print ( '>>>>p1>>>>\n' , p1)
>>>>p1>>>>
[[ 1 2 3 4]
[ 9 10 11 12]]
p3 = np. delete( matrix, 1 )
print ( '>>>>p3>>>>\n' , p3)
>>>>p3>>>>
[ 1 3 4 5 6 7 8 9 10 11 12]
p4 = np. delete( matrix, [ 0 , 1 ] , 1 )
print ( '>>>>p4>>>>\n' , p4)
>>>>p4>>>>
[[ 3 4]
[ 7 8]
[11 12]]
insert()函数
import numpy as np
matrix = [ [ 1 , 2 , 3 , 4 ] ,
[ 5 , 6 , 7 , 8 ] ,
[ 9 , 10 , 11 , 12 ] ]
q1 = np. insert( matrix, 1 , [ 1 , 1 , 1 , 1 ] , 0 )
print ( '>>>>q1>>>>\n' , q1)
>>>>q1>>>>
[[ 1 2 3 4]
[ 1 1 1 1]
[ 5 6 7 8]
[ 9 10 11 12]]
q2 = np. insert( matrix, 0 , [ 1 , 1 , 1 ] , 1 )
print ( '>>>>q2>>>>\n' , q2)
>>>>q2>>>>
[[ 1 1 2 3 4]
[ 1 5 6 7 8]
[ 1 9 10 11 12]]
q3 = np. insert( matrix, 3 , [ 1 , 1 , 1 , 1 ] , 0 )
print ( '>>>>q3>>>>\n' , q3)
>>>>q3>>>>
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 1 1 1]]
append()函数
import numpy as np
matrix = [
[ 1 , 2 , 3 , 4 ] ,
[ 5 , 6 , 7 , 8 ] ,
[ 9 , 10 , 11 , 12 ]
]
m1 = np. append( matrix, [ [ 1 , 1 , 1 , 1 ] ] , 0 )
m1
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[ 1, 1, 1, 1]])
m2 = np. append( matrix, [ [ 1 ] , [ 1 ] , [ 1 ] ] , 1 )
m2
array([[ 1, 2, 3, 4, 1],
[ 5, 6, 7, 8, 1],
[ 9, 10, 11, 12, 1]])
m3 = np. append( matrix, [ 1 , 1 , 1 , 1 ] )
m3
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 1, 1, 1])
np.random.choice(a,size,replace,p)
import numpy as np
a1 = np. random. choice( 7 , 5 )
a1
array([3, 0, 5, 3, 1])
a2 = np. random. choice( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] , 5 )
a2
array([1, 3, 3, 6, 1])
a3 = np. random. choice( np. array( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] ) , 5 )
a3
array([0, 3, 0, 1, 3])
a4 = np. random. choice( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] , 5 , replace= False )
a4
array([1, 3, 2, 4, 5])
a5 = np. random. choice( np. array( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] ) , 5 , p= [ 0.1 , 0.1 , 0.1 , 0.1 , 0.1 , 0.1 , 0.4 ] )
a5
array([2, 3, 5, 6, 6])
给出选取概率p
np.argmax(a,axis = None , out = None)
import numpy as np
a = np. array( [ [ 1 , 1 , 1 ] , [ 2 , 2 , 2 ] , [ 0 , 3 , 6 ] ] )
a
array([[1, 1, 1],
[2, 2, 2],
[0, 3, 6]])
b1 = np. argmax( a)
b1
8
b2 = np. argmax( a, 0 )
b2
array([1, 2, 2], dtype=int64)
星号的作用
import numpy as np
y1 = np. linspace( - 10.0 , 10.0 )
y1
array([-10. , -9.59183673, -9.18367347, -8.7755102 ,
-8.36734694, -7.95918367, -7.55102041, -7.14285714,
-6.73469388, -6.32653061, -5.91836735, -5.51020408,
-5.10204082, -4.69387755, -4.28571429, -3.87755102,
-3.46938776, -3.06122449, -2.65306122, -2.24489796,
-1.83673469, -1.42857143, -1.02040816, -0.6122449 ,
-0.20408163, 0.20408163, 0.6122449 , 1.02040816,
1.42857143, 1.83673469, 2.24489796, 2.65306122,
3.06122449, 3.46938776, 3.87755102, 4.28571429,
4.69387755, 5.10204082, 5.51020408, 5.91836735,
6.32653061, 6.73469388, 7.14285714, 7.55102041,
7.95918367, 8.36734694, 8.7755102 , 9.18367347,
9.59183673, 10. ])
import numpy as np
y2 = np. linspace( 1 , 10 , 10 )
y2
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
y3 = np. linspace( 1 , 10 , 10 , endpoint= False )
y3
array([1. , 1.9, 2.8, 3.7, 4.6, 5.5, 6.4, 7.3, 8.2, 9.1])
y4= np. linspace( 1 , 10 , 6 , retstep= True )
y4
(array([ 1. , 2.8, 4.6, 6.4, 8.2, 10. ]), 1.8)
拉平操作 ravel()和faltten()及reshape(1,-1)的区别联系(补充[None,:]操作)
import numpy as np
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 1 , 2 , 3 ] ] )
x. flatten( )
array([1, 2, 3, 4, 5, 6, 1, 2, 3])
x. ravel( )
array([1, 2, 3, 4, 5, 6, 1, 2, 3])
x. ravel( 'F' )
array([1, 4, 1, 2, 5, 2, 3, 6, 3])
import numpy as np
x = np. array( [ 1 , 2 , 3 , 6 , 7 , 8 ] )
x[ None , : ]
array([[1, 2, 3, 6, 7, 8]])
x[ : , None ]
array([[1],
[2],
[3],
[6],
[7],
[8]])
x[ np. newaxis, : ]
array([[1, 2, 3, 6, 7, 8]])
np.prod()计算元素乘积
x = np. array( [ [ 1 , 2 , 3 ] , [ 2 , 3 , 4 ] ] )
np. prod( x)
144
np. prod( x, axis = 1 )
array([ 6, 24])
np. prod( x, axis = 0 )
array([ 2, 6, 12])
把矩阵大于或小于N的元素设置M的技巧
import numpy as np
x = np. array( [ [ 1 , 2 , 3 ] , [ - 3 , 2 , 4 ] , [ 5 , - 2 , 9 ] ] )
y1 = np. maximum( 0 , x)
y1
array([[1, 2, 3],
[0, 2, 4],
[5, 0, 9]])
y2 = np. minimum( 0 , x)
y2
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x1 = x. copy( )
x1
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x1[ x1< 0 ] = 0
x1
array([[1, 2, 3],
[0, 2, 4],
[5, 0, 9]])
x2 = x. copy( )
x2[ x2> 0 ] = 0
x2
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
numpy中的矩阵copy问题
import numpy as np
x = np. array( [ [ 1 , 2 , 3 ] , [ - 3 , 2 , 4 ] , [ 5 , - 2 , 9 ] ] )
x
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x1 = x. copy( )
x1[ x1> 0 ] = 0
x1
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x2 = x
x2
array([[ 1, 2, 3],
[-3, 2, 4],
[ 5, -2, 9]])
x2[ x2> 0 ] = 0
x2
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x
array([[ 0, 0, 0],
[-3, 0, 0],
[ 0, -2, 0]])
x = np. array( [ [ 1 , 2 , 3 ] , [ - 3 , 2 , 4 ] , [ 5 , - 2 , 9 ] ] )
x3 = x[ 2 ]
x3
array([ 5, -2, 9])
x3[ 2 ] = 100
x
array([[ 1, 2, 3],
[ -3, 2, 4],
[ 5, -2, 100]])
np.zeros_like()构造全零矩阵,无需指定大小
import numpy as np
x = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
np. zeros_like( x)
array([[0, 0, 0],
[0, 0, 0]])
random.rand和random.rand和random.randint区别
import numpy as np
n = np. random. rand( 3 , 4 )
n
array([[0.7145455 , 0.90306545, 0.31379237, 0.04585752],
[0.52804719, 0.37188871, 0.47103428, 0.40720235],
[0.35102696, 0.88936889, 0.58651431, 0.47273037]])
import numpy as np
x = np. random. randn( 2 , 3 )
x
array([[-0.23255388, -1.06069825, -1.17883979],
[-1.75908433, 0.85869809, -0.51665204]])
y = np. multiply( 0.1 , np. random. randn( 2 , 3 ) ) + 0.5
y
array([[0.49487041, 0.4102452 , 0.4464379 ],
[0.4806604 , 0.46342751, 0.44927088]])
import numpy as np
z = np. random. randint( 2 , 9 , ( 2 , 3 ) )
z
array([[7, 2, 4],
[8, 5, 3]])
m = np. random. randint( 9 , size = ( 2 , 3 ) )
m
array([[6, 3, 3],
[0, 1, 0]])
python 断言 assert
x = 'You are right'
type ( x)
str
assert type ( x) == str , 'x is not str'
x = [ 1 , 2 , 3 ]
type ( x)
list
乘法之间的区别
A = np. arange( 95 , 99 ) . reshape( 2 , 2 )
A
array([[95, 96],
[97, 98]])
np. pad( A, ( ( 3 , 2 ) , ( 2 , 3 ) ) , 'constant' , constant_values = ( 0 , 0 ) )
array([[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 95, 96, 0, 0, 0],
[ 0, 0, 97, 98, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0]])
b = np. array( [ [ [ 1 , 2 ] , [ 3 , 4 ] ] , [ [ 3 , 4 ] , [ 7 , 8 ] ] , [ [ 4 , 5 ] , [ 1 , 2 ] ] ] )
b
array([[[1, 2],
[3, 4]],
[[3, 4],
[7, 8]],
[[4, 5],
[1, 2]]])
np. pad( b, ( ( 0 , 0 ) , ( 1 , 1 ) , ( 1 , 1 ) ) , 'constant' , constant_values = 0 )
array([[[0, 0, 0, 0],
[0, 1, 2, 0],
[0, 3, 4, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 3, 4, 0],
[0, 7, 8, 0],
[0, 0, 0, 0]],
[[0, 0, 0, 0],
[0, 4, 5, 0],
[0, 1, 2, 0],
[0, 0, 0, 0]]])
import numpy as np
c = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
c
array([[1, 2],
[3, 4]])
c. astype( np. float32)
array([[1., 2.],
[3., 4.]], dtype=float32)
import numpy as np
x = np. array( [ 1 , 3 , 5 ] )
y = np. array( [ 4 , 6 ] )
XX, YY = np. meshgrid( x, y)
XX
array([[1, 3, 5],
[1, 3, 5]])
YY
array([[4, 4, 4],
[6, 6, 6]])
import numpy as np
x = np. array( [ [ 3 , 4 , 5 ] , [ 1 , 3 , 4 ] ] )
y = np. array( [ [ 1 , 1 , 1 ] , [ 2 , 2 , 2 ] ] )
np. hstack( ( x, y) )
array([[3, 4, 5, 1, 1, 1],
[1, 3, 4, 2, 2, 2]])
np. vstack( ( x, y) )
array([[3, 4, 5],
[1, 3, 4],
[1, 1, 1],
[2, 2, 2]])
import numpy as np
a = np. array( [ 0.125 , 0.568 , 5.688 ] )
np. round ( a)
array([0., 1., 6.])
np. round ( a, decimals = 2 )
array([0.12, 0.57, 5.69])
np. floor( a)
array([0., 0., 5.])
np. ceil( a)
array([1., 1., 6.])
np.newaxis 在特定位置增加一个维度
import numpy as np
c = np. array( [ 1 , 2 , 5 , 4 ] )
c[ : , np. newaxis]
array([[1],
[2],
[5],
[4]])
python 广播机制
import numpy as np
a = np. array( [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] )
a = np. array( [ [ 1 , 2 , 3 , 6 ] , [ 4 , 5 , 6 , 6 ] ] )
a1 = a. reshape( ( 1 , 2 , 4 ) )
a1
array([[[1, 2, 3, 6],
[4, 5, 6, 6]]])
b = np. array( [ [ 3 , 4 , 5 , 6 ] , [ 1 , 2 , 3 , 4 ] , [ 4 , 5 , 5 , 5 ] ] )
b
array([[3, 4, 5, 6],
[1, 2, 3, 4],
[4, 5, 5, 5]])
b1 = b. reshape( ( 1 , 3 , 4 ) ) . transpose( ( 1 , 0 , 2 ) )
b1
array([[[3, 4, 5, 6]],
[[1, 2, 3, 4]],
[[4, 5, 5, 5]]])
a1
array([[[1, 2, 3, 6],
[4, 5, 6, 6]]])
c = np. array( [ [ [ 1 , 2 , 5 ] , [ 3 , 4 , 6 ] ] , [ [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ] )
c
array([[[1, 2, 5],
[3, 4, 6]],
[[4, 5, 6],
[7, 8, 9]]])
c. transpose( 1 , 0 , 2 )
array([[[1, 2, 5],
[4, 5, 6]],
[[3, 4, 6],
[7, 8, 9]]])
c. transpose( 1 , 2 , 0 )
array([[[1, 4],
[2, 5],
[5, 6]],
[[3, 7],
[4, 8],
[6, 9]]])
import numpy as np
a = np. array( [ 2 , 2 , 3 , 4 , 5 , 5 , 6 , 7 ] )
a[ 0 : 7 : 2 ]
array([2, 3, 5, 6])
import numpy as np
a = np. array( [ 2 , 2 , 3 , 4 , 5 , 5 , 6 , 7 ] )
a[ 0 : : 2 ]
array([2, 3, 5, 6])
a[ : : - 1 ]
array([7, 6, 5, 5, 4, 3, 2, 2])
import numpy as np
a = np. array( [ 2 , 2 , 3 , 4 , 5 , 5 , 6 , 7 ] )
s = slice ( 0 , 7 , 2 )
a[ s]
array([2, 3, 5, 6])