<jupyter_start><jupyter_text>Linear Regression using Numpy  Linear Regression with One Variable<jupyter_code>%matplotlib inline
# To get nice quality plots
%config InlineBackend.figure_format = 'svg'

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd<jupyter_output><empty_output><jupyter_text>Read Data from Dataset<jupyter_code># Read data from dataset
data = pd.read_csv('data/ex1data1.txt', names = ["Population", "Food Truck Profit"])
x = data.iloc[:, 0] # first column
y = data.iloc[:, 1] # second column
m = len(y)

# Show first few examples
print(f"Number of samples: {m}")
data.head()<jupyter_output>Number of samples: 97<jupyter_text>Plot Data<jupyter_code># Plot data
plt.scatter(x, y, marker='x', c = 'r', s = 20)
plt.ylabel('Profit in $10,000s')
plt.xlabel('Population of City in 10,000s')
plt.show()<jupyter_output><empty_output><jupyter_text>Model<jupyter_code># Change x and y from 1d array to 2d array (column vector)
X = x[:, np.newaxis]
y = y[:, np.newaxis]

# Add intercept term to X
onecol = np.ones((m, 1))
X = np.hstack((onecol, X))
print(f"X shape: {X.shape}, y shape: {y.shape}")
# Set parameters to zero:
theta = np.zeros((2, 1))
print(f"theta shape: {theta.shape}")<jupyter_output>theta shape: (2, 1)<jupyter_text>$$X = \begin{pmatrix}1 & x^{(1)} \\ \vdots & \vdots \\ 1 & x^{(m)}\end{pmatrix}, \quady = \begin{pmatrix}y^{(1)} \\ \vdots \\ y^{(m)}\end{pmatrix}, \quad\theta = \begin{pmatrix} \theta_0 \\ \theta_1\end{pmatrix}$$<jupyter_code># Define parameters for gradient descent:
## Number of iterations
iterations = 1500
## Learning rate
alpha = 0.01<jupyter_output><empty_output><jupyter_text>\begin{align*}&\text{Hypothesis:} && h_{\theta}(x^{(i)}) = \theta_0 + \theta_1 x^{(i)} \\&\text{Cost Function:} && J(\theta_0, \theta_1) = \frac{1}{2m}\sum_{i=1}^{m} (h_{\theta}(x^{(i)}) - y^{(i)})^2 = \frac{1}{2m}(X \theta - y)^T(X\theta - y)\end{align*}<jupyter_code># Cost function
def computeCost(X, y, theta):
    """ 
    Computes the cost-function value of the input data
    """
    m = len(y)
    res = X @ theta - y
    return np.sum(res ** 2) / (2*m)
# Compute initial cost (should be about 32.07)
computeCost(X, y, theta)<jupyter_output><empty_output><jupyter_text>\begin{align*}\text{Gradient descent update step:} && \theta = \theta - \frac{\alpha}{m} X^T (X \theta - y)\end{align*}<jupyter_code>def gradientDescent(X, y, theta, alpha, num_iters):
    """
    Performs gradient descent num_iters times with step size alpha
    """
    m = len(y)
    J_history = np.zeros(num_iters)
    for i in range(num_iters):
        theta = theta - (alpha/m) * X.T @ (X @ theta - y)
        J_history[i] = computeCost(X, y, theta)
    return theta, J_history
final_theta, J_history = gradientDescent(X, y, theta, alpha, iterations)
# Plot the progress of the minimization of the cost function through the gradient descent method.
plt.plot(range(2, iterations), J_history[2:])
plt.ylabel('Cost')
plt.xlabel('#Iterations')
plt.show()
# The cost, using the learned parameters for the linear model is:
computeCost(X, y, final_theta)
# Finally lets plot the data points vs the linear regression line:
plt.scatter(X[:, 1], y, marker='x', c = 'r', s = 20)
plt.ylabel('Profit in $10,000s')
plt.xlabel('Population of City in 10,000s')
plt.plot(X[:, 1], X @ final_theta)
plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression with Multiple Variables  Read Data from Dataset<jupyter_code>data2 = pd.read_csv('data/ex1data2.txt', sep = ",", names = ["Size(sqft)", "Nr. Bedrooms", "Price"])
x2 = data2.iloc[:, 0:2]
y2 = data2.iloc[:, 2]
m2 = len(y2)

