<jupyter_start><jupyter_text>Import packages<jupyter_code>import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Import given dataset named 'dataset1.csv'<jupyter_code># https://raw.githubusercontent.com/dhanyahari07/Data_Science/master/dataset1.csv
def read_data():
    # Write your code here
    data = pd.read_csv('https://raw.githubusercontent.com/dhanyahari07/Data_Science/master/dataset1.csv',sep=",",encoding ="utf-16")
    number_of_rows= data.shape[0]
    number_of_columns = data.shape[1]
    return data, number_of_rows, number_of_columns
read_data()<jupyter_output><empty_output><jupyter_text>Split dataset into variable components<jupyter_code>def X_Y_split():
    data,number_of_rows,number_of_columns = read_data()
    X = data['X'].values
    Y = data['Y'].values
    return X, Y
X_Y_split()<jupyter_output><empty_output><jupyter_text>Calculate coefficients<jupyter_code>def coefficients():
    X, Y = X_Y_split()
    mean_x = np.mean(X)
    mean_y = np.mean(Y)
    n = len(X)

    numer = 0
    denom  = 0
    for i in range(n):
      numer+=(X[i]-mean_x) *(Y[i]-mean_y)
      denom+=(X[i]-mean_x) **2 
    b1 = numer/denom
    b0 = mean_y-(b1 * mean_x)
    #write your coe here
    # Return coefficients
    return round(b1,3), round(b0,3)
coefficients()<jupyter_output><empty_output><jupyter_text>Plot the values along with the regression line<jupyter_code>def plot():
    # Write your code here
    X, Y = X_Y_split()
    b1,b0 = coefficients()
    
    # Calculating line values x and y
    max_x = np.max(X) + 100
    min_x = np.min(X) - 100
    x = np.linspace(min_x, max_x, 1000) # Returns number spaces evenly w.r.t interval
    y = b0 + b1 * x

    # Ploting Line
    plt.plot(x, y, color='b', label='Regression Line')

    # Ploting Scatter Points
    plt.scatter(X, Y, c='r', label='Scatter Plot')
    plt.xlabel('X')
    plt.ylabel('Y',rotation=0)
    plt.legend();
    plt.show()
plot()<jupyter_output><empty_output><jupyter_text>Compute the RMSE value<jupyter_code>def rmse():
    data,n,m = read_data()
    b1, b0 = coefficients()
    X, Y = X_Y_split()
    y_pred=b0+b1*X
    rmse=np.sum(np.square(y_pred-Y))
    rmse=np.sqrt((rmse/n))
    # Write your code here
    return round(rmse,3)
rmse()<jupyter_output><empty_output><jupyter_text>Compute the R^2 score<jupyter_code>def r2():
    _,n,_ = read_data()
    b1, b0 = coefficients()
    X, Y = X_Y_split()
    # write your code here
    mean_x = np.mean(X)
    mean_y = np.mean(Y)
    ss_tot = 0
    ss_res = 0
    for i in range(n):
      y_pred = b0 + b1* X[i]
      ss_tot += (Y[i]- mean_y) **2
      ss_res += (Y[i]- y_pred) **2
    r2 = 1 - (ss_res/ss_tot)
    return round(r2,3)
r2()<jupyter_output><empty_output><jupyter_text>Compare with values computed using sklearn<jupyter_code>def compare_rmse():
    _,n,_ = read_data()
    X, Y = X_Y_split()
    old_rmse = rmse()
    old_r2 = r2()

    # Write your code here
    X = X.reshape((n,1))
    reg = LinearRegression()
    reg = reg.fit(X,Y)
    Y_pred = reg.predict(X)
    mse = mean_squared_error(Y,Y_pred)
    s_rmse = np.sqrt(mse)
    s_r2 = reg.score(X,Y)
    return round(abs(old_rmse-s_rmse),3), round(abs(old_r2-s_r2),3)
compare_rmse()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13579163008486977, 'fraction_numerical': 0.018729880011706175, 'mean_word_length': 3.787114845938375, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '339888', 'license': 'MIT', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1356, 'n_words': 271, 'path': 'Evaluations/eval_Simple_Linear_Regression.ipynb', 'repo_name': 'ColdCoffee21/Foundations-of-Data-Science', 'size': '3417'}