遍历numpy数组的每一行

时间:2019-09-30 05:10:00

标签: python numpy

我有以下输入,即2D数组。而且我想遍历每一行进行一些计算,但是当我打印输出时,它似乎只遍历数组的第一行。任何贡献将不胜感激。

2D numpy数组corr_ret

[[-6.33870149e-04  8.55011683e-04 -1.17983411e-03  7.91091491e-04
 2.62196784e-04 -4.43688931e-05 -6.45968376e-06 -1.12538630e-03
  2.15421763e-03 -3.16434832e-03  2.60707339e-03  1.50132673e-03
-1.26622898e-03 -6.60251486e-04  5.02396330e-04  2.11832581e-04
 -7.69597583e-04 -3.29756120e-03 -1.24210577e-03 -4.62660468e-04
-3.49262651e-04 -3.43464642e-04 -2.55611240e-04  5.54536845e-04
-2.01366145e-03 -2.87531971e-04 -5.34641032e-04  1.72644604e-03
 5.76858322e-04 -2.29174764e-03]
[ 4.70029370e-04  1.77020781e-03 -1.05050155e-03 -6.18841938e-04
  1.16485579e-03 -6.00092725e-05  1.05676386e-04 -1.48243613e-03
 3.72088647e-03 -1.75323439e-03  1.09075078e-03  9.89241541e-05
 -1.59512783e-03 -7.11380812e-04  1.73537083e-03  8.78393781e-04
 -9.29723278e-04 -1.53385019e-03  1.20444451e-04 -4.84107094e-04
 -7.83347205e-04  1.18777621e-03 -1.54999170e-03  8.35286745e-05
 -1.74472610e-03  9.83476064e-04 -8.82693488e-04  1.58099698e-03
  3.15406396e-03 -1.26301009e-04]
  [-1.06896577e-03  8.31379672e-04  1.54808513e-04 -1.35452237e-03
 -2.19101603e-04 -7.26696656e-04 -2.93275089e-04 -6.88305530e-04
  2.89300411e-03 -2.38832429e-03 -7.67452518e-04 -2.40866147e-04
 -2.11929402e-03 -7.45901508e-04  5.02921628e-04  1.77651468e-04
 -2.08574762e-03 -1.80218000e-03 -1.23287491e-03 -7.47521806e-04
  -7.80485878e-04  6.15345860e-04 -1.40945995e-03  8.74548883e-04
-2.78711058e-03  1.92856732e-03  5.73070388e-04  1.29301575e-03
  1.89158005e-03  8.65315240e-04]]

我的代码:

for row in corr_ret:
  seed = 1
  So = corr_ret[0] #the first cell of the row
  N = 30                           
  mu = corr_ret.mean()/N        
  sigma = corr_ret.std()   
  print(sigma)
  T = 1          

我的输出:

 [-6.33870149e-04  8.55011683e-04 -1.17983411e-03  7.91091491e-04
 2.62196784e-04 -4.43688931e-05 -6.45968376e-06 -1.12538630e-03
 2.15421763e-03 -3.16434832e-03  2.60707339e-03  1.50132673e-03
 -1.26622898e-03 -6.60251486e-04  5.02396330e-04  2.11832581e-04
-7.69597583e-04 -3.29756120e-03 -1.24210577e-03 -4.62660468e-04
-3.49262651e-04 -3.43464642e-04 -2.55611240e-04  5.54536845e-04
 -2.01366145e-03 -2.87531971e-04 -5.34641032e-04  1.72644604e-03
  5.76858322e-04 -2.29174764e-03]
-5.2605086588854405e-06
0.0013733187643799106
[-6.33870149e-04  8.55011683e-04 -1.17983411e-03  7.91091491e-04
 2.62196784e-04 -4.43688931e-05 -6.45968376e-06 -1.12538630e-03
  2.15421763e-03 -3.16434832e-03  2.60707339e-03  1.50132673e-03
 -1.26622898e-03 -6.60251486e-04  5.02396330e-04  2.11832581e-04
  -7.69597583e-04 -3.29756120e-03 -1.24210577e-03 -4.62660468e-04
 -3.49262651e-04 -3.43464642e-04 -2.55611240e-04  5.54536845e-04
 -2.01366145e-03 -2.87531971e-04 -5.34641032e-04  1.72644604e-03
 5.76858322e-04 -2.29174764e-03]
