2

My question is simple: what is the most efficient (preferred) method in Python to create an array and fill it with values column by column?

First thing that comes into my mind is just to create array empty array (or array filled with zeros) and step by step fill it based on column number.

import random
import numpy as np

values = np.zeros([10, 20])
# or values = np.empty([10,20])
for i in range(len(values.T)):
    if i == 0:
        values[:,i] = 1
    elif i < 10:
        values[:,i] = random.sample(range(1, 100), 10)
    else:
        values[:,i] =  values[:,i] - values[:,i - 1] * i 

EDIT: What I am filling the columns with is not as important here as the most efficent way to do it.

5
  • Well that obviously depends how you want to fill the matrix. Here you can do it with less numpy calls. Commented Jul 19, 2017 at 12:37
  • I want to fill it column by column since some calculations, as in the example, depend on values from previous column. Commented Jul 19, 2017 at 12:39
  • 1
    But here these do not really depend: the first row consists out of ones. The next 9 out of 10s and finally it will start alternating between -10 and 10. Commented Jul 19, 2017 at 12:40
  • About the performance: I guess what you are doing is quite fine. Preallocate the correct size and fill it. Everything else like creating the array from multi-dimensional lists or appending rows to an existing numpy array is slower. Commented Jul 19, 2017 at 12:42
  • Ok, that is what I was looking for, since I am concerned more about proper way to do it rather than the calculations itself. I admit my example is stupid and do not fully reflect the complexity of calculations, since they will be based on other array. Commented Jul 19, 2017 at 12:45

1 Answer 1

1

You could use resize:

values = np.zeros([20])

for i in range(len(values)):
    if i == 0:
        values[i] = 1
    elif i < 10:
        values[i] = random.sample(range(1, 100), 1)
    else:
        values[i] = values[i] - values[i - 1] * i 

values = np.resize(values, [10, 20])

This is efficient since it mostly works on small arrays, defining each column, and then performs only one operation in order to create a larger array.

Sign up to request clarification or add additional context in comments.

1 Comment

You said the values vary column by column, not row by row?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.