print(f"Number of samples: {m2}")
data2.head()
def featureNormalize(x):
    mu = np.mean(x)
    std = np.std(x)
    return (x - mu)/std, mu, std
# Perform feature normalization:
x2_normalized, mu, std = featureNormalize(x2)<jupyter_output><empty_output><jupyter_text>Model (Iterative)<jupyter_code># Change y from 1d array to 2d array (column vector)
y2 = y2[:, np.newaxis]

# Add intercept term to X
onecol = np.ones((m2, 1))
X2 = np.hstack((onecol, x2_normalized))

theta2 = np.zeros((3,1))
# Define parameters
alpha2 = 0.01
num_iters2 = 400
# To compute cost we can use the already implemented function computeCost:
computeCost(X2, y2, theta2)
# We use gradient descent to calculate the optimal parameter vector theta:
final_theta2, J_history2 = gradientDescent(X2, y2, theta2, alpha2, num_iters2)
# Plot the progress of the minimization of the cost function through the gradient descent method.
plt.plot(range(num_iters2), J_history2)
plt.ylabel('Cost')
plt.xlabel('#Iterations')
plt.show()
# Final Theta:
final_theta2
# Final Cost:
computeCost(X2, y2, final_theta2)
from mpl_toolkits.mplot3d import Axes3D
# Plot features against the linear regression line
fig = plt.figure()
ax = Axes3D(fig)

ax.scatter3D(X2[:, 0], X2[:, 1], y2)
ax.plot3D(X2[:,0], X2[:,1], (X2 @ final_theta2).flatten(), c='r')
ax.set_xlabel('Normalized House Size')
ax.set_ylabel('Normalized Nr. Bedrooms')
ax.set_zlabel('House Price')
plt.show()
# Predict price for a 3 bedroom 1650 sqft house:
price = np.inner(np.array([1, (1650 - mu[0])/std[0], (3 - mu[1])/std[1]]), final_theta2.T)[0]
print(f"Predicted price for a 3 bedroom, 1650 sqft house: {price}$")<jupyter_output>Predicted price for a 3 bedroom, 1650 sqft house: 289221.5473712181$<jupyter_text>Model (direct, using Normal Equation) \begin{align*}\theta = (X^T X)^{\dagger} X^T y \end{align*}*Note:* For a matrix $A \in \mathbb{R}^{mxn}$, $A^{\dagger} \in \mathbb{R}^{nxm}$ denotes its [Moore-Penrose pseudo-inverse](https://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_inverse). It is a generalized inverse and provides a least squares solution to a linear equation system$A x = b$.It has the following properties: * For all $x \in \mathbb{R}^n$ it holds that $\|Ax - b\|_2 \geq \| Az - b \|_2$, where $z = A^{\dagger} b$.* If $A$ is invertible, then $A^{\dagger} = A^{-1}$.<jupyter_code># Add intercept term to X
X2_intercept = np.hstack((onecol, x2))

# Directly calculate theta
theta_final2_normal = np.linalg.pinv(X2_intercept.T @ X2_intercept) @ X2_intercept.T @ y2
# Predict price for a 3 bedroom 1650 sqft house using the result of the normal equation:
price_normal = np.inner(np.array([1, 1650, 3]), theta_final2_normal.T)[0]
print(f"Predicted price for a 3 bedroom, 1650 sqft house: {price_normal}$")<jupyter_output>Predicted price for a 3 bedroom, 1650 sqft house: 293081.4643349717$
{'fraction_non_alphanumeric': 0.1593594527363184, 'fraction_numerical': 0.036380597014925374, 'mean_word_length': 5.086092715231788, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 1, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '571110', 'license': 'MIT', 'n_tokens_mistral': 2533, 'n_tokens_neox': 2368, 'n_words': 666, 'path': 'exercises/ex1/Linear_Regression_Numpy.ipynb', 'repo_name': 'mfl28/CourseraMachineLearning', 'size': '6432'}