python 3 numpy基本用法まとめ

26688 ワード

  numpy : pip install numpy

numpy配列生成方法の概要
In [4]:
import numpy as np

#            
data = [1,2,3,4,5]
x = np.array(data)
print(x)
print(x.dtype)   #      
print(type(x))
[1 2 3 4 5]
int32


In [6]:
import numpy as np

#            
data = [[1,2], [3,4], [5,6]]
x = np.array(data)
print(x)
print(x.dtype)
print(x.ndim)    #      
print(x.shape)   #           
print(type(x))
[[1 2]
 [3 4]
 [5 6]]
int32
2
(3, 2)


In [16]:
import numpy as np

#  zeros       4,    0     
x = np.zeros(4)
print(x)
#        ,      2,      3,    0   
x = np.zeros((2,3))
print(x)
#  ones        ,      2,      3,    1   
x = np.ones((2,3))
print(x)
#  empty        ,      3,      3,          
y = np.empty((3,3))
print(y)
#  arange      
a = np.arange(5)
print(a)
b = np.arange(1,5,2)
print(b)
[ 0.  0.  0.  0.]
[[ 0.  0.  0.]
 [ 0.  0.  0.]]
[[ 1.  1.  1.]
 [ 1.  1.  1.]]
[[  2.97907948e-317   2.69387831e-316   8.66647269e-317]
 [  2.48185956e-315   2.48185956e-315   2.48185909e-315]
 [  0.00000000e+000   0.00000000e+000   6.52072824e+091]]
[0 1 2 3 4]
[1 3]

reshapeの使い方:
In [2]:
import numpy as np

#    
a = np.arange(10).reshape(2,5)
print(a)
print("
") # a = np.arange(12).reshape(2,2,3) print(a)
[[0 1 2 3 4]
 [5 6 7 8 9]]


[[[ 0  1  2]
  [ 3  4  5]]

 [[ 6  7  8]
  [ 9 10 11]]]

基本演算:
配列の算術演算は要素による
In [9]:
import numpy as np

a = np.array([30,40,50,60])
b = np.arange(4)

print(a)
print(b)
print("
a-b:") # c = a - b print(c) print("
b**2:") #b print(b**2) # print("
a<45:") print(a<45)
[30 40 50 60]
[0 1 2 3]

a-b:
[30 39 48 57]

b**2:
[0 1 4 9]

a<45:
[ True  True False False]

numpyの*は要素で計算され、マトリクス乗算の場合dot関数を呼び出す必要があります.
In [12]:
import numpy as np

a = np.array([
    [1, 2],
    [3, 4]
])

b = np.array([
    [1,1],
    [0,4]
])

#*  
print("a*b:")
print(a*b)

#dot  
print("
dot(a,b):") print(np.dot(a,b))
a*b:
[[ 1  2]
 [ 0 16]]

dot(a,b):
[[ 1  9]
 [ 3 19]]

axisパラメータの使用方法:
In [19]:
import numpy as np

a = np.arange(12).reshape(2,2,3)
print(a)
print("       :")
print(a.sum(axis=0))
print("       :")
print(a.sum(axis=1))
print("       :")
print(a.sum(axis=2))

#               ,  2,3      ,              ,         ,     
[[[ 0  1  2]
  [ 3  4  5]]

 [[ 6  7  8]
  [ 9 10 11]]]
       :
[[ 6  8 10]
 [12 14 16]]
       :
[[ 3  5  7]
 [15 17 19]]
       :
[[ 3 12]
 [21 30]]

In [20]:
import numpy as np

a = np.arange(12).reshape(3,4)

print(a)

print("          :")
print(a.min(axis=0))
print("          :")
print(a.min(axis=1))
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
          :
[0 1 2 3]
          :
[0 4 8]

In [21]:
import numpy as np

a = np.arange(12).reshape(3,4)

print(a)

print("          :")
print(a.cumsum(axis=0))
print("          :")
print(a.cumsum(axis=1))
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
          :
[[ 0  1  2  3]
 [ 4  6  8 10]
 [12 15 18 21]]
          :
[[ 0  1  3  6]
 [ 4  9 15 22]
 [ 8 17 27 38]]

汎用関数の使用方法:
In [22]:
import numpy as np

a = np.arange(3)
print(a)

print(np.exp2(a)) #   2weidi
[0 1 2]
[ 1.  2.  4.]

転載先:https://www.cnblogs.com/jpfss/p/9604494.html