Home > Mobile >  Pythonically adding a column to an innitially empty matrix in a loop
Pythonically adding a column to an innitially empty matrix in a loop

Time:10-08

I am newish to python and struggling to do this, despite the fact that it feels like a really basic thing to do.

here is my attempt

max_depth_vars = np.linspace(5, 1, 12)
n_mdvs         = len(max_depth_vars)
predictions    = np.array

for index, max_depth_var in zip(range(0,n_mdvs),max_depth_vars):
    # Instantiate model 
    rf = RandomForestRegressor(max_depth=max_depth_var)
    # Train the model on training data
    rf.fit(X_train, Y_train)

    predictions= np.column_stack((np.array(predictions), np.array(rf.predict(X_train))))

the array predictions starts as empty outside the loop then columns are added to it in the loop - or at least that's what I wanted. What I get is:

ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 1 and the array at index 1 has size 361

on the final line

How do you pythonically add columns to an initially empty matrix in a loop?

CodePudding user response:

To add to the comments, np.array is optimized to be fixed in size. Consecutive resizing of an array using concatenate, vstack, hstack, etc in a for loop is less readable and generally less efficient (therefore pointless). Stick to the standard and simple .append of a list, then make the array at the end:

predictions = list()
for index, max_depth_var in zip(range(0,n_mdvs),max_depth_vars):
    #your_code
    predictions.append(your_iteration_array)
predictions = np.array(predictions)
  • Related