Skip to content

Instantly share code, notes, and snippets.

View mGalarnyk's full-sized avatar

Michael Galarnyk mGalarnyk

View GitHub Profile
@mGalarnyk
mGalarnyk / Generate Jupyter Notebook Config
Last active December 26, 2016 03:29
Generate config for jupyter notebook. The purpose of this gist is so people can copy and paste from here to their terminal. My blog https://medium.com/@GalarnykMichael
jupyter notebook --generate-config
mkdir certs
cd certs
sudo openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
cd ~/.jupyter/
nano jupyter_notebook_config.py
c = get_config()
c.IPKernelApp.pylab = 'inline'
c.NotebookApp.certfile = u'/home/ubuntu/certs/mycert.pem'
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
# Your password below will be whatever you copied earlier
c.NotebookApp.password = u'sha1:941c93244463:0a28b4a9a472da0dc28e79351660964ab81605ae'
c.NotebookApp.port = 8888
# Install R + RStudio on Ubuntu 12.04
sudo apt-key adv –keyserver keyserver.ubuntu.com –recv-keys E084DAB9
# Ubuntu 12.04: precise
# Ubuntu 14.04: trusty
# Ubuntu 16.04: xenial
# Basic format of next line deb https://<my.favorite.cran.mirror>/bin/linux/ubuntu <enter your ubuntu version>/
sudo add-apt-repository 'deb https://ftp.ussg.iu.edu/CRAN/bin/linux/ubuntu precise/'
sudo apt-get update
@mGalarnyk
mGalarnyk / R_Programming.R
Last active December 31, 2016 17:55
R programming Coursera John Hopkins Week 1 Review Suggestion Showing data.table functionality https://medium.com/@GalarnykMichael/in-progress-review-course-2-r-programming-jhu-coursera-ad27086d8438#.vclier2hw
# Reading in data
quiz_data <- fread('hw1_data.csv')
# Column names of the dataset
names(quiz_data)
# First two rows
quiz_data[c(1,2)]
# Number of rows in data.table
@mGalarnyk
mGalarnyk / pollutantmean.R
Last active January 2, 2017 16:11
pollutantmean.R This file is used for the John Hopkins Data Science Specialization (R Programming). This file is posted for the blog post reviewing the specialization https://medium.com/@GalarnykMichael/in-progress-review-course-2-r-programming-jhu-coursera-ad27086d8438#.ui3hb8n46
pollutantmean <- function(directory, pollutant, id = 1:332) {
# Format number with fixed width and then append .csv to number
fileNames <- paste0(directory, '/', formatC(id, width=3, flag="0"), ".csv" )
# Reading in all files and making a large data.table
lst <- lapply(fileNames, data.table::fread)
dt <- rbindlist(lst)
if (c(pollutant) %in% names(dt)){
@mGalarnyk
mGalarnyk / complete.R
Last active January 2, 2017 16:25
complete.R This file is used for the John Hopkins Data Science Specialization (R Programming). This file is posted for the blog post reviewing the specialization https://medium.com/@GalarnykMichael/in-progress-review-course-2-r-programming-jhu-coursera-ad27086d8438#.ui3hb8n46
complete <- function(directory, id = 1:332) {
# Format number with fixed width and then append .csv to number
fileNames <- paste0(directory, '/', formatC(id, width=3, flag="0"), ".csv" )
# Reading in all files and making a large data.table
lst <- lapply(fileNames, data.table::fread)
dt <- rbindlist(lst)
return(dt[complete.cases(dt), .(nobs = .N), by = ID])
@mGalarnyk
mGalarnyk / corr.R
Last active January 2, 2017 21:26
corr.R This file is used for the John Hopkins Data Science Specialization (R Programming). This file is posted for the blog post reviewing the specialization https://medium.com/@GalarnykMichael/in-progress-review-course-2-r-programming-jhu-coursera-ad27086d8438#.38n89lga5
corr <- function(directory, threshold = 0) {
# Reading in all files and making a large data.table
lst <- lapply(file.path(directory, list.files(path = directory, pattern="*.csv")), data.table::fread)
dt <- rbindlist(lst)
# Only keep completely observed cases
dt <- dt[complete.cases(dt),]
# Apply threshold
export SPARK_PATH=~/spark-1.6.0-bin-hadoop2.6
export PYSPARK_DRIVER_PYTHON="jupyter"
export PYSPARK_DRIVER_PYTHON_OPTS="notebook"
#For python 3, You have to add the line below or you will get an error
# export PYSPARK_PYTHON=python3
alias snotebook='$SPARK_PATH/bin/pyspark --master local[2]'
import numpy as np
# Solving following system of linear equation
# 1a + 1b = 35
# 2a + 4b = 94
a = np.array([[1, 1],[2,4]])
b = np.array([35, 94])
print(np.linalg.solve(a,b))
library(datasets)
library(data.table)
iris_dt <- as.data.table(iris)
# 1 There will be an object called 'iris' in your workspace.
# In this dataset, what is the mean of 'Sepal.Length' for the species virginica? Please round your answer to the nearest whole number.
# Basic data.table syntax below .
#iris_dt[ essentially SQL Where class, select statement, groupby]
iris_dt[Species == "virginica",round(mean(Sepal.Length)) ]