<jupyter_start><jupyter_text>**Artificial Intelligence - MSc**CS6462 - PROBABILISTIC AND EXPLAINABLE AI SEM2 2021/2CS6462_Lab_4.2Instructor: <NAME>Copyright (C) 2022 - All rights reserved, do not copy or distribute without permission of the author. Linear Regression In statistics, [linear regression](https://en.wikipedia.org/wiki/Linear_regression) is a linear approach for modelling the relationship between a scalar response and one or more explanatory variables (also known as dependent and independent variables).[Linear regression](https://www.youtube.com/watch?v=nk2CQITm_eo) is the next step up after correlation.  Example-2<jupyter_code># import basic modules
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
## Generate linear artificial (synthetic) data

# to replicate same experiment each time
#R = np.random.RandomState(1001) # <- magic number (9)

# number of data points
N = 50

# independent var
x = np.linspace(10, 15, num=N)

# noise
ϵ = np.random.randn(N)*0.15

# true response var
y = np.sin(x) + ϵ 
# plot (linear) data points
x = x.reshape(-1, 1)
y = y.reshape(-1, 1)
plt.scatter(x, y);<jupyter_output><empty_output><jupyter_text>Linear estimation We will start using one of the most well-konwn library in Python for machine learning: [scikit-learn](https://scikit-learn.org/stable/).Scikit has many models to try, we will start by using:* [LinearRegression](https://scikit-learn.org/stable/modules/classes.htmlmodule-sklearn.linear_model)<jupyter_code>from sklearn.linear_model import LinearRegression

LR = LinearRegression()
y_fit =  LR.fit(x,y).predict(x)

plt.scatter(x, y)
plt.plot(x, y_fit, color='red')
legend_title = 'Linear Regression\n {} + {}*x'.format(round(LR.intercept_[0],2),round(LR.coef_[0][0],2))
legend = plt.legend(loc='upper left', frameon=False, title=legend_title)
plt.title("Linear Regression")
plt.show();
from sklearn.linear_model import LinearRegression

LR = LinearRegression()
y_fit =  LR.fit(x,y).predict(x)
plt.figure(figsize=(8,6))
plt.tight_layout();
sns.displot(y, kde=True);<jupyter_output><empty_output><jupyter_text>Exercise-4* Create a new input dataset using: ```linspace(0,1,100)```;* use this input and create an output summing the input to: ```x2+ np.cos(4*np.pi*x2) + np.random.randn(len(x2))*0.2```;* plot input in green;* use the linear model to fit the input data;* plot the linear model in brown;* add the equation of the model in the plot similarlly as in the example.* plot the histogram and the distribution shape in the same figure as in the example<jupyter_code># your code here<jupyter_output><empty_output><jupyter_text>Exercise-5* Create a new input dataset using: ```linspace(0,1,100)```;* create a periodic datatset using the input in the following code:```np.hstack([np.cos(2*np.pi*input),np.sin(2*np.pi*input),                      np.cos(4*np.pi*input),np.sin(4*np.pi*input),                      np.cos(6*np.pi*input),np.sin(6*np.pi*input),                      np.cos(8*np.pi*input),np.sin(8*np.pi*input),                      np.cos(10*np.pi*input),np.sin(10*np.pi*input),                      np.cos(12*np.pi*input),np.sin(12*np.pi*input)])```* create the output using the following code:```input + np.cos(4*np.pi*input) + np.random.randn(len(input))*0.2```* you can rename "input" with a suitable var name* plot input in orange using a figure with this size: (20,12)* plot output in cyan* show the equation of the model in the plot* plot the histogram and the distribution shape in the same figure as in the example<jupyter_code># your code here<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13022998060404545, 'fraction_numerical': 0.024383485730119146, 'mean_word_length': 4.927750410509031, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 18, 'https://': 4, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '370401', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1196, 'n_words': 352, 'path': 'Weeks_4-6/Labs/CS6462_Lab_4_2.ipynb', 'repo_name': 'evassev/CS6462_SEM2_2021-2', 'size': '3617'}