Skip to content

Instantly share code, notes, and snippets.

@Robofied
Created February 22, 2019 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Robofied/caa6eadbeb2c6ad4af194618e3bf341d to your computer and use it in GitHub Desktop.
Save Robofied/caa6eadbeb2c6ad4af194618e3bf341d to your computer and use it in GitHub Desktop.
#Import required modules
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
#Some random values for input to a model
X_train = [[1,4],[3,5]]
Y_train = [1,2]
X_test = [[1,5]]
#Create a Linear Regressor
lin_regressor = LinearRegression()
#Pass the order of your polynomial here
poly = PolynomialFeatures(degree = 2)
#Convert to be used further to linear regression.
#If we have two variables a and b, then degree 2 and using fit_transform would give us 1,a, b, a2, ab and b2.
X_transform = poly.fit_transform(X_train)
X_test_ = poly.fit_transform(X_test)
#This finds the coefficient of polynomial regression. This is training part of the algorithm.
lin_regressor.fit(X_transform,Y_train)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
#Predict the value on the value passed
y_preds = lin_regressor.predict(X_test_)
#Printing the predicted value
print(y_preds)
#[1.34317343]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment