<jupyter_start><jupyter_text>Normal Equation<jupyter_code>import numpy as np
import scipy.linalg as la
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
np.set_printoptions(precision=3)<jupyter_output><empty_output><jupyter_text>The solution to the linear regression is $$\theta=(X^TX)^{-1}X^TY$$ 1. Linear Regression with one variable<jupyter_code>data1 = pd.read_csv("ex1data1.txt", header=None, names=['Population', 'Profit'])
data1_copy = data1
data1.plot(kind="scatter", x='Population', y='Profit', marker='x', figsize=(12, 8))
data1.head()
data1.insert(0, 'x0', 1)
data1.head()
col = data1.shape[1]
data1_x = data1.iloc[:, :col - 1].values
data1_y = data1.iloc[:, col - 1:].values.ravel()

print(data1_x.shape)
print(data1_y.shape)
def normal_equation(x, y):
    return la.pinv(x.T @ x) @ x.T @ y
theta1 = normal_equation(data1_x, data1_y)
theta1
x = np.linspace(data1_copy.Population.min(), data1_copy.Population.max(), 100).reshape(100, 1)
x = np.insert(x, 0, values=np.ones(100), axis=1)
y = np.dot(x, theta1)

fig, ax = plt.subplots(figsize=(12, 8))
ax.plot(x[:, 1].flatten(), y.flatten(), 'r', label='Prediction')
ax.scatter(data1_copy.Population, data1_copy.Profit, marker='x', label='Source data')
ax.legend(loc=2)
ax.set_xlabel("Population")
ax.set_ylabel("Profit")
ax.set_title("Normal Equation")<jupyter_output><empty_output><jupyter_text>2. Linear Regression with multiple variables<jupyter_code>data2 = pd.read_csv("ex1data2.txt", header=None, names=['Size', 'Bedroom', 'Price'])
data2.head()
data2.insert(0, 'x0', 1)

col = data2.shape[1]
data2_x = data2.iloc[:, :col - 1].values
data2_y = data2.iloc[:, col - 1:].values.ravel()

print(data2_x.shape)
print(data2_y.shape)
theta2 = normal_equation(data2_x, data2_y)
theta2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17587939698492464, 'fraction_numerical': 0.042992741485203795, 'mean_word_length': 6.929203539823009, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '242618', 'license': 'MIT', 'n_tokens_mistral': 758, 'n_tokens_neox': 724, 'n_words': 136, 'path': 'exercise/ex1_linear_reg/2_normal_equation.ipynb', 'repo_name': 'Zx55/Coursera-AndrewNg-ML', 'size': '1791'}