<jupyter_start><jupyter_text>Defining all required functions for Gradient Descent<jupyter_code>def cost(x_train,y_train,m):
    total_cost=0
    for i in range(len(x_train)):
        total_cost+=(1/len(x_train))*((y_train[i]-(m*x_train[i]).sum())**2)
    return total_cost
def step_gradient(x_train,y_train,learning_rate,m):
    M=len(x_train)
    N=len(x_train[0])
    m_slope=np.zeros(N)
    for i in range(M):
        for j in range(N):
            x=x_train[i]
            y=y_train[i]
            m_slope[j]+=(-2/M)*((y-(m*x).sum())*x[j])
    m=m-learning_rate*m_slope
    return m
def gd(x_train,y_train,learning_rate,no_of_iterations):
    M=len(x_train)
    m=np.zeros(len(x_train[0]))
    for i in range(no_of_iterations):
        m=step_gradient(x_train,y_train,learning_rate,m)
        #print(i,"Cost:",cost(x_train,y_train,m))
    print("Cost:",cost(x_train,y_train,m))
    return m
def predict(x_test, m):
    y_predict = np.zeros(len(x_test)) 
    for i in range(len(x_test)):
        y_predict[i] = (x_test[i] * m).sum()
    return y_predict
def score(x , y , m):
    u = 0
    v = 0
    mean = y.mean()
    for i in range(len(x)):
        u = u + ( y[i] - ( m * x[i] ).sum() ) ** 2
        v = v + ( y[i] - mean ) ** 2
    print("score :" , 1- (u/v))
def stocastic_step_gradient(x_train,y_train,learning_rate,m):
    M=len(x_train)
    N=len(x_train[0])
    m_slope=np.zeros(N)    
    for i in range (M):
        for j in range(N):
            x=x_train[i]
            y=y_train[i]
            m_slope[j] += (-2/M)*(y-(m*x).sum())*x[j]
            m[j]=m[j]-learning_rate*m_slope[j]            
    m = m - learning_rate * m_slope
    return m
def stocastic_gd(x_train,y_train,learning_rate,no_of_iterations):
    M=len(x_train)
    m=np.zeros(len(x_train[0]))
    for i in range(no_of_iterations):
        m=stocastic_step_gradient(x_train,y_train,learning_rate,m)
        #print(i,"Cost:",cost(x_train,y_train,m))
    print("Cost:",cost(x_train,y_train,m))
    return m<jupyter_output><empty_output><jupyter_text>Data Preprocessing<jupyter_code>boston = pd.read_csv('0000000000002417_training_boston_x_y_train.csv')
boston
boston.shape
boston.isnull().sum()
boston.columns
y_train = boston[' Y']
y_train = np.array(y_train)
boston.drop(" Y", inplace = True , axis = 1)
y_train.shape
x_train = np.array(boston)
x_train.shape
df = pd.DataFrame(x_train)
df
features = df.columns
b=13
for a in features:
    df[b]=df[a]*df[a]
    b+=1
c=26
for a in features:
    df[c]=df[a]*df[a]*df[a]
    c+=1
d=39
for a in features:
    df[d]=df[a]*df[a]*df[a]*df[a]
    d+=1
x_train = np.array(df)
x_train = np.append(x_train , np.ones((len(x_train),1), dtype = int) , axis = 1)
x_train = scaler.fit_transform(x_train)
x_train.shape
x_test = pd.read_csv('0000000000002417_test_boston_x_test.csv',header=None)
df = x_test
features = df.columns
b=13
for a in features:
    df[b]=df[a]*df[a]
    b+=1
c=26
for a in features:
    df[c]=df[a]*df[a]*df[a]
    c+=1
d=39
for a in features:
    df[d]=df[a]*df[a]*df[a]*df[a]
    d+=1
x_test = np.array(df)
x_test = np.append(x_test , np.ones((len(x_test),1), dtype = int) , axis = 1)
x_test = scaler.transform(x_test)
x_test.shape<jupyter_output><empty_output><jupyter_text>Applying in-built Linear Regression<jupyter_code>%%time
from sklearn.linear_model import LinearRegression
alg = LinearRegression()
alg.fit(x_train , y_train)
print(alg.score(x_train , y_train))<jupyter_output>0.8521712084300799
Wall time: 66 ms<jupyter_text>Applying stocastic GD<jupyter_code>%%time
learning_rate = 0.01
num_iterations = 300
m = stocastic_gd(x_train,y_train, learning_rate, num_iterations)
score(x_train , y_train , m)<jupyter_output>Cost: 16.69247387223295
score : 0.8101679173520975
Wall time: 32.3 s<jupyter_text>Applying GD<jupyter_code>%%time
learning_rate = 0.12
num_iterations = 2000
m = gd(x_train,y_train, learning_rate, num_iterations)
score(x_train , y_train , m)<jupyter_output>Cost: 15.936646208455949
score : 0.8187634281575007
Wall time: 3min 10s<jupyter_text>Making predictions using Gradient Descent<jupyter_code>y_predict = predict(x_test,m)
#y_predict = np.around(y_predict , 5)
np.savetxt('0000000000002417_test_boston_y_test.csv' , y_predict)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1534641759281154, 'fraction_numerical': 0.04516434145187988, 'mean_word_length': 4.327455919395466, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 3, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '580201', 'license': 'MIT', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1803, 'n_words': 293, 'path': 'Basics of ML and DL/ML/GradientDescent_for_N-D_data/boston_assignment/boston_assignment.ipynb', 'repo_name': 'rammya29/Hands-on-ML-Basic-to-Advance-', 'size': '4229'}