Skip to content

Instantly share code, notes, and snippets.

@smc77
smc77 / multivariate_grad_descent.R
Created October 23, 2011 21:32
Multivariate Gradient Descent
# Load data and initialize values
data <- read.csv("http://www.statalgo.com/wp-content/uploads/2011/10/housing.csv")
num.iterations <- 1000
x <- data[, c("area", "bedrooms")]
y <- matrix(data$price, ncol=1) / 1000 # Divide by a thousand so that numbers are in $1000's
# Function to standardize input values
zscore <- function(x, mean.val=NA) {
@smc77
smc77 / multivariate
Created October 22, 2011 23:44
Multivariate Regression
# details about dataset available http://archive.ics.uci.edu/ml/datasets/Housing
housing <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data")
names(housing) <- c("CRIM", "ZN", "INDUS", "CHAS", "NOX", "RM", "AGE", "DIS", "RAD", "TAX", "PTRATIO", "B", "LSTAT", "MEDV")
# Subset the data for our model
housing <- housing[, c("CRIM", "RM", "PTRATIO", "LSTAT", "MEDV")]
plot(housing)
@smc77
smc77 / linear algebra in R
Created October 20, 2011 01:32
Quick linear algebra demo
# Matrix addition
matrix(c(1, 2, 3, 0, 5, 1), ncol=2) + matrix(c(4, 2, 0, 0.5, 5, 1), ncol=2)
# Matrix multiplication
3 * matrix(c(1, 2, 3, 0, 5, 1), ncol=2)
# Matrix-Vector Multiplication
matrix(c(1, 4, 2, 3, 0, 1), ncol=2) %*% c(1, 5)
# Matrix-Mector Multiplication
@smc77
smc77 / gist:1291757
Created October 17, 2011 01:53
Linear regression gradient descent.
# Load data and initialize values
data <- read.csv("http://www.statalgo.com/wp-content/uploads/2011/10/housing.csv")
alpha <- 0.01
m <- nrow(data)
x <- matrix(c(rep(1,m), data$area), ncol=2)
y <- matrix(data$price, ncol=1) / 1000
# Z-Score the feature
x.scaled <- x
@smc77
smc77 / intuitive_regression
Created October 5, 2011 02:01
Fitting various random lines to the housing data to get an intuition about the loss function.
# Example of randomly chosen lines
plot(housing)
abline(0, 5, col="red")
abline(-50, 10, col="blue")
# Create the loss function
loss <- function(intercept, slope) sum(((intercept + (slope * housing[, "num.rooms"])) - housing[, "median.values"])^2)/2
# Create some data for a given line and compute the loss
loss(0, 5)
@smc77
smc77 / cs229_univariate_regression
Created October 3, 2011 00:28
Univariate regression with housing data
# First look at a linear model fit to the housing data
# details about dataset available http://archive.ics.uci.edu/ml/datasets/Housing
housing <- read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data")[, c(6, 14)]
names(housing) <- c("num.rooms", "median.values")
housing.lm <- lm(median.values ~ num.rooms, data=housing)
plot(housing)
abline(housing.lm)
summary(housing.lm)