<jupyter_start><jupyter_text>Linear Regression<jupyter_code>import numpy as np
data = np.loadtxt("Dataset/data.csv",delimiter=",")
data.shape
x = data[:, 0].reshape(-1,1)
y = data[:,1]
x.shape
from sklearn import model_selection
X_train, X_test, Y_train ,Y_test = model_selection.train_test_split(x,y)

from sklearn.linear_model import LinearRegression

algo_1 = LinearRegression()
algo_1.fit(X_train,Y_train)
algo_1.coef_
algo_1.intercept_
#--> Training model

import matplotlib.pyplot as plt
m = algo_1.coef_[0]
c = algo_1.intercept_

x_line = np.arange(30,70,0.1)
y_line = m * x_line + c

plt.plot(x_line, y_line,"r")
train_1d = X_train.reshape(75)
plt.scatter(X_train,Y_train)
plt.show()
#--> Testing model

import matplotlib.pyplot as plt
m = algo_1.coef_[0]
c = algo_1.intercept_

x_line = np.arange(30,70,0.1)
y_line = m * x_line + c

plt.plot(x_line, y_line,"r")
train_1d = X_test.reshape(25)
plt.scatter(X_test,Y_test)
plt.show()
Score_test = algo_1.score(X_test,Y_test)
Score_test
Score_train = algo_1.score(X_train,Y_train)
Score_train<jupyter_output><empty_output><jupyter_text>Self - Implementation of Algorithm for 1-d array<jupyter_code>import numpy as np
data = np.loadtxt("Dataset/data.csv",delimiter=",")
data.shape
x = data[:, 0]
y = data[:,1]
x.shape
from sklearn import model_selection
x_train, x_test, y_train ,y_test = model_selection.train_test_split(x,y,test_size = 0.3)<jupyter_output><empty_output><jupyter_text>---> Flowstep 1: m,c = fit(x_train,y_train)step 2: y_pred = predict(x_test,m,c)step 3: score(y_test,y_pred)    ---> step 2 and step 3 can be preformed on both "test data" as well as "trainning data"<jupyter_code># fit function

def fit(x_train,y_train):
    num = (x_train * y_train).mean() - x_train.mean()*y_train.mean()
    den = (x_train**2).mean() - x_train.mean()**2
    m = num / den
    c = y_train.mean() - m*x_train.mean()
    
    return m,c
# predict function

def predict(x,m,c):
    return m * x + c
# score fuction

def score(y_true,y_pred):
    u = ((y_true - y_pred)**2).sum()
    v = ((y_true - y_true.mean())**2).sum()
    
    return 1-u/v
# cost function

def cost(x,y,m,c):
    return ((y-m*c+ c)**2).mean()  # in place of 'mean' we can use 'sum' also
m, c = fit(x_train, y_train)

# test data

y_test_pred = predict(x_test, m, c) 
print("Test Score: ",score(y_test, y_test_pred))

# train data

y_train_pred =  predict(x_train, m, c)

print("Train Score: ",score(y_train, y_train_pred)) 
print("cost on training data : ",cost(x_train,x_train,m,c))

print("M, C : ", m, c)<jupyter_output>Test Score:  0.37279298743188527
Train Score:  0.6330174585756392
cost on training data :  164266.62741084528
M, C :  1.412452890282859 3.446889670595894
{'fraction_non_alphanumeric': 0.14803849000740193, 'fraction_numerical': 0.049592894152479645, 'mean_word_length': 4.953744493392071, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '407911', 'license': 'MIT', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1146, 'n_words': 238, 'path': '26. Linear Regression/.ipynb_checkpoints/Linear Regression-checkpoint.ipynb', 'repo_name': 'AkashKumarSingh11032001/Coding_Ninja_DataScience_Course', 'size': '2702'}