Stack Exchange
Multiple Linear Regression Machine Learning in Python --ValueError: shapes (8,15) and (390,) not aligned
I am trying to evaluate output based on certain input, using Multiple Linear Regression Machine Learning .I have trained the data and getting correct expected values while running below code:
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd


# Importing the dataset
#dataset = pd.read_csv('50_Startups.csv')
dataset = pd.read_excel('MAHI2.xlsx')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, 5].values

# Encoding categorical data
from sklearn.preprocessing import LabelEncoder, OneHotEncoder
labelencoder = LabelEncoder()
X[:, 0] = labelencoder.fit_transform(X[:, 0])

labelencoder1 = LabelEncoder()
X[:, 1] = labelencoder.fit_transform(X[:, 1])

labelencoder2 = LabelEncoder()
X[:, 2] = labelencoder.fit_transform(X[:, 2])

labelencoder3 = LabelEncoder()
X[:, 3] = labelencoder.fit_transform(X[:, 3])

onehotencoder = OneHotEncoder(categorical_features = "all")
#X = onehotencoder.fit_transform(X).toarray()
X = onehotencoder.fit_transform(X).toarray()

# Avoiding the Dummy Variable Trap
X = X[:, 1:]


from sklearn.linear_model import LinearRegression
regressor = LinearRegression()

regressor.fit(X, y)
y_pred = regressor.predict(X)
df = pd.DataFrame({'Actual': y.flatten(), 'Predicted': y_pred.flatten()})
df

Now I am trying to use same model to evaluate another set of input data as below :
dataset1 = pd.read_excel('MAHI3.xlsx')
#dataset2 = pd.get_dummies(dataset1)
X1 = dataset1.iloc[:, :-1].values
y2 = dataset1.iloc[:, 5].values                 

# Encoding categorical data
#labelencoder3 = LabelEncoder()
X1[:, 0] = labelencoder.fit_transform(X1[:, 0])

#labelencoder4 = LabelEncoder()
X1[:, 1] = labelencoder.fit_transform(X1[:, 1])

#labelencoder5 = LabelEncoder()
X1[:, 2] = labelencoder.fit_transform(X1[:, 2])

#labelencoder6 = LabelEncoder()
X1[:, 3] = labelencoder.fit_transform(X1[:, 3])

#onehotencoder2 = OneHotEncoder(categorical_features = "all")
X1 = onehotencoder.fit_transform(X1).toarray()

output = regressor.predict(X1)

df1 = pd.DataFrame({'Actual1': y2.flatten(), 'Predicted1': output.flatten()})
df1

But while I am running this code getting below error:
ValueError: shapes (6,13) and (390,) not aligned: 13 (dim 1) != 390 (dim 0)
It will be great if anyone help me to resolve this issue.

I don't have access to your dataset but I seems that your problem is a dimensionality problem. The thing that seems to change dimensions is the "onehotencoder".
Try to use the same one hot encoder for both.
ohe = onehotencoder.fit(X)
X = ohe.transform(X).toarray()
X1 = ohe.transform(X1).toarray()

You should make sure that the number of features that the "regressor" model is receiving is the same that when it is trained. 