-5.2605086588854405e-06
 0.0013733187643799106
[-6.33870149e-04  8.55011683e-04 -1.17983411e-03  7.91091491e-04
 2.62196784e-04 -4.43688931e-05 -6.45968376e-06 -1.12538630e-03
 2.15421763e-03 -3.16434832e-03  2.60707339e-03  1.50132673e-03
 -1.26622898e-03 -6.60251486e-04  5.02396330e-04  2.11832581e-04
 -7.69597583e-04 -3.29756120e-03 -1.24210577e-03 -4.62660468e-04
 -3.49262651e-04 -3.43464642e-04 -2.55611240e-04  5.54536845e-04
 -2.01366145e-03 -2.87531971e-04 -5.34641032e-04  1.72644604e-03
 5.76858322e-04 -2.29174764e-03]
-5.2605086588854405e-06
0.0013733187643799106

2 个答案:

答案 0 :(得分:0)

要获取第一个单元格,请使用corr_ret数组(原始数组),该数组实际上返回该数组的第一行。 要获取每一行的第一个元素,请使用So = row [0]

for row in corr_ret:
    seed = 1
    So = row[0] #the first cell of the row
    N = 30                           
    mu = corr_ret.mean()/N        
    sigma = corr_ret.std()   
    print(sigma)
    T = 1   

答案 1 :(得分:0)

尽管这个问题已经得到了公认的答案,但是由于您正在使用numpy,所以我建议您看看axis关键字(另请参见here) 。它可用于许多numpy函数,并且可以帮助您沿特定轴获取特定值。就您而言,例如stdev_row = np.nanstd(corr_ret, axis=1)

import numpy as np

corr_ret = np.array([
    [-6.33870149e-04,8.55011683e-04,-1.17983411e-03,7.91091491e-04
    ,2.62196784e-04,-4.43688931e-05,-6.45968376e-06,-1.12538630e-03
    ,2.15421763e-03,-3.16434832e-03,2.60707339e-03,1.50132673e-03
    -1.26622898e-03,-6.60251486e-04,5.02396330e-04,2.11832581e-04
    ,-7.69597583e-04,-3.29756120e-03,-1.24210577e-03,-4.62660468e-04
    -3.49262651e-04,-3.43464642e-04,-2.55611240e-04,5.54536845e-04
    -2.01366145e-03,-2.87531971e-04,-5.34641032e-04,1.72644604e-03
    ,5.76858322e-04,-2.29174764e-03, np.nan, np.nan, np.nan],
    [4.70029370e-04,1.77020781e-03,-1.05050155e-03,-6.18841938e-04
    ,1.16485579e-03,-6.00092725e-05,1.05676386e-04,-1.48243613e-03
    ,3.72088647e-03,-1.75323439e-03,1.09075078e-03,9.89241541e-05
    ,-1.59512783e-03,-7.11380812e-04,1.73537083e-03,8.78393781e-04
    ,-9.29723278e-04,-1.53385019e-03,1.20444451e-04,-4.84107094e-04
    ,-7.83347205e-04,1.18777621e-03,-1.54999170e-03,8.35286745e-05
    ,-1.74472610e-03,9.83476064e-04,-8.82693488e-04,1.58099698e-03
    ,3.15406396e-03,-1.26301009e-04],
    [-1.06896577e-03,8.31379672e-04,1.54808513e-04,-1.35452237e-03
    ,-2.19101603e-04,-7.26696656e-04,-2.93275089e-04,-6.88305530e-04
    ,2.89300411e-03,-2.38832429e-03,-7.67452518e-04,-2.40866147e-04
    ,-2.11929402e-03,-7.45901508e-04,5.02921628e-04,1.77651468e-04
    ,-2.08574762e-03,-1.80218000e-03,-1.23287491e-03,-7.47521806e-04
    ,-7.80485878e-04,6.15345860e-04,-1.40945995e-03,8.74548883e-04
    -2.78711058e-03,1.92856732e-03,5.73070388e-04,1.29301575e-03
    ,1.89158005e-03,8.65315240e-04, np.nan]])

stdev_row = np.nanstd(corr_ret, axis=1) # axis=0 would be columns
# array([0.00134935, 0.00139341, 0.00129365])

请注意,您发布的输入不是2D数组;因此,我添加了NaNs,以使每行的长度相等(3x30)。