Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / caret.R
Created August 7, 2015 21:24
Example of training a glm model on a spam data-set, using the caret library.
# Example of training a glm model on a spam data-set, using the caret library.
library(caret)
library(kernlab)
# Load spam dataset.
data(spam)
# Split the data into a training/test set by 60% training/40% test.
inTrain <- createDataPartition(y = spam$type, p=0.6, list=FALSE)
training <- spam[inTrain,]
@primaryobjects
primaryobjects / predict.R
Created August 8, 2015 02:11
Predictive classification example with R. Machine Learning examples.
library(AppliedPredictiveModeling)
library(caret)
data(AlzheimerDisease)
adData = data.frame(diagnosis,predictors)
trainIndex = createDataPartition(diagnosis, p = 0.60,list=FALSE)
training = adData[trainIndex,]
testing = adData[-trainIndex,]
@primaryobjects
primaryobjects / mnist.R
Last active September 17, 2018 01:27
MNIST machine learning example in R.
library(caret)
library(doParallel)
# Enable parallel processing.
cl <- makeCluster(detectCores())
registerDoParallel(cl)
# Load the MNIST digit recognition dataset into R
# http://yann.lecun.com/exdb/mnist/
# assume you have all 4 files and gunzip'd them
@primaryobjects
primaryobjects / mnist-learning-curve.R
Created August 8, 2015 15:45
MNIST machine learning example, plotting a learning curve.
library(caret)
library(doParallel)
library(reshape2)
library(ggplot2)
# Enable parallel processing.
cl <- makeCluster(detectCores())
registerDoParallel(cl)
# Load the MNIST digit recognition dataset into R
@primaryobjects
primaryobjects / regression-machine-learning.R
Created August 10, 2015 03:28
Predicting with Regression in R.
library(caret)
data(faithful)
set.seed(333)
# Plot data.
plot(x=faithful$waiting, faithful$eruptions)
# Calculate linear model.
@primaryobjects
primaryobjects / q3.R
Last active October 4, 2015 02:21
Practical Machine Learning Quiz 3.
# Question 1
library(AppliedPredictiveModeling)
library(caret)
data(segmentationOriginal)
set.seed(125)
#inTrain <- createDataPartition(segmentationOriginal$Case, list=FALSE)
inTrain <- data$Case == "Train"
# Quiz 4
# Question 1.
library(ElemStatLearn)
library(randomForest)
library(caret)
data(vowel.train)
data(vowel.test)
@primaryobjects
primaryobjects / example-chart-sma-SPY.png
Last active August 29, 2015 14:27
Forecasting stock charts in R.
example-chart-sma-SPY.png
@primaryobjects
primaryobjects / quickSort.R
Created September 5, 2015 02:59
Quick sort and Selection sort algorithm in R.
# Simple implementation of Selection Sort and Quicksort in R.
# Quick sort algorithm:
# 1. Select a random value from the array.
# 2. Put all values less than the random in arrayLeft.
# 3. Put all values greater than the random in arrayRight.
# 4. If arrayLeft or arrayRight has more than 1 value, repeat the above steps on it.
# 5. The sorted result is arrayLeft, random, arrayRight.
#
# Selection sort algorithm:
# 1. Find the smallest value in the array and move it to a result array.
@primaryobjects
primaryobjects / mapReduce.R
Last active December 8, 2020 22:03
Map Reduce example in R. Demonstrates applying a map and reduce function to an input array, resulting in a hash of key/value pairs. This example maps a method to find prime divisors of each input value and then reduces the result by summing the input value for each prime divisor. See demo at http://www.r-fiddle.org/#/fiddle?id=tyir23SG&version=1
is.prime <- function(num) {
if (num == 2) {
TRUE
} else if (any(num %% 2:(num-1) == 0)) {
FALSE
} else {
TRUE
}
}