x and y must have same first dimension, but have shapes (9,) and (4,)

Question:

y_pred = fig.predict(x.reshape(1, -1)).reshape(-1)
colors_dark = ["#1F1FIF", "#313131", "#636363", "#AEAEAE", "#DADADA"] 
colors_red = ["#331313", "#582626", "#9E1717", "#D35151", "#E9B4B4"] 
colors_green=[ "# 01411C", "#4B6F44", "#4F7942", "#74C365", "#D0F0C0"]
filterwarnings("ignore")
epochs =[ i for i in range(9)]
fig, ax = plt.subplots(1, 2,figsize=(14,7))
train_acc1 = model_history.history[ "accuracy" ] 
train_lossl = model_history.history[ "loss"]
val_acc1 = model_history.history[ "val_accuracy" ]
val_loss1 = model_history.history[ "val_loss" ]
fig.text(s='Epochs vs. Training and Validation Accuracy/Loss',size=18,fontweight='bold',      

fontname='monospace',color=colors_dark[1],y=1,x=0.28,alpha=0.8)
sns.despine()


ax[0].plot(epochs,train_acc1,marker='o',markerfacecolor=colors_green[2],color=colors_green[3],
label="Training Accuracy")
ax[0].plot(epochs, val_acc1,  

marker='o',markerfacecolor=colors_red[2],color=colors_red[3],label="Validation Accuracy")
ax[0].legend(frameon=False)
ax[0].set_xlabel("Epochs")
ax[0].set_ylabel("Accuracy")
sns.despine()
ax[1].plot(epochs[:],train_loss1,marker='o',markerfacecolor=colors_green[2],
color=colors_green[3],label="Training Loss")
ax[1].plot(epochs[:],val_loss1,marker="o",markerfacecolor=colors_red[2],color=colors_red[3],
label = "Validation Loss")
ax[1].legend(frameon=False)
ax[1].set_xlabel("Epochs")
ax[1].set_ylabel("Training & Validation Loss")
fig.show()

This is my code. But when i execute it, it says that x and y must have first dimension but have shapes 9 and 4. Please help me !!!! any help would be appreciated

Asked By: Nithya Balagopal

||

Answers:

At whatever line the error is in you are trying to plot 2 lists with different lengths specifically 9,4. The list epochs, that you use as x_array whenever you plot something, has length 9. So it means that one of the lists train_acc1, train_loss1, val_acc1, val_loss1 has length of 4. When you get the error see at which line it is and see which of those lists you are trying to plot there.
Then instead of having the epoch as the x_array try something with a length of 4 like [0,1,2,3]. I suspect you might get this error again in another line so follow the same process. Hope this helps.

Answered By: Jerry_V