Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python NumPy中高效的每单元标准化_Python_Performance_Numpy - Fatal编程技术网

Python NumPy中高效的每单元标准化

Python NumPy中高效的每单元标准化,python,performance,numpy,Python,Performance,Numpy,我正在使用以下代码对所有用户(第一维度)的训练/测试矩阵中的每个单元进行标准化。这当然是非常低效的,但我想确保这个想法有效。我如何使用NumPy的优化方法来实现它 X_dims = X.shape channels = 14 # not all columns as binary variables should stay untouched mu_cell = np.zeros(shape=(channels, X_dims[2], X_dims

我正在使用以下代码对所有用户(第一维度)的训练/测试矩阵中的每个单元进行标准化。这当然是非常低效的,但我想确保这个想法有效。我如何使用NumPy的优化方法来实现它

   X_dims      = X.shape   
   channels    = 14 # not all columns as binary variables should stay untouched
   mu_cell     = np.zeros(shape=(channels, X_dims[2], X_dims[3]))
   sigma_cell  = np.zeros(shape=(channels, X_dims[2], X_dims[3]))

   for j in range(channels):
      for k in range(X_dims[2]):
         for l in range(X_dims[3]):
            mu_cell[j,k,l]    = np.mean(X_train[:,j,k,l])
            sigma_cell[j,k,l] = np.std(X_train[:,j,k,l])


   def standardizeCellWise(matrix):
      for i in range(matrix.shape[0]):
         for j in range(channels):
            for k in range(matrix.shape[2]):
               for l in range(matrix.shape[3]):
                  matrix[i, j, k, l] -= mu_cell[j,k,l]
                  matrix[i, j, k, l] = matrix[i, j, k, l] / sigma_cell[j,k,l] if sigma_cell[j,k,l] != 0 else 0      
      return matrix

   X_train = standardizeCellWise(X_train)
   X_test  = standardizeCellWise(X_test)

如图所示,
mu
sigma
数组可以用numpythonic方式计算-

import numpy as np

mu_cell = X_train[:,0:channels,:,:].mean(0)
sigma_cell = X_train[:,0:channels,:,:].std(0)
接下来,如果您知道在输入
矩阵中没有任何无限数或
NaN
,则可以使用此矢量化方法来标准化单元格-

def standardizeCellWise(matrix,mu_cell,sigma_cell):
    matrix_cut = matrix[:,0:channels,:,:]
    matrix_cut = (matrix_cut - mu_cell[None,:])/sigma_cell[None,:]
    mask = ~np.isfinite(matrix_cut)
    matrix_cut[mask] = 0
    matrix[:,0:channels,:,:] = matrix_cut
    return matrix
对于一般的输入矩阵情况,您只需要像这样更改
掩码的计算-

mask = np.tile(sigma_cell[None,:]==0,[matrix.shape[0],1,1,1])

你不能做平均值(X列,轴=0)