*Please*, **always** provide the *full* error and traceback inside a code block. Otherwise, the error could have occured due to not only any of the lines of code you’ve shown, but also other first or third-party code that isn’t, and we are left to blindly guess at where it might be.

Fortunately, since you helpfully provided your full code itself in a code block, I was able to figure out what is going on.

Testing the code, there are three direct issues here, plus one side one.

First, the immediate error is my fault for not actually running the code I provided—the `rng.choice()`

call needs have its arguments passed by keyword, since the ones I used are not the first three. I.e. you should have

```
rng.choice(items.flatten(), size=k, p=weights.flatten())
```

Second, your weights are probabilities and need to sum to one, so simply divide the weights array by its sum before passing it:

```
rng.choice(items.flatten(), size=k, p=(weights / sum(weights)).flatten())
```

Finally, it looks like your different categories are not the same length, so they aren’t actually a 2D array (or matrix) at all (per the original question). Therefore, Numpy treats them as a 1D array of list objects, which is not what you want. However, since you don’t actually care about the arrays dimensions in the final output, you can solve this by flattening one step earlier, when you put the weights and items into the arrays.

To do this, use the unpacking operator (`*`

) to create flat lists of `weights`

and `items`

instead of nested ones, which is most convenient to do when creating the arrays:

```
weights=np.array([*weight_1, *weight_2, *weight_3])
items=np.array([*cat_1, *cat_2, *cat_3])
```

Then, you can omit the flattening calls in `rng.choice()`

, though they don’t hurt:

```
rng.choice(items, size=k, p=weights / sum(weights))
```

If you do have an actual matrix or 2D array (or an array of any dimension), which is what the original question was asking, you don’t need to flatten beforehand, and you may or may not want to flatten before outputting, depending on what you want your output to look like.

Also, FYI, you have an extra `all_cat`

and `cat_weight`

that don’t appear to be used anywhere, at least in the given code.