In one of our previous articles, we discussed polynomial regression using the sklearn Python library. We read the “mpg” dataset from the seaborn library and created a polynomial regression model that takes the horsepower of a car as input and determines its mpg or miles per gallon as output.
We also used the following Python code for that purpose.
import seaborn from sklearn.model_selection import train_test_split from sklearn.linear_model import LinearRegression from sklearn.preprocessing import PolynomialFeatures from sklearn.metrics import r2_score, mean_squared_error df = seaborn.load_dataset("mpg") print(df.head()) df = df.dropna() X_train, X_test, y_train, y_test = train_test_split(df[["horsepower"]], df["mpg"], train_size=0.8, shuffle=True, random_state=1) polynomial_features = PolynomialFeatures(degree=2, include_bias=False) X_train_transformed = polynomial_features.fit_transform(X_train) polynomial_regression = LinearRegression() polynomial_regression.fit(X_train_transformed, y_train) X_test_transformed = polynomial_features.transform(X_test) y_test_predicted = polynomial_regression.predict(X_test_transformed) r2 = r2_score(y_test, y_test_predicted) rmse = mean_squared_error(y_test, y_test_predicted, squared=False) print("R2: ", r2) print("RMSE: ", rmse)
So, here, we are first creating additional features using the PolynomialFeatures class. Then, the features are passed to the …






0 Comments