NumPy

7096 ワード

import numpy as np
type(np.array) = ndarray = n-dimensional array
np.zeros((2, 2)) : 2x2 matrix, all elements = 0
np.ones((2, 2)) : 2x2 matrix, all elements = 1
np.full((2, 2), 1.2) : 2x2 matrix, all elements = 1.2
np.eye(3) : 3x3 identity matrix
np.tri(3) : 3x3 matrix, filled with 1 in triangle shape.
np.Empty(3,3):アレイを迅速に生成しますが、初期化する必要はありません.メモリの任意の値を取得できます.
np.zeros like(a):配列と同じ形状のzeros配列を作成してください
np.ones_like(a)
np.full_like(a, 5)
np.arange(start, end, step)
np.linspace(0,1,5):0から1の間の均一な5等分
np.logspace(0,1,5):平均log値
np.random.random(3,3):ランダム値を使用して3 x 3マトリクスを作成する
np.random.randint(0,10,(3,3):0から10
np.random.normal(0,10,(3,3):正規分布値
np.random.rand(3,3):均一分布値3 x 3マトリクス
np.random.randn(3,3):標準正規分布値3 x 3行列
np.eye(3,dtype=int)、intマトリクス、float、boolなど.
date = np.array('2020-01-01', dtype=np.datetime64)
-> array('2020-01-01', dtype='datetime64[D]')
date + np.arange(12)
-> array(['2020-01-01', '2020-01-02', '2020-01-03', '2020-01-04',
'2020-01-05', '2020-01-06', '2020-01-07', '2020-01-08',
'2020-01-09', '2020-01-10', '2020-01-11', '2020-01-12'],
dtype='datetime64[D]')
def array_info(array):
  print(array)
  print('ndim: ', array.ndim) # dimension
  print('shape: ', array.shape)
  print('dtype: ', array.dtype)
  print('size: ', array.size) # total elements 갯수
  print('itemsize: ', array.itemsize) # single element bytes
  print('nbytes: ', array.nbytes) # total elements bytes
  print('strides: ', array.strides) # 다음 차원으로 넘어가기 위해 필요한 bytes
  
array_info(a2)
-> 
[[1 2 3]
 [4 5 6]
 [7 8 9]]
ndim:  2
shape:  (3, 3)
dtype:  int64
size:  9
itemsize:  8
nbytes:  72
strides:  (24, 8)
-------boolean indexing------
a1 = [7, 2, 3, 4, 5]
b1 = [False, True, True, False, True]
print(a1[b1])
-> [2 3 5] # true 만 가져옴
np.Insert(array,index,number):指定された位置(index)に指定された値(number)を挿入し、ソース配列Xを変更する
np.Insert(a 2、0、100、axis=1):2 D以降のバージョンで値を入力する軸を指定します.
np.delete(a1, 2)
np.delete(a2, 1, axis=1)