'Initializing and updating numpy vstack and numpy hstack inside a for loop
I need to create and save numpy arrays of all the models stacked vertically together as well as stacked horizontally together for the columns of actual and predicted values from my model. How can I do this inside a for loop which loops through each model? My code below does not seem to work and it does not stack them the way I want them as it seems to overwrite the previous model each time it loops! The created and saved np arrays (actual_values_all_models_vstacked, pred_values_all_models_vstacked,actual_values_hstacked, pred_values_hstacked) should be of the size [8032500, 1], [8032500, 1], [1147500, 7], [1147500, 7] accordingly.
nmodel, nx, ny, nz = 7, 150, 150, 51
layer_cells = nx*ny
model_cells = nx*ny*nz
all_data_cells_num = nx*ny*nz*nmodel
total_cells = np.arange(all_data_cells_num)
def get_model_index(model_list,total_cells,model_cells ):
model_index_array = np.array([])
for i in model_list:
model_index = total_cells[(i-1)*model_cells:model_cells*i]
model_index_array = np.append(model_index_array, model_index)
return model_index_array
model_runs = ['RUN_01', 'RUN_02', 'RUN_03', 'RUN_04', 'RUN_05', 'RUN_06', 'RUN_07']
model_list = [1, 2, 3, 4, 5, 6, 7]
# Initializing:
actual_values_all_models_vstacked = np.zeros([8032500, 1]) # 7*150*150*51 = 8032500
pred_values_all_models_vstacked = np.zeros([8032500, 1]) # 7*150*150*51 = 8032500
actual_values_hstacked = np.zeros([1147500, 7]) # 150*150*51 = 1147500
pred_values_hstacked = np.zeros([1147500, 7]) # 150*150*51 = 1147500
for model_index in model_list:
run_name = model_runs[model_index-1]
blind_model = [model_index]
blind_model_idx = get_model_index(blind_model,total_cells,model_cells )
blind_model_idx = np.array([int(i) for i in blind_model_idx])
actual = np.arange(1147500)
pred = np.arange(1147500)
actual_pred = np.hstack((actual, pred))
actual_values_all_models_vstacked = np.vstack(actual_pred[:, 1]) # assuming col 1 is actual
pred_values_all_models_vstacked = np.vstack(actual_pred[:, 2]) # assuming col 2 is pred vals
actual_values_hstacked = np.hstack(actual_pred[:, 1])
pred_values_hstacked = np.hstack(actual_pred[:, 2])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
