Monday, 14 November 2022

ValueError: operands could not be broadcast together with shape (313,) (283,)

I'm using the code from this post Source

But somehow I'm getting error as the title after passing my dataframe which has 6377 line. I'd tried with less number of data and it works but when my data is more than 380, it starts giving me that error. So I'm wondering is this due the numpy or the script limitation or is it something to do with the data i have.

`

def list_ancestors(df):

    ancestors = []
    for ar in trace_nodes(df):
        ancestors.append(np.c_[np.repeat(ar[:, 0], ar.shape[1]-1),
                               ar[:, 1:].flatten()])
    return pd.DataFrame(np.concatenate(ancestors),
                        columns=['descendant', 'ancestor'])
def trace_nodes(df):

    mask = np.in1d(df[:, 1], df[:, 0])
    st.table(mask)
    gen_branches = df[~mask]
    df = df[mask]
    yield gen_branches
    while df.size != 0:
        mask = np.in1d(df[:, 1], df[:, 0])
        next_gen = df[~mask]
        gen_branches = numpy_col_inner_many_to_one_join(next_gen, gen_branches)
        df = df[mask]
        yield gen_branches


def numpy_col_inner_many_to_one_join(ar1, ar2):

    ar1 = ar1[np.in1d(ar1[:, -1], ar2[:, 0])]
    ar2 = ar2[np.in1d(ar2[:, 0], ar1[:, -1])]
    if 'int' in ar1.dtype.name and ar1[:, -1].min() >= 0:
        bins = np.bincount(ar1[:, -1])
        counts = bins[bins.nonzero()[0]]
    else:
        counts = np.unique(ar1[:, -1], False, False, True)[1]
    left = ar1[ar1[:, -1].argsort()]
    right = ar2[ar2[:, 0].argsort()]
    return np.concatenate([left[:, :-1],
                           right[np.repeat(np.arange(right.shape[0]),
                                           counts)]], 1)
                       
x = list_ancestors(df.values)      

`

Here is the list of my data Data

Error i get - ValueError: operands could not be broadcast together with shape (313,) (283,)

So I found out the 313 is referring to the row of the ar2 in numpy_col_inner_many_to_one_join fuction.

Below is the traceback:

  2022-11-10 17:15:16.172 Uncaught app exception
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\streamlit\runtime\scriptrunner\script_runner.py", line 562, in _run_script    
    exec(code, module.__dict__)
  File "C:\Users\Admin\OneDrive\Documents\app5.py", line 71, in <module>
    list_ancestors(df.values)
  File "C:\Users\Admin\OneDrive\Documents\app5.py", line 39, in list_ancestors
    for ar in trace_nodes(df):
  File "C:\Users\Admin\OneDrive\Documents\app5.py", line 52, in trace_nodes
    gen_branches = numpy_col_inner_many_to_one_join(next_gen, gen_branches)
  File "C:\Users\Admin\OneDrive\Documents\app5.py", line 69, in numpy_col_inner_many_to_one_join
    return np.concatenate([left[:, :-1],right[np.repeat(np.arange(right.shape[0]), counts)]], 1)
  File "<__array_function__ internals>", line 180, in repeat
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\fromnumeric.py", line 479, in repeat
    return _wrapfunc(a, 'repeat', repeats, axis=axis)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\fromnumeric.py", line 57, in _wrapfunc
    return bound(*args, **kwds)
ValueError: operands could not be broadcast together with shape (313,) (283,)


from ValueError: operands could not be broadcast together with shape (313,) (283,)

No comments:

Post a Comment