<jupyter_start><jupyter_text>Boston House PredictorIn this notebook we will use gradient descent for predicting house prices. We will use sklearn's boston houses data set to train our model.We will try using feature scaling to see if our results improve by any means. Accuracy of Model - 0.87817<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import time
# Loading data
data = np.genfromtxt('boston_x_y_train.csv', delimiter=',')
# Splitting into X_train and Y_train
X_train = data[:, 0:13]
Y_train = data[:, 13]


# we will try to convert the training data into degree 2 to calculate a more complex boundary
columns = X_train.shape[1]

# cross pairs
for j in range(columns):
    for k in range(j + 1, columns):
        new_column = (X_train[:, j] * X_train[:, k]).reshape(-1, 1)
        X_train = np.append(X_train, new_column, axis=1)

# pair with self
for i in range(columns):
    new_column = (X_train[:, i] * X_train[:, i]).reshape(-1,1)
    X_train = np.append(X_train, new_column, axis=1)
    
# adding a row of ones to training data for gradient descent implementation
ones = np.ones(X_train.shape[0]).reshape(-1, 1)
X_train = np.append(X_train, ones, axis=1)

X_test = np.genfromtxt('boston_x_test.csv', delimiter=',')

#testing data - degree 2
columns = X_test.shape[1]

# cross pairs
for j in range(columns):
    for k in range(j + 1, columns):
        new_column = (X_test[:, j] * X_test[:, k]).reshape(-1, 1)
        X_test = np.append(X_test, new_column, axis=1)

# pair with self
for i in range(columns):
    new_column = (X_test[:, i] * X_test[:, i]).reshape(-1,1)
    X_test = np.append(X_test, new_column, axis=1)

test_ones = np.ones(X_test.shape[0]).reshape(-1, 1)
X_test = np.append(X_test, test_ones, axis=1)


data.shape, X_train.shape, Y_train.shape, X_test.shape
# Creating a data frame
df = pd.DataFrame(X_train)
df.head()
# Describing our data
df.describe()<jupyter_output><empty_output><jupyter_text>Gradient Descent For N Features<jupyter_code>def step_gradient(X_train, Y_train, learning_rate, m,j):
    # Calculate new slope for jth feature
    m_j = 0
    n_data_pts = X_train.shape[0]
    N = len(m)
    for i in range(n_data_pts):
        # calculate the formula m1xi(1)+m2xi(2)+...
        x_i = X_train[i, :]
        y_i = Y_train[i]
        temp_sum = 0
        for k in range(N):
            temp_sum += m[k]*x_i[k]
        ### sub y_i from temp sum
        temp_sum = y_i - temp_sum
        ## complete formula
        m_j += (-2/n_data_pts) * (temp_sum) * x_i[j]
    # update m[j] and return
    m[j] = m[j] - (learning_rate*m_j)
    return m[j]
def gradient_descent(X_train, Y_train, learning_rate, num_iterations):
    # Start with random values for all m's
    m = [0]*(X_train.shape[1])
    m[-1] = 1 #c
    N = len(m)
    x_data = []
    y_data = []
    fig = plt.figure()
    for i in range(num_iterations):
        # For all iterations do the following
        for j in range(N):
            m[j] = step_gradient(X_train, Y_train, learning_rate, m,j)
        a = cost(X_train, Y_train, m)
        x_data.append(i)
        y_data.append(a)
        plt.plot(x_data,y_data,'*')
        print("Cost - : ", i, a)
        plt.draw()
        plt.pause(1e-17)
        time.sleep(0.1)
    return m
def cost(X_train, Y_train, m):
    # This will calculate mean square error
    cost = 0
    n_data_pts = len(X_train)
    N = len(m)
    for i in range(n_data_pts):
        x_i = X_train[i, :]
        y_i = Y_train[i]
        temp_sum = 0
        for k in range(N):
            temp_sum += m[k]*x_i[k]
        temp_sum = y_i - temp_sum
        cost += (1/n_data_pts) * ((temp_sum)**2)
    return cost
def predict(X_test, m):
    n_fts = X_train.shape[1]
    n_m = np.array(m).reshape(n_fts, 1)
    return np.dot(X_test, n_m)
df_test = pd.DataFrame(X_test)
df_test.describe()
def run():
    # Intialize parameters for gradient descent
    num_iterations = 1200
    learning_rate = 0.01
    m = gradient_descent(X_train, Y_train, learning_rate, num_iterations)
    print(m)
    Y_pred = predict(X_test, m)
    np.savetxt('pred.csv', Y_pred, fmt='%.5f', delimiter=',')
run()<jupyter_output>Cost - :  0 291.0192669088228
{'fraction_non_alphanumeric': 0.12867032704702794, 'fraction_numerical': 0.020291238959178802, 'mean_word_length': 3.064015518913676, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '518194', 'license': 'MIT', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1576, 'n_words': 419, 'path': '.ipynb_checkpoints/Boston House Predictor-checkpoint.ipynb', 'repo_name': 'shikharvaish28/Boston-House-Prediction', 'size': '4189'}