<jupyter_start><jupyter_text>SGDRegressor :<jupyter_code>from sklearn.linear_model import SGDRegressor
from sklearn.metrics import mean_squared_error
import numpy as np

def sgdregressor_sklearn(lr_rate , n_iter , eta0 , data , label):
    
    clf = SGDRegressor(alpha=0.00001 , penalty = None , learning_rate=lr_rate , eta0=eta0 , n_iter=n_iter )
    clf.fit(X,Y)
    
    pred = clf.predict(X)
    weight = clf.coef_
    intercept = clf.intercept_
    MSE = mean_squared_error(Y,pred)
    
    
    return weight , intercept , MSE , pred<jupyter_output><empty_output><jupyter_text>Manual SGDRegressor :<jupyter_code>def sgdregresor( n_iter , lr_rate , data ):
    
    w_new = np.zeros(shape = (1,13))
    b_new = 0
    counter = 1
    
    while (counter <= n_iter):
        w_old = w_new
        b_old = b_new
        w_gradient = np.zeros(shape = (1,13))
        b_gradient = 0
        
          
        X_manual = data.sample(10)
        X_10 = np.array(X_manual.drop("PRICE" , axis=1))
        label = X_manual["PRICE"]
        label = np.array(label)
        ##print (label)
        ##X_10 = np.array(data.sample(10))



        for i in range(len(X_10)):
            
            y_pred = np.dot(w_old , X_10[i]) + b_old
            w_gradient += X_10[i] * (label[i] - y_pred)
#             w_gradient += np.dot(X_10[i] , (label[i] - y_pred))
            b_gradient += (label[i] - y_pred)
            
        w_gradient = -2 * (w_gradient/len(X_10))
        b_gradient = -2 * (b_gradient/len(X_10))
        
        w_new = w_old - (lr_rate * w_gradient)
        b_new = b_old - (lr_rate * b_gradient)
        
        counter += 1
        
    return w_new , b_new

    







from sklearn.metrics import mean_squared_error

def prediction_manual_sgd(w , b , data , label):
    y_pred = []
    data = np.array(data)
    for i in range(len(data)) :
        pred_values = np.asscalar( np.dot(w,data[i]) + b )
        y_pred.append(pred_values)
    y_pred = np.array(y_pred)
    MSE = mean_squared_error(label , y_pred)
    
    return y_pred , MSE
results = pd.DataFrame(columns=['Iterations' , 'Algorithm' ,'Learning_rate' , 'MSE' ,'Intercept'])
weight_compare = pd.DataFrame(columns=['Iteration' , 'Diff_in_W_more_than_30' , 'Diff_in_b'])
import matplotlib.pyplot as plt

def pred_actual_plot(pred , actual , title):
    
    plt.scatter(actual,pred)
    plt.title(title)
    plt.xlabel('actual y')
    plt.ylabel('predicted y')
    plt.grid(b=True, linewidth=0.5)
    plt.show()
    
def diff_intercept(b , b_manual):
    return abs(b - b_manual)
def diff_weight(w,w_manual):
    count = 0
    percent=abs((w-w_manual)/w_manual)*100
    for i in range(13):
        if (percent[i]>30):
            count+=1
            
    return count<jupyter_output><empty_output><jupyter_text>1.1 Learning_rate = 0.01 , iterations = 1 :<jupyter_code>w,b,MSE,pred = sgdregressor_sklearn('constant' , 1 , 0.01 , X , Y)
pred_actual_plot(pred , Y , "Sklearn SGD")

w_manual , b_manual = sgdregresor(1 , 0.01 , X_std )
pred_manual , MSE_manual = prediction_manual_sgd(w_manual,b_manual,X,Y)
pred_actual_plot(pred_manual , Y , "Manual SGD")

intercept_diff = diff_intercept(b,b_manual)


weight_diff = diff_weight(w,w_manual[0])


df_temp = [1 , 'Sklearn SGD' , 0.01 , MSE , b[0]]
results.loc[0] = df_temp

df_temp_manual = [1 , 'Manual SGD' , 0.01 , MSE_manual , b_manual[0]]
results.loc[1] = df_temp_manual

df_temp_diff = [1 , weight_diff , intercept_diff[0]]
weight_compare.loc[0] = df_temp_diff<jupyter_output>/anaconda3/envs/yolo/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py:130: DeprecationWarning: n_iter parameter is deprecated in 0.19 and will be removed in 0.21. Use max_iter and tol instead.
  DeprecationWarning)<jupyter_text>1.2  Learning_rate = 0.01 , iterations = 10 :<jupyter_code>w,b,MSE,pred = sgdregressor_sklearn('constant' , 10 , 0.01 , X , Y)
pred_actual_plot(pred , Y , "Sklearn SGD")

w_manual , b_manual = sgdregresor(10 , 0.01 , X_std )
pred_manual , MSE_manual = prediction_manual_sgd(w_manual,b_manual,X,Y)
pred_actual_plot(pred_manual , Y , "Manual SGD")

intercept_diff = diff_intercept(b,b_manual)




