python基础
列表推导式与条件赋值
l = [ ]
def my_func ( x) :
return 2 * x
for i in range ( 5 ) :
l. append( my_func( i) )
l
[0, 2, 4, 6, 8]
[ my_func( i) for i in range ( 5 ) ]
[0, 2, 4, 6, 8]
[ m+ '_' + n for m in [ 'cat' , 'dog' ] for n in [ 'huahua' , 'xiaogou' ] ]
['cat_huahua', 'cat_xiaogou', 'dog_huahua', 'dog_xiaogou']
value = 'cat' if 2 > 1 else 'dog'
value
'cat'
value1 = 'cat' if 3 > 4 else 'dog'
value1
'dog'
l = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]
[ i if i<= 5 else 5 for i in l]
[1, 2, 3, 4, 5, 5, 5]
ll= [ ]
for i in l:
if i<= 5 :
ll. append( i)
else :
ll. append( 5 )
print ( l1)
[1, 2, 3, 4, 5, 5, 5]
匿名函数与map用法
my_func = lambda x: 2 * x
my_func( 3 )
6
multi_para_func = lambda a, b: a+ b
multi_para_func( 1 , 2 )
3
[ ( lambda x: 2 * x) ( i) for i in range ( 5 ) ]
[0, 2, 4, 6, 8]
type ( map ( lambda x: 2 * x, range ( 5 ) ) )
map
list ( map ( lambda x: 2 * x, range ( 5 ) ) )
[0, 2, 4, 6, 8]
list ( map ( lambda x, y: str ( x) + '_' + y, range ( 5 ) , list ( 'abcde' ) ) )
['0_a', '1_b', '2_c', '3_d', '4_e']
list ( 'abcde' )
['a', 'b', 'c', 'd', 'e']
zip对象与enumerate方法
l1, l2, l3 = list ( 'abc' ) , list ( '123' ) , list ( 'www' )
list ( zip ( l1, l2, l3) )
[('a', '1', 'w'), ('b', '2', 'w'), ('c', '3', 'w')]
tuple ( zip ( l1, l2, l3) )
(('a', '1', 'w'), ('b', '2', 'w'), ('c', '3', 'w'))
for i, j, k in zip ( l1, l2, l3) :
print ( i, j, k)
a 1 w
b 2 w
c 3 w
l = list ( 'abcd' )
for index, value in enumerate ( l) :
print ( index, value)
0 a
1 b
2 c
3 d
for index, value in zip ( range ( len ( l) ) , l) :
print ( index, value)
0 a
1 b
2 c
3 d
range ( len ( l) )
range(0, 4)
dict ( zip ( l1, l2) )
{'a': '1', 'b': '2', 'c': '3'}
zipped = list ( zip ( l1, l2, l3) )
zipped
[('a', '1', 'w'), ('b', '2', 'w'), ('c', '3', 'w')]
list ( zip ( * zipped) )
[('a', 'b', 'c'), ('1', '2', '3'), ('w', 'w', 'w')]
numpy基础
数组的构造
import numpy as np
np. array( [ 1 , 2 , 3 ] )
array([1, 2, 3])
np. linspace( 1 , 5 , 11 )
array([1. , 1.4, 1.8, 2.2, 2.6, 3. , 3.4, 3.8, 4.2, 4.6, 5. ])
np. arange( 1 , 5 , 2 )
array([1, 3])
np. zeros( ( 2 , 3 ) )
array([[0., 0., 0.],
[0., 0., 0.]])
np. eye( 3 )
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
np. eye( 3 , k= 1 )
array([[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]])
np. full( ( 2 , 3 ) , 10 )
array([[10, 10, 10],
[10, 10, 10]])
np. full( ( 2 , 3 ) , [ 4 , 5 , 6 ] )
array([[4, 5, 6],
[4, 5, 6]])
np. random. rand( 3 )
array([0.69693795, 0.77524397, 0.84901272])
np. random. rand( 3 , 3 )
array([[0.31053969, 0.01425395, 0.49375968],
[0.35167381, 0.33864666, 0.36161546],
[0.19646831, 0.7157192 , 0.61992155]])
a, b = 5 , 15
( b- a) * np. random. rand( 3 ) + a
array([ 9.83526578, 14.11106503, 12.64892721])
np. random. randn( 3 )
array([-0.62224203, 0.61740145, 0.41347703])
np. random. randn( 2 , 2 )
array([[ 1.61607207, 0.64803234],
[-0.16899488, -1.11458885]])
sigma, mu = 2.5 , 3
mu+ np. random. randn( 3 ) * sigma
array([4.8267152 , 6.0578538 , 3.40683054])
low, high, size = 5 , 15 , ( 2 , 3 )
np. random. randint( low, high, size)
array([[12, 8, 10],
[12, 13, 12]])
my_list = [ 'a' , 'b' , 'c' , 'd' ]
np. random. choice( my_list, 2 , replace= False , p = [ 0.1 , 0.7 , 0.1 , 0.1 ] )
array(['b', 'c'], dtype='<U1')
np. random. choice( my_list, ( 3 , 3 ) )
array([['a', 'd', 'd'],
['c', 'b', 'b'],
['a', 'a', 'a']], dtype='<U1')
np. random. permutation( my_list)
array(['a', 'c', 'b', 'd'], dtype='<U1')
数组的变形与组合
np. zeros( ( 2 , 3 ) ) . T
array([[0., 0.],
[0., 0.],
[0., 0.]])
np. r_[ np. zeros( ( 2 , 3 ) ) , np. zeros( ( 2 , 3 ) ) ]
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
np. c_[ np. zeros( ( 2 , 3 ) ) , np. zeros( ( 2 , 3 ) ) ]
array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
np. r_[ np. array( [ 0 , 0 ] ) , np. zeros( ( 2 , 1 ) ) ]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-36-911ceccd7020> in <module>
----> 1 np.r_[np.array([0,0]),np.zeros((2,1))]
D:\anaconda\lib\site-packages\numpy\lib\index_tricks.py in __getitem__(self, key)
402 objs[k] = objs[k].astype(final_dtype)
403
--> 404 res = self.concatenate(tuple(objs), axis=axis)
405
406 if matrix:
ValueError: all the input arrays must have same number of dimensions
np. c_[ np. array( [ 0 , 0 ] ) , np. zeros( ( 2 , 1 ) ) ]
array([[0., 0.],
[0., 0.]])
target = np. arange( 8 ) . reshape( 2 , 4 )
target
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
target. reshape( ( 4 , 2 ) , order = 'c' )
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
target. reshape( ( 4 , 2 ) , order = 'f' )
array([[0, 2],
[4, 6],
[1, 3],
[5, 7]])
target. reshape( ( 4 , - 1 ) )
target
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
target = np. ones( ( 3 , 1 ) )
target
array([[1.],
[1.],
[1.]])
target. reshape( - 1 )
array([1., 1., 1.])
数组的切片与索引
import numpy as np
target = np. arange( 9 ) . reshape( 3 , 3 )
target
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
target[ : - 1 , [ 0 , 2 ] ]
array([[0, 2],
[3, 5]])
target[ np. ix_( [ True , False , True ] , [ True , False , True ] ) ]
array([[0, 2],
[6, 8]])
target[ np. ix_( [ 1 , 2 ] , [ True , False , True ] ) ]
array([[3, 5],
[6, 8]])
new = target. reshape( - 1 )
new
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
new[ new% 2 == 0 ]
array([0, 2, 4, 6, 8])
常用函数
a = np. array( [ - 1 , 1 , - 1 , 0 ] )
np. where( a> 0 , a, 5 )
array([5, 1, 5, 5])
a = np. array( [ - 2 , - 5 , 0 , 1 , 3 , - 1 ] )
np. nonzero( a)
(array([0, 1, 3, 4, 5], dtype=int64),)
a. argmax( )
4
a. argmin( )
1
a = np. array( [ 0 , 1 ] )
a. any ( )
True
a. all ( )
False
a = np. array( [ 1 , 5 , 8 ] )
a. cumprod( )
array([ 1, 5, 40], dtype=int32)
a. cumsum( )
array([ 1, 6, 14], dtype=int32)
np. diff( a)
array([4, 3])
广播机制
res = 3 * np. ones( ( 2 , 2 ) ) + 1
res
import numpy as np
res = 3 * np. ones( ( 2 , 2 ) ) + 1
res
array([[4., 4.],
[4., 4.]])
np. ones( ( 2 , 2 ) )
array([[1., 1.],
[1., 1.]])
res = 1 / res
res
array([[0.25, 0.25],
[0.25, 0.25]])
import numpy as np
res = np. ones( ( 3 , 2 ) )
res
array([[1., 1.],
[1., 1.],
[1., 1.]])
res* np. array( [ [ 2 , 3 ] ] )
array([[2., 3.],
[2., 3.],
[2., 3.]])
res* np. array( [ [ 2 ] , [ 3 ] , [ 4 ] ] )
array([[2., 2.],
[3., 3.],
[4., 4.]])
res* np. array( [ [ 2 ] ] )
array([[2., 2.],
[2., 2.],
[2., 2.]])
向量与矩阵的运算
a = np. array( [ 1 , 2 , 3 ] )
b = np. array( [ 1 , 3 , 5 ] )
a. dot( b)
22
martix_target = np. arange( 4 ) . reshape( - 1 , 2 )
martix_target
array([[0, 1],
[2, 3]])
np. arange( 4 )
array([0, 1, 2, 3])
np. arange( 4 ) . reshape( - 1 , 2 )
array([[0, 1],
[2, 3]])
np. linalg. norm( martix_target, 'fro' )
3.7416573867739413
np. linalg. norm( martix_target, np. inf)
5.0
a = np. arange( 4 ) . reshape( - 1 , 2 )
a
array([[0, 1],
[2, 3]])
b = np. arange( - 4 , 0 ) . reshape( - 1 , 2 )
b
array([[-4, -3],
[-2, -1]])
a@b
array([[ -2, -1],
[-14, -9]])