新建數(shù)組
a=[]
for i in xxx:
a.append(i[0:13])
多維數(shù)組 b = np.array([(1.5,2,3), (4,5,6)])
指定類型 c = np.array( [ [1,2], [3,4] ], dtype=complex )
生成數(shù)組并賦值為特殊值 ones:全1 zeros:全0 empty:隨機數(shù)衬吆,取決于內(nèi)存情況京革,如果是二維的笛坦,即寫成np.zeros( (3,4) ,dtype=np.int16 ) 還可以指定數(shù)據(jù)類型晦嵌。
生成均勻分布的array:
arange(最小值,最大值荸实,步長)(左閉右開)
linspace(最小值匀们,最大值缴淋,元素數(shù)量)
指數(shù)分布 logspace
對數(shù)組中每個元素進行處理准给,比如 a=arange(5) 2 可以寫為
a = [1,2,3,4,5]
b = list(map(lambda x:x2,a))
數(shù)組的交/并集
list 可以直接相加
水平組合 np.hstack((a,b ))
垂直組合 np.vstack((a,b)) np.concatenate((a,b),axis=0)
沿著縱軸方向組合 np.dstack((a,b)) 參考鏈接
數(shù)組拼接 np.append(x,y)
合并 np.concatenate((a,b), axis = None)
維度轉(zhuǎn)換
reshape 是把一個已經(jīng)存在了的ndarray 轉(zhuǎn)換為另一個shape, 即包含了原來所有的數(shù)組 ndarray = np.arange(0,12).reshape((3,4))
(注意:新的形狀必須保持size相同)
條件篩選
np.where( (arr == 3) | (arr == 1) )
a=np.where((x>5)&(x<10))
print(x[a])
idx=np.where(ne>100)
idx=np.array(idx).reshape(np.size(idx))
print(time[idx])
根據(jù)時間挑選最接近某個時刻的下標:
import pyspedas
arr=np.abs(MS_Bw_time-pyspedas.time_double(tt2))
idx12 = np.where(arr == np.amin(arr))
where篩選出來的index是tuple類型泄朴,把它轉(zhuǎn)化到array類型:
idx=[]
for item in index:
idx.extend(item)
def find_nearest(array, value):
array = np.asarray(array)
idx = (np.abs(array - value)).argmin()
return array[idx]
def find_nearest2(array,values):
indices = np.abs(np.subtract.outer(array, values)).argmin(0)
return indices
NAN value的處理
把nan 項去除 或 設(shè)為0
event_l_shell = event_l_shell[~np.isnan(event_l_shell)]
event_l_shell[np.isnan(event_l_shell)]=0
把NAN 去掉 .dropna()
把NAN替換成某個值 A[np.isnan(A)] = 0 或者.fillna(0)
bad_indices = np.isnan(x) | np.isnan(y)
good_indices = ~bad_indices
good_x = x[good_indices]
good_y = y[good_indices]
生成全是nan 的數(shù)組: np.full([3,2], np.nan)
空數(shù)組的判斷 any(array) == True/False