Learn Before
Code

Python: Polynomial Regression

#imports import numpy as np from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures #creating sample data array_x = np.array([5, 10, 15, 20, 25]).reshape((-1, 1)) ~ calling reshape makes sure that the array is 2D array_y = np.array([5, 3, 15, 23, 6]) #transforming data transformed = PolynomialFeatures(degree = 2, include_bias = False) #degree = 2, so this is a binomial regression #then fit it transformed.fit(array_x) #now it has been fitted, so let’s create a new input x_mod = transformed.transform(array_x) #create a model and fit it model = LinearRegression().fit(x_mod, y) #get results r_squared = model.score(x_mod, y) intercept = model.intercept_ coefficients = model.coef_ #predict response y_pred = model.predict(x_)

0

0

Updated 2020-02-27

Tags

Data Science