
Now, we divide the dataset into train and test set.
X_train, X_test, y_train, y_test = train_test_split(df[["horsepower"]], df["mpg"], train_size=0.8, shuffle=True, random_state=1)
Here, we are using the train_test_split() function to split the dataset into training set and test set. Please note that we train a machine learning model on the training set. And after that, the machine learning model is run on the test set to see the performance of the model.
The train_size=0.8 parameter here indicates 80% of the total dataset is kept as training set. And 20% will be used as test set. The shuffle=True parameter indicates that we shuffle the data before splitting. And the random_state=1 parameter controls the random number generator that is used for shuffling the data.
Now, the following Python statements are executed:
polynomial_features = PolynomialFeatures(degree=2, include_bias=False) X_train_transformed = polynomial_features.fit_transform(X_train)
Firstly, we initialize PolynomialFeatures and we indicate that the polynomial has degree 2. Then, we use the polynomial_features.fit_transform() method to transform the X values of the training set.
Now, we execute the following Python statements:
polynomial_regression = LinearRegression() polynomial_regression.fit(X_train_transformed, y_train)
We have previously discussed the similarities between the linear regression and the Polynomial regression. So, the polynomial regression is an extension of the linear regression. So, here, we will use the LinearRegression() method to initialize the polynomial regression…








































0 Comments