1

I have two arrays which have different dimensions. I want to bring the latter(india_cases2) to the former(baseline_diag) so that I can plot them easily. I'm unable to plot india_cases.

india_daily_cases = pd.read_csv('India_Cases.csv')
india_daily_cases_subset = india_daily_cases.loc[india_daily_cases['t']> 33.,['Daily Confirmed']]
india_cases = india_daily_cases_subset.to_numpy()
india_cases2 = india_cases.T

The baseline_diag is an output of function - not sure if providing the whole function code is of any help here. The difference in dimension is as follows:

enter image description here

The code to plot the graphs is follows:

ax.plot(t, india_cases.T, 'r', alpha=0.5, lw=2, label='Actual Observed Cases')
ax.bar(t - 0.2, baseline_onsets, 0.4, label='New Onsets')
ax.bar(t + 0.2, baseline_cases, 0.4, label='New Cases')

The error I get is follows:

ValueError: x and y must have same first dimension, but have shapes (47,) and (1, 47)

How do I change the dimenion for india_cases2

4 Answers 4

2

There are a number of equivalent methods you can use:

india_cases2.ravel()
np.squeeze(india_cases2)
india_cases2.reshape(-1)

Even something like

india_cases2.shape = (india_cases2.size,)

Alternatively, you could expand t with

t.reshape(1, -1)
Sign up to request clarification or add additional context in comments.

1 Comment

Glad you like it. It's a bit more explicit than ravel
1
india_daily_cases_subset = india_daily_cases.loc[india_daily_cases['t']> 33.,['Daily Confirmed']]
india_cases = india_daily_cases_subset.to_numpy()
india_cases2 = india_cases.T

india_daily_cases_subset, with this loc indexing produces a DataFrame with 1 column. The india_cases is a (n,1) array, which with the transpose becomes (1,n).

india_daily_cases_subset = india_daily_cases.loc[india_daily_cases['t']> 33.,'Daily Confirmed']

without the [] around the column selector, the result would be a pandas.Series. Its to_numpy() is then a 1d array (n,) shape.

In other words, by paying attention to your dataframe indexing, you can get the desired 1d array without the squeeze/ravel step.

1 Comment

Thanks. But I wasn't clear about without using []. Can you explain it a little more. I'm sort of coding newbie - so can't get it even if its obvious.
0

use reshaping by adding desired dimension:

baseline_onsets = baseline_onsets[None, :]

I do not see baseline_cases shape but you might need to do the same for that array.

Comments

-1

I would use numpy.ndarray.reshape.

I am not sure which shape will be preferable, but you can change the shape as you want by doing reshape(47), reshape((47,1)) or reshape((1,47)).

https://numpy.org/doc/stable/reference/generated/numpy.ndarray.reshape.html?highlight=reshape#numpy.ndarray.reshape

2 Comments

This is a link only answer
Sorry, I added how to use.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.