weight_diff = diff_weight(w,w_manual[0])


df_temp = [10 , 'Sklearn SGD' , 0.01 , MSE , b[0]]
results.loc[2] = df_temp

df_temp_manual = [10 , 'Manual SGD' , 0.01 , MSE_manual , b_manual[0]]
results.loc[3] = df_temp_manual

df_temp_diff = [10 , weight_diff , intercept_diff[0]]
weight_compare.loc[1] = df_temp_diff<jupyter_output>/anaconda3/envs/yolo/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py:130: DeprecationWarning: n_iter parameter is deprecated in 0.19 and will be removed in 0.21. Use max_iter and tol instead.
  DeprecationWarning)<jupyter_text>1.3  Learning_rate = 0.01 , iterations = 100 :<jupyter_code>w,b,MSE,pred = sgdregressor_sklearn('constant' , 100 , 0.01 , X , Y)
pred_actual_plot(pred , Y , "Sklearn SGD")

w_manual , b_manual = sgdregresor(100 , 0.01 , X_std )
pred_manual , MSE_manual = prediction_manual_sgd(w_manual,b_manual,X,Y)
pred_actual_plot(pred_manual , Y , "Manual SGD")

intercept_diff = diff_intercept(b,b_manual)




weight_diff = diff_weight(w,w_manual[0])


df_temp = [100 , 'Sklearn SGD' , 0.01 , MSE , b[0]]
results.loc[4] = df_temp

df_temp_manual = [100 , 'Manual SGD' , 0.01 , MSE_manual , b_manual[0]]
results.loc[5] = df_temp_manual

df_temp_diff = [100 , weight_diff , intercept_diff[0]]
weight_compare.loc[2] = df_temp_diff<jupyter_output>/anaconda3/envs/yolo/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py:130: DeprecationWarning: n_iter parameter is deprecated in 0.19 and will be removed in 0.21. Use max_iter and tol instead.
  DeprecationWarning)<jupyter_text>1.4  Learning_rate = 0.01 , iterations = 1000 :<jupyter_code>w,b,MSE,pred = sgdregressor_sklearn('constant' , 1000 , 0.01 , X , Y)
pred_actual_plot(pred , Y , "Sklearn SGD")

w_manual , b_manual = sgdregresor(1000 , 0.01 , X_std )
pred_manual , MSE_manual = prediction_manual_sgd(w_manual,b_manual,X,Y)
pred_actual_plot(pred_manual , Y , "Manual SGD")

intercept_diff = diff_intercept(b,b_manual)




weight_diff = diff_weight(w,w_manual[0])


df_temp = [1000 , 'Sklearn SGD' , 0.01 , MSE , b[0]]
results.loc[6] = df_temp

df_temp_manual = [1000 , 'Manual SGD' , 0.01 , MSE_manual , b_manual[0]]
results.loc[7] = df_temp_manual

df_temp_diff = [1000 , weight_diff , intercept_diff[0]]
weight_compare.loc[3] = df_temp_diff<jupyter_output>/anaconda3/envs/yolo/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py:130: DeprecationWarning: n_iter parameter is deprecated in 0.19 and will be removed in 0.21. Use max_iter and tol instead.
  DeprecationWarning)<jupyter_text>1.5  Learning_rate = 0.01 , iterations = 5000 :<jupyter_code>w,b,MSE,pred = sgdregressor_sklearn('constant' , 5000 , 0.01 , X , Y)
pred_actual_plot(pred , Y , "Sklearn SGD")

w_manual , b_manual = sgdregresor(5000 , 0.01 , X_std )
pred_manual , MSE_manual = prediction_manual_sgd(w_manual,b_manual,X,Y)
pred_actual_plot(pred_manual , Y , "Manual SGD")

intercept_diff = diff_intercept(b,b_manual)




weight_diff = diff_weight(w,w_manual[0])


df_temp = [5000 , 'Sklearn SGD' , 0.01 , MSE , b[0]]
results.loc[8] = df_temp

df_temp_manual = [5000 , 'Manual SGD' , 0.01 , MSE_manual , b_manual[0]]
results.loc[9] = df_temp_manual

df_temp_diff = [5000 , weight_diff , intercept_diff[0]]
weight_compare.loc[4] = df_temp_diff<jupyter_output>/anaconda3/envs/yolo/lib/python3.6/site-packages/sklearn/linear_model/stochastic_gradient.py:130: DeprecationWarning: n_iter parameter is deprecated in 0.19 and will be removed in 0.21. Use max_iter and tol instead.
  DeprecationWarning)<jupyter_text>Conclusion :<jupyter_code>results
weight_compare<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12516079238487265, 'fraction_numerical': 0.0410342166195009, 'mean_word_length': 4.048701298701299, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '233870', 'license': 'Apache-2.0', 'n_tokens_mistral': 3385, 'n_tokens_neox': 3044, 'n_words': 559, 'path': 'NLP-Amazon-Food-Reviews/SGD_Linear_Regression (1).ipynb', 'repo_name': 'simformsolutions/MachineLearningCaseStudies', 'size': '7774'}