numpy及scipy的使用

numpy的使用

把list A转换为numpy 矩阵

np.array(A)
np.array(A, 'int32')

numpy加载txt文件里面的矩阵

matrix = np.loadtxt(txt_name, dtype='i', delimiter=',')

将nparray里面每个元素转换为int型

nparray.astype(int)	

array[::2] 的用法

array.shape = (2*n,)
array.shape[::2]    #表示第奇数个元素组成的向量
array.shape[1::2]    #表示第偶数个元素组成的向量

numpy删除固定行,或者固定列元素

det.shape => (4,5)
np.delete(det, (row_idx), axis=0)    #axis=0表示删除行,axis=1表示删除列
det.shape => (3,5)

维度扩展

np.expand_dims(array, axis=0)    #将(96,96) 扩展成为(1,96,96)

通道转换——transpose

array.shape = (batchsize, w, h ,channels)
array_new=np.transpose(array, (0,3,1,2)) 

numpy array与常数比较大小

array > threshold    #将得到一个array, 它的size与原始array相同,元素由0,1构成

求和

np.sum(array)    #将array中所有元素相加

连接

np.concatenate
list = range(0,100)
np.concatenate((t[:3], t[10:]), axis=0)

numpy.clip(a, a_min, a_max, out=None) #过滤大于或小于某个区间的值

exm = np.array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9],
       [ 5,  6,  7,  8,  9, 10, 11, 12, 13, 14]])
np.clip(exm, 3, 11)
output:
array([[ 3,  3,  3,  3,  4,  5,  6,  7,  8,  9],
       [ 5,  6,  7,  8,  9, 10, 11, 11, 11, 11]])    #将小于区间值设置为区间低值,大于的设置为区间高值

scipy的使用

ndimage.zoom

array.shape  = batchsize, channels, w, h
array_new = scipy.ndimage.zoom(array, (1,1,resizescale, resizescale))
原文地址:https://www.cnblogs.com/fariver/p/6511888.html