0

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

5
  • 1
    Could please post the error traceback,also your code is messed up,try to fix it Commented Apr 30, 2020 at 14:20
  • I'm guessing you need to do array.flatten(), array.resize((-1, 1)), or something similar. I agree that you should correct the question. At SO it's best to give minimum working example. At this point, I'm not able to reproduce your code. Commented Apr 30, 2020 at 14:45
  • Code added to post which allows reproduction of the problem. Commented Apr 30, 2020 at 15:11
  • Ok, now try to make your example minimal. Commented Apr 30, 2020 at 15:14
  • More minimal done. Thanks. Commented Apr 30, 2020 at 15:21

1 Answer 1

1

Either:

bin_space[:,1] = new_resource.T

or:

bin_space[:,1] = new_resource.flatten()
Sign up to request clarification or add additional context in comments.

1 Comment

Hey thanks. The new_resource.flatten() seems to do the trick. Does this work by allowing numpy to cast to the shape of the array slice that I am referencing?

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.