介绍了获取、设置、调整 NumPy 数组数值的方法与工具,包括取值操作(如arr[2, 1])、切片操作(如 arr[:, 1:5])、掩码操作(如 arr[arr > 0])、花哨的索引操作(如 arr[0, [1, 5]]),以及组合操作(如 arr[:, [1, 5]])。
import numpy as np
np. random. seed( 0 )
x3 = np. random. randint( 10 , size= ( 3 , 4 , 5 ) )
print ( x3)
print ( type ( x3) )
print ( x3. ndim)
print ( x3. shape)
print ( x3. size)
[[[5 0 3 3 7]
[9 3 5 2 4]
[7 6 8 8 1]
[6 7 7 8 1]]
[[5 9 8 9 4]
[3 0 3 5 0]
[2 3 8 1 3]
[3 3 7 0 1]]
[[9 9 0 4 7]
[3 2 7 2 0]
[0 4 5 5 6]
[8 4 1 4 9]]]
<class 'numpy.ndarray'>
3
(3, 4, 5)
60
x = np. arange( 9 ) . reshape( ( 3 , 3 ) )
print ( 2 ** x)
[[ 1 2 4]
[ 8 16 32]
[ 64 128 256]]
y = np. empty( ( 3 , 3 ) )
np. multiply( x, 10 , out= y)
print ( y)
[[ 0. 10. 20.]
[30. 40. 50.]
[60. 70. 80.]]
xx = np. arange( 5 )
yy = np. zeros( 10 )
np. power( 2 , xx, out= yy[ : : 2 ] )
print ( yy)
[ 1. 0. 2. 0. 4. 0. 8. 0. 16. 0.]
x = np. arange( 1 , 5 )
print ( np. add. reduce ( x) )
print ( np. add. accumulate( x) )
10
[ 1 3 6 10]
x = np. arange( 1 , 6 )
np. multiply. outer( x, x)
array([[ 1, 2, 3, 4, 5],
[ 2, 4, 6, 8, 10],
[ 3, 6, 9, 12, 15],
[ 4, 8, 12, 16, 20],
[ 5, 10, 15, 20, 25]])
big_array = np. random. rand( 1000000 )
% timeit sum ( big_array)
% timeit np. sum ( big_array)
142 ms ± 4.36 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
977 µs ± 118 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
M = np. random. random( ( 3 , 4 ) )
print ( M)
print ( np. min ( M, axis= 0 ) )
print ( M. min ( axis= 0 ) )
[[0.91144496 0.1512867 0.42149518 0.76388113]
[0.813865 0.90006791 0.71559529 0.26995452]
[0.97107599 0.99902256 0.97262125 0.78889139]]
[0.813865 0.1512867 0.42149518 0.26995452]
[0.813865 0.1512867 0.42149518 0.26995452]
a = np. array( [ 0 , 1 , 2 ] )
print ( a)
b = np. ones( ( 3 , 3 ) )
print ( b)
print ( a + b)
[0 1 2]
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
[[1. 2. 3.]
[1. 2. 3.]
[1. 2. 3.]]
a= np. array( [ 1 , 2 , 3 , 4 , 5 ] )
aa= a[ np. newaxis, : ]
aa2 = a[ : , np. newaxis]
print ( aa. shape)
print ( aa)
print ( aa2)
(1, 5)
[[1 2 3 4 5]]
[[1]
[2]
[3]
[4]
[5]]
print ( aa + aa2)
[[ 2 3 4 5 6]
[ 3 4 5 6 7]
[ 4 5 6 7 8]
[ 5 6 7 8 9]
[ 6 7 8 9 10]]
x = np. linspace( 0 , 5 , 50 )
y = np. linspace( 0 , 5 , 50 ) [ : , np. newaxis]
z = np. sin( x) ** 10 + np. cos( 10 + y* x) * np. cos( x)
% matplotlib inline
import matplotlib. pyplot as plt
plt. imshow( z, origin= 'lower' , extent= [ 0 , 5 , 0 , 5 ] , cmap= 'viridis' )
plt. colorbar( ) ;
import numpy as np
import pandas as pd
rng = np. random. RandomState( 0 )
x = rng. randint( 10 , size= ( 3 , 4 ) )
print ( x)
[[5 0 3 3]
[7 9 3 5]
[2 4 7 6]]
print ( x > 3 )
print ( np. sum ( x > 3 ) )
print ( np. sum ( x > 3 , axis= 0 ) )
print ( np. sum ( ( x > 1 ) & ( x < 4 ) ) )
[[ True False False False]
[ True True False True]
[False True True True]]
7
[2 2 1 2]
4
print ( x[ x > 5 ] )
yanma = ( x > 2 ) & ( x < 5 )
print ( x[ yanma] )
[7 9 7 6]
[3 3 3 4]
x = np. array( [ 1 , 2 , 3 , 4 , 5 ] )
ind = np. array( [ [ 1 , 2 ] , [ 3 , 4 ] ] )
print ( x[ ind] )
[[2 3]
[4 5]]
X = np. arange( 12 ) . reshape( 3 , 4 )
print ( X)
row = np. array( [ 0 , 1 , 2 ] )
col = np. array( [ 2 , 1 , 3 ] )
print ( X[ row, col] )
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[ 2 5 11]
print ( X[ row[ : , np. newaxis] , col] )
[[ 2 1 3]
[ 6 5 7]
[10 9 11]]
print ( X)
mask = np. array( [ 0 , 1 , 0 , 1 ] , dtype= 'bool' )
print ( X[ row[ : , np. newaxis] , mask] )
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[ 1 3]
[ 5 7]
[ 9 11]]
x = np. zeros( 10 )
print ( x)
i = [ 1 , 2 , 2 , 3 , 3 , 3 ]
x[ i] += 1
print ( x)
x = np. zeros( 10 )
np. add. at( x, i, 1 )
print ( x)
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 0. 0. 0. 0. 0. 0.]
[0. 1. 2. 3. 0. 0. 0. 0. 0. 0.]