CHATGPT AI
fitting


§ Code

#importing libraries
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#loading dataset
dataset = pd.read_csv('Position_Salaries.csv')
X = dataset.iloc[:,1:2].values #making sure X is a matrix and not a vector
y = dataset.iloc[:,2].values #dependent variable vector


§ Markdown

**No need to split the data into training and test set because the dataset is too small**

**Fitting Linear Regression to the dataset**





§ Code

from sklearn.linear_model import LinearRegression #importing linear regression class from sklearn library
lin_reg = LinearRegression() #creating an object of the linear regression class
lin_reg.fit(X,y) #fitting linear regression model to the dataset



§ Output

> ['LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)']


§ Markdown

**Fitting Polynomial Regression to the Dataset**



§ Code

from sklearn.preprocessing import PolynomialFeatures #importing polynomial features class from sklearn library
poly_reg = PolynomialFeatures(degree=4) #creating an object of polynomial features class and setting degree to 4 (degree is number of polynomials we want to create) , this will create all polynomials with degree less than or equal to 4 (1,x,x^2,x^3,x^4) , it will also add a column of 1s for constant b0 in equation y=b0+b1x+b2x^2+....+bnx^n (this is done by setting include_bias parameter in constructor to true which is default value ) , so now we have 6 columns in matrix X' now instead of 1 column in matrix X before . This new matrix X' will be used for fitting polynomial regression model . So basically what this class does is it takes one feature and transforms it into multiple features by creating all possible polynomials with degree less than or equal to specified degree . Now we have 6 columns in matrix X' which can be used for fitting polynomial regression model . We can also specify include bias parameter in constructor as false if we don't want that extra column of 1s . Now we have created an object of PolynomialFeatures class with degree set to 4 , now we will fit this object into our independent variable X i.e transform our independent variable X into new matrix X' with 6 columns using fit method on poly_reg object which was created using PolynomialFeatures class . We can use transform method on poly_reg object for transforming independent variable X into new matrix X' but since fit method also does this job so better use fit method only . So basically what fit method does is it takes one feature and transforms it into multiple features by creating all possible polynomials with degree less than or equal to specified degree and then stores these new transformed features in poly_reg object itself which can be accessed using transformed attribute on poly_reg object i.e poly_reg.transformed attribute returns us the new transformed matrix X' which has 6 columns instead of 1 column before transformation . Now after transforming our independent variable into new matrix X' , now we need to fit this new transformed matrix into linear regression model so that we get our polynomial regression model , so for that purpose first create an instance of linear regression class i.e lin reg 2 and then call fit method on this instance passing transformed attribute on poly reg object i.e poly reg dot transformed as first argument and dependent variable y as second argument . Now lin reg 2 object has fitted our transformed independent variable i.e poly reg dot transformed and dependent variable y into linear regression model thus giving us our polynomial regression model which can be accessed using predict method on lin reg 2 object passing some value as argument whose prediction you want . So basically what predict method does is it takes some value x passed as argument and then passes it through equation y = b0 + b1x + b2x^2 + ... + bnx^n where coefficients b0 , b1 ,...bn are stored inside lin reg 2 object itself which were calculated when fitting was done using fit method passing transformed attribute on poly reg object as first argument and dependent variable y as second argument while calling fit method on lin reg 2 instance . Thus predict method returns us predicted value corresponding to x passed as argument after passing x through equation y = b0 + b1x + b2x^2 + ... + bnx^n where coefficients are stored inside lin reg 2 instance itself calculated during fitting process when calling fit method on lin reg 2 instance passing transformed attribute on poly reg instance as first argument and dependent variable y as second argument while calling fit method on lin reg 2 instance . Thus finally after fitting process when calling predict method on lin reg 2 instance passing some value x whose prediction you want , predict method returns us predicted value corresponding to x passed as argument after passing x through equation y = b0 + b1x + b2x^2 + ... + bnx^n where coefficients are stored inside lin reg 2 instance itself calculated during fitting process when calling fit method on lin reg 2 instance passing transformed attribute on poly reg instance as first argument and dependent variable y as second argument while calling fit method on lin
§ Code #importing libraries import pandas as pd import numpy as np import matplotlib.pyplot as plt %matplotlib inline #loading dataset dataset = pd.read_csv('Position_Salaries.csv') X = dataset.iloc[:,1:2].values #making sure X is a matrix and not a vector y = dataset.iloc[:,2].values #dependent variable vector § Markdown **No need to split the data into training and test set because the dataset is too small** **Fitting Linear Regression to the dataset** § Code from sklearn.linear_model import LinearRegression #importing linear regression class from sklearn library lin_reg = LinearRegression() #creating an object of the linear regression class lin_reg.fit(X,y) #fitting linear regression model to the dataset § Output > ['LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)'] § Markdown **Fitting Polynomial Regression to the Dataset** § Code from sklearn.preprocessing import PolynomialFeatures #importing polynomial features class from sklearn library poly_reg = PolynomialFeatures(degree=4) #creating an object of polynomial features class and setting degree to 4 (degree is number of polynomials we want to create) , this will create all polynomials with degree less than or equal to 4 (1,x,x^2,x^3,x^4) , it will also add a column of 1s for constant b0 in equation y=b0+b1x+b2x^2+....+bnx^n (this is done by setting include_bias parameter in constructor to true which is default value ) , so now we have 6 columns in matrix X' now instead of 1 column in matrix X before . This new matrix X' will be used for fitting polynomial regression model . So basically what this class does is it takes one feature and transforms it into multiple features by creating all possible polynomials with degree less than or equal to specified degree . Now we have 6 columns in matrix X' which can be used for fitting polynomial regression model . We can also specify include bias parameter in constructor as false if we don't want that extra column of 1s . Now we have created an object of PolynomialFeatures class with degree set to 4 , now we will fit this object into our independent variable X i.e transform our independent variable X into new matrix X' with 6 columns using fit method on poly_reg object which was created using PolynomialFeatures class . We can use transform method on poly_reg object for transforming independent variable X into new matrix X' but since fit method also does this job so better use fit method only . So basically what fit method does is it takes one feature and transforms it into multiple features by creating all possible polynomials with degree less than or equal to specified degree and then stores these new transformed features in poly_reg object itself which can be accessed using transformed attribute on poly_reg object i.e poly_reg.transformed attribute returns us the new transformed matrix X' which has 6 columns instead of 1 column before transformation . Now after transforming our independent variable into new matrix X' , now we need to fit this new transformed matrix into linear regression model so that we get our polynomial regression model , so for that purpose first create an instance of linear regression class i.e lin reg 2 and then call fit method on this instance passing transformed attribute on poly reg object i.e poly reg dot transformed as first argument and dependent variable y as second argument . Now lin reg 2 object has fitted our transformed independent variable i.e poly reg dot transformed and dependent variable y into linear regression model thus giving us our polynomial regression model which can be accessed using predict method on lin reg 2 object passing some value as argument whose prediction you want . So basically what predict method does is it takes some value x passed as argument and then passes it through equation y = b0 + b1x + b2x^2 + ... + bnx^n where coefficients b0 , b1 ,...bn are stored inside lin reg 2 object itself which were calculated when fitting was done using fit method passing transformed attribute on poly reg object as first argument and dependent variable y as second argument while calling fit method on lin reg 2 instance . Thus predict method returns us predicted value corresponding to x passed as argument after passing x through equation y = b0 + b1x + b2x^2 + ... + bnx^n where coefficients are stored inside lin reg 2 instance itself calculated during fitting process when calling fit method on lin reg 2 instance passing transformed attribute on poly reg instance as first argument and dependent variable y as second argument while calling fit method on lin reg 2 instance . Thus finally after fitting process when calling predict method on lin reg 2 instance passing some value x whose prediction you want , predict method returns us predicted value corresponding to x passed as argument after passing x through equation y = b0 + b1x + b2x^2 + ... + bnx^n where coefficients are stored inside lin reg 2 instance itself calculated during fitting process when calling fit method on lin reg 2 instance passing transformed attribute on poly reg instance as first argument and dependent variable y as second argument while calling fit method on lin
0 Comments & Tags 0 Distribuiri 1 Views

Password Copied!

Please Wait....