Reputation: 23
I am using Python 3.7 with numpy 1.18.3. I have an array set up which contains 4 rows. I periodically need to add a new column. Each time I add a new entry to the array it is a defined as 4,1 array (t).
I occasionally check to see if the bin will accommodate an allocation (d_entry below) and if it will I have to subtract the allocation from the bin and update the column.
import numpy as np
t = 4x1 numpy array [[ 150], [50000], [30000], [20000]]
d_entry = 4x1 numpy array [[2.6000e+01], [1.1638e+04], [5.0000e+00], [9.8290e+03]]
new_bin = np.subtract(t, d_entry)
bin_space = np.append(bin_space, new_bin, axis=1)
So for example, with two bin entries, my bin_space ends up looking like:
[[ 107. 124.], [27798. 38362.], [29992. 29995.], [ 9401. 10171.]]
The bin space entries (columns) occasionally need to be updated, as explained, replacing entries with updated 4 x 1 columns.
To do this I reference the bin_space column like this:
space_left = np.array(bin_space[:,j])
(which PyCharm says is now a (4,) - not 4,1
bin_space[:,j] = np.subtract(space_left, d_entry)**
I then end up with the error. I am not sure how to may the array extract the same type.
Traceback (most recent call last): File "D:/Development/oasg-apps/src/mcl/python/mcl_bin_packing.py", line 398, in main() File "D:/Development/oasg-apps/src/mcl/python/mcl_bin_packing.py", line 247, in main n_bins, bin_for_item, bin_space = allocate_ffd(d_vectors, t_resource, num_metrics_types) File "D:/Development/oasg-apps/src/mcl/python/mcl_bin_packing.py", line 135, in allocate_ffd bin_space[:,j] = np.subtract(space_left, d_entry) ValueError: could not broadcast input array from shape (4,1) into shape (4)
This reproduces the issue, but with "could not broadcast input array from shape (2,1) into shape (2)":
import numpy as np
bin_space = np.array([0, 0])
bin_space = np.reshape(bin_space, (2,1))
# Shape our target resources into a numpy array.
target_resource = np.array([150, 50000])
target_resource = np.reshape(target_resource,(2,1))
new_resource = np.array([10, 200])
new_resource = np.reshape(new_resource,(2,1))
bin_space = np.append(bin_space, target_resource, axis=1)
bin_space = np.append(bin_space, target_resource, axis=1)
bin_space[:,1] = new_resource
Any help appreciated.
Thanks,
Clive
Upvotes: 0
Views: 3179
Reputation: 998
Either:
bin_space[:,1] = new_resource.T
or:
bin_space[:,1] = new_resource.flatten()
Upvotes: 1