<jupyter_start><jupyter_text>CSAL4243: Introduction to Machine Learning<NAME> (<EMAIL>)  Lecture 3: Gradient Descent  Overview - [What is Machine Learning?](what-is-ml)- [The three different types of machine learning](The-three-different-types-of-machine-learning)    - [Supervised Learning](Supervised-Learning)- [Machine Learning pipeline](Machine-Learning-pipeline)- [Goal of Machine Learning algorithm](Goal-of-Machine-Learning-algorithm)- [ Linear Regression with one variable](Linear-Regression-with-one-variable)    - [Model Representation](Model-Representation)    - [Cost Function](Cost-Function)- [Gradient Descent](Gradient-Descent)    - [Gradient Descent Equation](Gradient-Descent-Equation)    - [Derivate Part](Derivate-Part)    - [Learning Rate $\alpha$](Learning-Rate-$\alpha$)    - [Convergence of Gradient Descent](Convergence-of-Gradient-Descent)- [Gradient Descent for Linear Regression](Gradient-Descent-for-Linear-Regression)    - [Deriving $\frac{\partial}{\partial \theta_j} J(\theta_0, \theta_1)$](Deriving-$\frac{\partial}{\partial \theta_j} J(\theta_0, \theta_1)$)- [Price of the house yet again?](Price-of-the-house-yet-again?)    - [Read data](Read-data)    - [Plot data](Plot-data)    - [Initialize Hyper Parameters](Initialize Hyper Parameters)    - [Model/Hypothesis Function](Model/Hypothesis-Function)    - [Cost Function](Cost-Function)    - [Gradient Descent](Gradient-Descent)    - [Run Gradient Descent](Run-Gradient-Descent)    - [Plot Convergence](Plot-Convergence)    - [Predict output using trained model](Predict-output-using-trained-model)    - [Plot Results](Plot-Results)- [Resources](Resources)- [Credits](Credits)   What is Machine Learning?   * Machine Learning is making computers/machcines learn from data* Learning improve over time with more data   The three different types of machine learning    Supervised Learning    Machine Learning pipeline  - x is called input variables or input features.- y is called output or target variable. Also sometimes known as label.- h is called hypothesis or model. - pair (x(i),y(i)) is called a sample or training example- dataset of all training examples is called training set.- m is the number of samples in a dataset.- n is the number of features in a dataset excluding label.     Goal of Machine Learning algorithm- How well the algorithm will perform on unseen data. - Also called generalization.   Linear Regression with one variable  Model Representation- Model is represented by h$\theta$(x) or simply h(x)- For Linear regression with one input variable h(x) =  $\theta$0 + $\theta$1x - $\theta$0 and $\theta$1 are called weights or parameters.- Need to find $\theta$0 and $\theta$1 that maximizes the performance of model.    Cost Function Let $\hat{y}$ = h(x) =  $\theta$0 + $\theta$1xError in single sample (x,y) = $\hat{y}$ - y = h(x) - y Cummulative error of all m samples = $\sum_{i=1}^{m} (h(x^i) - y^i)^2$Finally mean error or cost function = J($\theta$) = $\frac{1}{2m}\sum_{i=1}^{m} (h(x^i) - y^i)^2$    Gradient Descent  Gradient Descent Equation- $\alpha$ is called learning rate that control the step size.  Derivative Part  Learning Rate $\alpha$  Convergence of Gradient Descent --- Gradient Descent for Linear RegressionCost function:J($\theta$) = $\frac{1}{2m}\sum_{i=1}^{m} (h(x^i) - y^i)^2$Gradient descent equation:$\theta_j := \theta_j - \alpha \frac{\partial}{\partial \theta_j} J(\theta_0, \theta_1)$--- Replacing J($\theta$) for each j\begin{align*} \text{repeat until convergence: } \lbrace & \newline \theta_0 := & \theta_0 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m}(h_\theta(x_{i}) - y_{i}) \newline \theta_1 := & \theta_1 - \alpha \frac{1}{m} \sum\limits_{i=1}^{m}\left((h_\theta(x_{i}) - y_{i}) x_{i}\right) \newline \rbrace& \end{align*}---  Deriving $\frac{\partial}{\partial \theta_j} J(\theta_0, \theta_1)$---   Price of the house yet again?   Read data<jupyter_code>%matplotlib inline
import pandas as pd
import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt

