<jupyter_start><jupyter_text>Linear Regression using Gradient Descent  Import Packages<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Import Data set<jupyter_code>train_data = pd.read_csv('apartment_data_train.csv')
train_data.head()
train_data.info()
train_data.describe()
test_data = pd.read_csv('apartment_data_test.csv')
test_data.head()<jupyter_output><empty_output><jupyter_text>Feature Separation and Normalization<jupyter_code>X_train = train_data.iloc[:, :-1].values
X_test = test_data.iloc[:, :].values
y_train = train_data.iloc[:, -1:].values
m = y_train.shape[0]
train_data_mean = train_data.mean(0)
train_data_std = train_data.std(0)
X_train_mean = train_data_mean[0:2].values
X_train_std = train_data_std[0:2].values
X_train_norm = (X_train - X_train_mean) / X_train_std
X_test_norm = (X_test - X_train_mean) / X_train_std<jupyter_output><empty_output><jupyter_text>Add Intercept Term to Features<jupyter_code>train_ones = np.ones((X_train_norm.shape[0], 1))
test_ones = np.ones((X_test_norm.shape[0], 1))
X_train_norm = np.column_stack((train_ones, X_train_norm))
X_test_norm = np.column_stack((test_ones, X_test_norm))<jupyter_output><empty_output><jupyter_text>Perform Gradient Descent  Compute Cost<jupyter_code>def compute_cost(X, y, theta):
    hx = np.matmul(X, theta)
    error_values = hx - y
    squared_error = np.square(error_values)
    cost = np.sum(squared_error, 0)
    return cost<jupyter_output><empty_output><jupyter_text>Gradient Descent<jupyter_code>def gradient_descent(X, y, theta, alpha, iterations):
    iteration_array = np.array([itr for itr in range(iterations)])
    cost_history = []
    for iteration in range(iterations):
        hx = np.matmul(X, theta)
        error_value = hx - y
        error_value_multi = np.matmul(error_value.T, X)
        delta = np.multiply(error_value_multi.T, (alpha / m))
        theta = theta - delta
        cost_history.append(compute_cost(X, y, theta))
    return [theta, np.column_stack((iteration_array, np.asarray(cost_history)))]<jupyter_output><empty_output><jupyter_text>Use Gradient Descent<jupyter_code>alpha = 0.01
num_iterations = 400
theta = np.zeros((X_train_norm.shape[1], 1))
theta, cost_history_result = gradient_descent(X_train_norm, y_train, theta, alpha, num_iterations)
result = np.matmul(X_test_norm, theta)
print(result)<jupyter_output>[[289314.62033778]]<jupyter_text>Cost Function<jupyter_code>plt.plot(cost_history_result)
plt.xlabel("Iteration")
plt.ylabel("Cost")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12239382239382239, 'fraction_numerical': 0.013899613899613899, 'mean_word_length': 6.665680473372781, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, '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': '545996', 'license': 'Apache-2.0', 'n_tokens_mistral': 1019, 'n_tokens_neox': 985, 'n_words': 154, 'path': 'supervised/regression/linearregression/linear_regression.ipynb', 'repo_name': 'adityakamble49/ml-algorithms', 'size': '2590'}