<jupyter_start><jupyter_text>Regression A regression problem is when we want to estimate the output of a continuous variable.In order to do so, we fit a function to the existing data and we use it for the generalization. ![image.png](./regression_1.png) In order to do so, we start by defining the function with unknown parameters. For example, in the case above, we expect the mapping function to be linear (y = a*x + b).Once defined, we minimize the cost function, which usually in this case is the sum of all the mean square errors between each training point, and the one given by the function. Imports<jupyter_code>from math import sqrt
import numpy as np
import matplotlib.pyplot as plt
from fit_plot import fit_plot<jupyter_output><empty_output><jupyter_text>Regression toy problem<jupyter_code>def true_fun(X):
    ''' Takes a 1D numpy array and returns a 1D numpy array,
    where f(x) = (1-x) * sqrt(x)'''
    return (1-X)*np.sqrt(X)
n_samples = 45
X0 = np.linspace(0,2,45)
y = true_fun(X0) + np.random.randn(n_samples)*0.15<jupyter_output><empty_output><jupyter_text>Visualizing the data<jupyter_code>plt.scatter(X0, y)
plt.show()<jupyter_output><empty_output><jupyter_text>We will use a function called 'fit_plot'.This function plots the true function, the sample, and the model found with the given degree of the polynomial.  Toy exercise You will use the sklearn library to solve the problem above.<jupyter_code>from sklearn.svm import SVR
from sklearn.linear_model import LinearRegression, SGDRegressor
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor

X = X0.reshape(-1,1)
# step 1: Define the regressor
clf = SVR(kernel='linear')

# step 2: Divide your dataset into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y)

# step 3: fit the model
clf.fit(X_train, y_train)

# step 4: test the model
clf.score(X_test, y_test)
# plot the points and the found function

XX = np.linspace(0, 2, 500).reshape(-1,1)
yy = clf.predict(XX)

plt.figure()
plt.scatter(XX, yy)
plt.scatter(X, y, c='r', label='data')
plt.show()<jupyter_output><empty_output><jupyter_text>Use the same algorithm but change the kernel to 'rbf' and then 'poly'<jupyter_code># Code here for fit and plot with kernal='rbf'

# end code
# Code here for fit and plot with kernal='poly'

# end code<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09171224232225494, 'fraction_numerical': 0.013041649137568364, 'mean_word_length': 5.427027027027027, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '280737', 'license': 'MIT', 'n_tokens_mistral': 822, 'n_tokens_neox': 794, 'n_words': 289, 'path': 'JUPYTER/Supervised/Regression/Regression.ipynb', 'repo_name': 'Reynolds534/IASS_18_ML', 'size': '2377'}