0

I am trying to generate a 5 column array in Python where the first number in each row remains fixed at .2, but the next 4 numbers in each row vary and each row of numbers sum to 1. So something like

[.2, .2, .2, .2, .2]
[.2, .3, .1, .2, .2]
[.2, .2,  0, .6,  0]
[.2, .5,  0, .1, .2]

Would this be possible?

jemmet
  • 1

2 Answers2

0

A simple answer is this:

array = [0.2]
rest = [ran.random() for i in range(4)]
s = sum(rest)
rest = [ i/s * 0.8 for i in rest ]
array.extend(rest)
fynsta
  • 348
  • 3
  • 10
0

If you're using NumPy, you can use the normalization technique here to make rows of 4 that sum to .8, then add a column of .2 at the start.

import numpy as np

n = 0.2
a = np.random.rand(4, 4)

# Sum rows and divide by target
s = a.sum(1) / (1-n)

# Normalize rows
normalized = a / s.reshape(-1, 1)
print(normalized.sum(1))  # -> [0.8 0.8 0.8 0.8]

# Add column of .2
first_col = np.repeat(n, a.shape[0])
final = np.concatenate((first_col.reshape(-1, 1), normalized), axis=1)
print(final.sum(1))  # -> [1. 1. 1. 1.]

Example values:

>>> print(a)
[[0.86134437 0.56626254 0.21527553 0.16657095]
 [0.01680889 0.4971182  0.61437178 0.77192482]
 [0.98655061 0.26207574 0.62670237 0.67427712]
 [0.83763804 0.41413746 0.16745744 0.02619564]]
>>> print(s)
[2.26181674 2.3752796  3.1870073  1.80678573]
>>> print(normalized)
[[0.3808197  0.25035739 0.09517815 0.07364476]
 [0.00707659 0.20928829 0.2586524  0.32498272]
 [0.30955392 0.08223255 0.1966429  0.21157062]
 [0.46360674 0.22921227 0.09268251 0.01449848]]
>>> print(final)
[[0.2        0.3808197  0.25035739 0.09517815 0.07364476]
 [0.2        0.00707659 0.20928829 0.2586524  0.32498272]
 [0.2        0.30955392 0.08223255 0.1966429  0.21157062]
 [0.2        0.46360674 0.22921227 0.09268251 0.01449848]]
wjandrea
  • 28,235
  • 9
  • 60
  • 81