38 KiB
38 KiB
In [1]:
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import PolynomialFeatures # use the fit_transform method of the created object!
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_errorIn [2]:
n = 100
x = np.random.rand(n, 1)
y = 2.0 + 5 * x**2 + 0.1 * np.random.randn(n, 1)
line_model = LinearRegression().fit(x, y)
line_predict = line_model.predict(x)
#line_mse = ...
#poly_features = ...
#poly_model = LinearRegression().fit(..., y)
#poly_predict = ...
#poly_mse = ...
plt.scatter(x, y, label = "Data")
plt.scatter(x, line_predict, label = "Line model")
plt.legend()
plt.show()In [ ]:
from sklearn.model_selection import train_test_splitIn [ ]:
polynomial_features = ...
#X_train, X_test, y_train, y_test = train_test_split(polynomial_features, y, test_size=0.2)