# read data in pandas frame
dataframe = pd.read_csv('datasets/house_dataset1.csv')

# assign x and y
X = np.array(dataframe[['Size']])
y = np.array(dataframe[['Price']])

m = y.size # number of training examples
#Insert the usual column of 1's into the "X" matrix
X = np.insert(X,0,1,axis=1)
# check data by printing first few rows
dataframe.head()<jupyter_output><empty_output><jupyter_text>Plot data<jupyter_code>#visualize results
plt.scatter(X[:,1], y)
plt.show()<jupyter_output><empty_output><jupyter_text>Initialize Hyper Parameters<jupyter_code>iterations = 1500
alpha = 0.000000001<jupyter_output><empty_output><jupyter_text>Model/Hypothesis Function<jupyter_code>def h(theta,X): #Linear hypothesis function
    return np.dot(X,theta)<jupyter_output><empty_output><jupyter_text>Cost Function<jupyter_code>def computeCost(mytheta,X,y): #Cost function
    """
    theta_start is an n- dimensional vector of initial theta guess
    X is matrix with n- columns and m- rows
    y is a matrix with m- rows and 1 column
    """
    #note to self: *.shape is (rows, columns)
    return float((1./(2*m)) * np.dot((h(mytheta,X)-y).T,(h(mytheta,X)-y)))

#Test that running computeCost with 0's as theta returns 65591548106.45744:
initial_theta = np.zeros((X.shape[1],1)) #(theta is a vector with n rows and 1 columns (if X has n features) )
print (computeCost(initial_theta,X,y))<jupyter_output>65591548106.45744<jupyter_text>Gradient Descent Function<jupyter_code>#Actual gradient descent minimizing routine
def descendGradient(X, theta_start = np.zeros(2)):
    """
    theta_start is an n- dimensional vector of initial theta guess
    X is matrix with n- columns and m- rows
    """
    theta = theta_start
    jvec = [] #Used to plot cost as function of iteration
    thetahistory = [] #Used to visualize the minimization path later on
    for meaninglessvariable in range(iterations):
        tmptheta = theta
        # append for plotting
        jvec.append(computeCost(theta,X,y))
        thetahistory.append(list(theta[:,0]))
        #Simultaneously updating theta values
        for j in range(len(tmptheta)):
            tmptheta[j] = theta[j] - (alpha/m)*np.sum((h(theta,X) - y)*np.array(X[:,j]).reshape(m,1))
        theta = tmptheta
    return theta, thetahistory, jvec<jupyter_output><empty_output><jupyter_text>Run Gradient Descent<jupyter_code>#Actually run gradient descent to get the best-fit theta values
initial_theta = np.zeros((X.shape[1],1))
theta, thetahistory, jvec = descendGradient(X,initial_theta)

thetahistory<jupyter_output><empty_output><jupyter_text>Plot Convergence<jupyter_code>plt.plot(jvec)
plt.title("Convergence of Cost Function")
plt.xlabel("Iteration number")
plt.ylabel("Cost function")
plt.show()<jupyter_output><empty_output><jupyter_text>Predict output using trained model<jupyter_code>hx = h(theta, X)<jupyter_output><empty_output><jupyter_text>Plot Results<jupyter_code>plt.scatter(X[:,1], y)
plt.plot(X[:,1], hx)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13411161731207288, 'fraction_numerical': 0.01494874715261959, 'mean_word_length': 4.772391125718981, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '132282', 'license': 'MIT', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2218, 'n_words': 638, 'path': 'lectures/lec03-gradient-descent.ipynb', 'repo_name': 'w4zir/ml17s.github.io', 'size': '7058'}