<jupyter_start><jupyter_text>The guide for this notebook:https://www.datazone.space/all-data-science-guides/machine-learning-polynomial-regression<jupyter_code>import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
np.random.seed(42)
#We are setting a seed so that the notebook gives the same result every time
X = 8 * np.random.rand(250, 1) - 4 #np.random.rand generates values between 0 and 1
#This line generates 250 values of x between -4 and (4 * 1), in (250,1), the 1 indicates it is a one-dimensional vector

y = 3 + 3 * X + 1.5 * (X**2) + 0.5 * (X**3) + 2.5* np.random.randn(250, 1)#Generates 250 negative and positive values with a mean of 0 and variance of 1 to add noise
#This line generates 250 values of y
plt.scatter(X, y)
plt.xlabel("$X$", fontsize=16)
plt.show()
# Plot the data
from sklearn.preprocessing import PolynomialFeatures
poly_features = PolynomialFeatures(degree = 3, include_bias=False)
X_poly = poly_features.fit_transform(X)
X[0],X_poly[0]
lin_reg = LinearRegression()
lin_reg.fit(X, y)
lin_reg.intercept_, lin_reg.coef_
X_test = np.linspace(-4, 4, 100).reshape(100, 1) #Generates 100 random values from 0 to 3
y_test = lin_reg.predict(X_test)#Predicts the outputs of the test data
plt.scatter(X, y)
plt.plot(X_test, y_test, "r-", linewidth=3, label="Predictions")
plt.xlabel("$x_1$", fontsize=16)
plt.ylabel("$y$", rotation=0, fontsize=16)
plt.legend(loc="upper left", fontsize=12)
plt.axis([-4, 4, -20, 80])
plt.show()
lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)
lin_reg.intercept_, lin_reg.coef_
X_test = np.linspace(-4, 4, 100).reshape(100, 1) #Generates 100 random values from 0 to 3
X_test_poly = poly_features.transform(X_test)#Transforms the new test data to add the new features
y_test = lin_reg.predict(X_test_poly)#Predicts the outputs of the test data
plt.scatter(X, y)
plt.plot(X_test, y_test, "r-", linewidth=3, label="Predictions")
plt.xlabel("$x_1$", fontsize=16)
plt.ylabel("$y$", rotation=0, fontsize=16)
plt.legend(loc="upper left", fontsize=12)
plt.axis([-4, 4, -20, 80])
plt.show()
poly_features = PolynomialFeatures(degree = 30, include_bias=False)
X_poly = poly_features.fit_transform(X)
lin_reg = LinearRegression()
lin_reg.fit(X_poly, y)
lin_reg.intercept_, lin_reg.coef_
X_test = np.linspace(-4, 4, 100).reshape(100, 1) #Generates 100 random values from 0 to 3
X_test_poly = poly_features.transform(X_test)#Transforms the new test data to add the new features
y_test = lin_reg.predict(X_test_poly) #Predicts the outputs of the test data
plt.scatter(X, y)
plt.plot(X_test, y_test, "r-", linewidth=3, label="Predictions")
plt.xlabel("$x_1$", fontsize=16)
plt.ylabel("$y$", rotation=0, fontsize=16)
plt.legend(loc="upper left", fontsize=12)
plt.axis([-4, 4, -20, 80])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.138898756660746, 'fraction_numerical': 0.04866785079928952, 'mean_word_length': 5.901960784313726, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '462039', 'license': 'MIT', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1059, 'n_words': 290, 'path': 'Polynomial Regression/Polynomial Regression.ipynb', 'repo_name': 'DataZone-space/Machine-Learning', 'size': '2815'}