In [1]:
import numpy as np
import matplotlib.pyplot as plt

Part I

In [2]:
# Define number of rows and number of columns
n_rows = 100
n_columns = 1000
In [3]:
mu = np.arange(n_rows)*1e-2
sigma = np.arange(n_columns)*1e-3
In [4]:
print(mu.shape)
print(sigma.shape)
(100,)
(1000,)
In [5]:
np.random.seed(10)
a = np.random.randn(n_rows,n_columns)
In [6]:
print(a.shape)
(100, 1000)
In [7]:
# Shift elements of a so that the mean of row i is mu_i.
b = a+mu[...,np.newaxis]
In [8]:
print(b.shape)
(100, 1000)
In [9]:
# Add the sigma_j part.
x = sigma[np.newaxis,...]*a+mu[...,np.newaxis]
In [10]:
print(x.shape)
(100, 1000)
In [11]:
print(x)
[[ 0.00000000e+00  7.15278974e-04 -3.09080058e-03 ...  1.43637531e+00
  -3.89437428e-01  6.41808174e-01]
 [ 1.00000000e-02  9.85766641e-03  9.72116008e-03 ...  3.81116684e-01
   6.21021703e-01  1.71856479e+00]
 [ 2.00000000e-02  2.06759350e-02  1.98723332e-02 ...  2.02969280e-01
   8.64315556e-01  1.38244408e+00]
 ...
 [ 9.70000000e-01  9.70728859e-01  9.68958944e-01 ...  7.81332403e-01
   1.45628193e+00  1.75980748e-01]
 [ 9.80000000e-01  9.79969054e-01  9.82354298e-01 ... -1.80437129e-01
   6.87742548e-01  4.81393381e-01]
 [ 9.90000000e-01  9.89187947e-01  9.94305022e-01 ...  1.20669385e+00
   2.58779146e-01  1.39365318e+00]]

Part II

In [12]:
mean_per_column = x.mean(axis=0)
print(mean_per_column.shape)
print(mean_per_column[:10])
(1000,)
[0.495      0.49505392 0.49545773 0.49469218 0.49567979 0.49503831
 0.49528705 0.4943314  0.4952664  0.49381432]
In [13]:
plt.figure()
plt.hist(mean_per_column, bins=30)
plt.xlabel('mean per column')
plt.ylabel('frequency')
plt.tight_layout()

The distribution peaks at 0.5.

In [14]:
std_per_row = x.std(axis=1)
print(std_per_row.shape)
print(std_per_row[:10])
(100,)
[0.53137772 0.56604899 0.58745518 0.58051712 0.57414026 0.54794615
 0.61715369 0.57992739 0.5843169  0.55816371]
In [15]:
plt.figure()
plt.hist(std_per_row, bins=30)
plt.xlabel('std per row')
plt.ylabel('frequency')
plt.tight_layout()
In [16]:
# Indices where condition is true.
w = np.where(x>0.7)
print(w)
(array([ 0,  0,  0, ..., 99, 99, 99]), array([310, 369, 435, ..., 996, 997, 999]))
In [17]:
print(w[0]) # Indices for rows.
print(w[0].shape)
[ 0  0  0 ... 99 99 99]
(35760,)
In [18]:
print(w[1]) # Indices for columns.
print(w[1].shape)
[310 369 435 ... 996 997 999]
(35760,)
In [19]:
# For each row, find all elements of the row that are above 0.7 and calculate their mean.
for r in range(n_rows):
    wr = np.where(w[0]==r)
    print(x[w[0][wr],w[1][wr]].mean())
1.0634202285173913
1.0612036160776497
1.0611085614328764
1.0696556389718959
1.0485462749187504
1.105210150312609
1.079259730291291
1.1387198374480982
1.1156180553083022
1.0655795419003529
1.09840969435757
1.1330472009017871
1.2021540014689567
1.0785261757676714
1.1218486975408308
1.0767269953897807
1.1211699755111124
1.0485242021257675
1.1507315105494833
1.0892338237633201
1.1480919377682657
1.1314561433465242
1.0983048904418387
1.1134239657534268
1.080357935781241
1.1080998306915915
1.128379080255825
1.1215290929486275
1.201486255265855
1.0977814438514548
1.08350563621506
1.0922658055355348
1.1859180677840493
1.1674812609336078
1.2130848264852607
1.0982145670500407
1.1288493409223028
1.152730587567534
1.1231273409116345
1.1283772997987258
1.1577190209261161
1.142842947989638
1.103336883733116
1.1655234628092803
1.122855391015031
1.160094032601682
1.1382681276173454
1.1507897764135209
1.1331854238227406
1.0975429749033014
1.1252967774459302
1.1148336322173646
1.0851695956256862
1.126948412684076
1.1478552799150492
1.117060113010593
1.1433467698561086
1.1366074622021127
1.103545478876309
1.13800372932182
1.139258545326751
1.1585952350932098
1.1604372851064164
1.0606054409070134
1.0996368310627802
1.1297423090587375
1.124428735235742
1.1213221731664411
1.1329609280144772
1.1015870262235958
1.1152171516332434
1.0697103392219998
1.102132831577136
1.1082171993014653
1.0982165292213206
1.1043811396841177
1.0789396567760474
1.1017441720645644
1.087034256828378
1.0729437488055698
1.0986849084047214
1.114330100383397
1.1175020880725237
1.106719415953381
1.091739178908474
1.1108950515948277
1.1341321698671236
1.1230853148402584
1.1309604547104695
1.153667413590265
1.157157078358886
1.150839874827217
1.155837134155136
1.1782602008051082
1.1634637986856944
1.2001239401073607
1.1762387365372327
1.2195778465753506
1.2032324762674946
1.211253327333453
In [ ]: