Recently I started to learn sklearn, numpy and pandas and I made a function for multivariate linear regression. Im wondering, is it possible to make multivariate polynomial regression?
This is my code for multivariate polynomial regression, it shows this error:
in check_consistent_length " samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [8, 3]
Do you know whats the problem?
import numpy as np
import pandas as pd
import xlrd
from sklearn import linear_model
from sklearn.model_selection import train_test_split
def polynomial_prediction_of_future_strenght(input_data, cement, blast_fur_slug,fly_ash,
water, superpl, coarse_aggr, fine_aggr, days):
variables = prediction_accuracy(input_data)[4]
results = prediction_accuracy(input_data)[5]
var_train, var_test, res_train, res_test = train_test_split(variables, results, test_size = 0.3, random_state = 4)
Poly_Regression = PolynomialFeatures(degree=2)
poly_var_train = Poly_Regression.fit_transform(var_train)
poly_var_test = Poly_Regression.fit_transform(var_test)
input_values = [cement, blast_fur_slug, fly_ash, water, superpl, coarse_aggr, fine_aggr, days]
regression = linear_model.LinearRegression()
model = regression.fit(poly_var_train, res_train)
predicted_strenght = regression.predict([input_values])
predicted_strenght = round(predicted_strenght[0], 2)
score = model.score(poly_var_test, res_test)
score = round(score*100, 2)
print(prediction, score)
a = polynomial_prediction_of_future_strenght(data_less_than_28days, 260.9, 100.5, 78.3, 200.6, 8.6, 864.5, 761.5, 28)
Multivariate polynomial regression was used to generate polynomial iterators for time series exhibiting autocorrelations. A stepwise technique was used to add and remove polynomial terms to ensure the model contained only those terms that produce a statistically significant contribution to the fit.
Multiple Linear Regression is a machine learning algorithm where we provide multiple independent variables for a single dependent variable. However, linear regression only requires one independent variable as input.
You can transform your features to polynomial using this sklearn
module and then use these features in your linear regression model.
from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model
poly = PolynomialFeatures(degree=2)
poly_variables = poly.fit_transform(variables)
poly_var_train, poly_var_test, res_train, res_test = train_test_split(poly_variables, results, test_size = 0.3, random_state = 4)
regression = linear_model.LinearRegression()
model = regression.fit(poly_var_train, res_train)
score = model.score(poly_var_test, res_test)
Also, in your code you are training your model on the entire dataset and then you split it into train and test. This means that your model has already seen your test data while training. You need to split first, then train your model only on training data and then test the score on the test set. I have included these changes as well. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With