Skip to content

Instantly share code, notes, and snippets.

Avatar

Bryan Goodrich bryangoodrich

View GitHub Profile
@bryangoodrich
bryangoodrich / LessonOne.R
Created Apr 19, 2016
Adventures in R Training Code
View LessonOne.R
#########################################
# Lesson 1 - R Basics
#
# Learning Objectives
# 1. R Data Types
# 2. Indexing
# 3. Boolean Logic and Filtering
# 4. Importing/Exporting
# 5. The R Environment
#########################################
@bryangoodrich
bryangoodrich / ftp_BLS.R
Last active Aug 29, 2015
Historical FTP Special Request BLS Access
View ftp_BLS.R
################################################################################
# Author: Bryan Goodrich
# Created: Sometime 2012? Earlier?
#
# These functions allowed direct querying and mapping capabilities to the Bureau
# of Labor Statistics (BLS) Special Requests data through their FTP site. The
# data were stored in fixed-width text files.
#
# This code is non-functional in that the BLS no longer supports this FTP access.
# Additionally, the specific Local Area (LA) statistics utilized are also not
@bryangoodrich
bryangoodrich / grid_file.cpp
Created Jul 25, 2015
Input and rotate a grid through designated input format and file import
View grid_file.cpp
/*********************************************************************
* Compilation: g++ grid_file.cpp -o grid_file.exe
* Execution: grid_file.exe input.txt
*
* Input and rotate a grid
*
* This is a toy code snippet for handling informational
* input (integer size of grid), importing multiple
* columns, and exporting with formatted output. This
* example uses C++ approaches to file and string input
@bryangoodrich
bryangoodrich / flip_grid.cpp
Created Jul 24, 2015
Input and rotate a grid through designated input format
View flip_grid.cpp
/*********************************************************************
* Compilation: g++ grid.cpp -o grid.exe
* Execution: grid.exe < input.txt
*
* Input and rotate a grid
*
* This is a toy code snippet for handling informational
* input (integer size of grid), importing multiple
* columns, and exporting with formatted output. Nothing
* amazing, but can provide a basis for this sort of
@bryangoodrich
bryangoodrich / sorted_map.cpp
Created Jul 24, 2015
Sort an incoming map by key length, ties broken by string sorted order
View sorted_map.cpp
/*********************************************************************
* Compilation: g++ -std=c++11 map.cpp -o map.exe
* Execution: map.exe < input.txt
*
* Output the contents of a sorted map
*
* This is a toy code snippet for using C++11 types to handle
* an input stream, store the incoming data columns by line,
* and sort the output list according to length of key,
* with ties broken by string sort order (map default).
@bryangoodrich
bryangoodrich / reverse_input.cpp
Last active Aug 29, 2015
Reverse an integer text input stream.
View reverse_input.cpp
/*********************************************************************
* Compilation: g++ -std=c++11 reverse.cpp -o reverse.exe
* Execution: reverse.exe < input.txt
*
* Reverse an integer text input stream
*
* This is a toy code snippet for using C++11 types to handle
* an input stream, store the incoming data by line, and reverse
* it while printing to standard output.
*
@bryangoodrich
bryangoodrich / TwitterTopics.r
Last active Nov 23, 2020
Twitter Topic Modeling Using R
View TwitterTopics.r
# Twitter Topic Modeling Using R
# Author: Bryan Goodrich
# Date Created: February 13, 2015
# Last Modified: April 3, 2015
#
# Use twitteR API to query Twitter, parse the search result, and
# perform a series of topic models for identifying potentially
# useful topics from your query content. This has applications for
# social media, research, or general curiosity
#
@bryangoodrich
bryangoodrich / PercolationModel.py
Created Mar 17, 2015
Using Union-Find Data Structure to Model Percolation System
View PercolationModel.py
import array
class WeightedQuickUnion:
def __init__(self, N):
self.count = N
self.id = array.array('i', range(N))
self.size = array.array('i', [1] * N)
def connected(self, p, q):
return self.find(p) == self.find(q)
@bryangoodrich
bryangoodrich / RegularizedGradientDescent.R
Created Aug 26, 2014
Regularized Gradient Descent Logistic Classifier with Decision Boundary
View RegularizedGradientDescent.R
loadInput <- function() {
structure(c(0.051267, -0.092742, -0.21371, -0.375, -0.51325,
-0.52477, -0.39804, -0.30588, 0.016705, 0.13191, 0.38537, 0.52938,
0.63882, 0.73675, 0.54666, 0.322, 0.16647, -0.046659, -0.17339,
-0.47869, -0.60541, -0.62846, -0.59389, -0.42108, -0.11578, 0.20104,
0.46601, 0.67339, -0.13882, -0.29435, -0.26555, -0.16187, -0.17339,
-0.28283, -0.36348, -0.30012, -0.23675, -0.06394, 0.062788, 0.22984,
0.2932, 0.48329, 0.64459, 0.46025, 0.6273, 0.57546, 0.72523,
0.22408, 0.44297, 0.322, 0.13767, -0.0063364, -0.092742, -0.20795,
-0.20795, -0.43836, -0.21947, -0.13882, 0.18376, 0.22408, 0.29896,
@bryangoodrich
bryangoodrich / GradientDescent.R
Last active Aug 29, 2015
Gradient Descent for Logistic Classifier
View GradientDescent.R
gradientDescent <- function(X, y, initial_theta, method = "BFGS", ...) {
m <- nrow(y)
sigmoid <- function(x) 1 / (1 + exp(-x))
gradFunction <- function(theta) (1/m) * (t(X) %*% (sigmoid(X %*% theta)-y))
costFunction <- function(theta)
(1/m) * (t(-y) %*% log(sigmoid(X %*% theta)) - t(1-y) %*% log(1 - sigmoid(X %*% theta)))
optim(initial_theta, costFunction, gradFunction, method = method, ...)
}