Skip to content

Instantly share code, notes, and snippets.

View csrsen's full-sized avatar

Craig Srsen csrsen

View GitHub Profile
@csrsen
csrsen / garbage file generator.sh
Created January 8, 2019 00:40
Garbage file generator
# generate a 10MB file named `testfile` filled with random stuff
dd if=/dev/urandom of=testfile bs=1000000 count=10
@csrsen
csrsen / linear_regression.py
Created October 24, 2018 20:30
Linear Regression
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression
data = pd.read_csv('data.csv')
X = data.iloc[:, 0].values.reshape(-1, 1) # values converts it into a numpy array
Y = data.iloc[:, 1].values.reshape(-1, 1) # -1 means that calculate the dimension of rows, but have 1 column
linear_regressor = LinearRegression()
linear_regressor.fit(X, Y) # perform linear regression
@csrsen
csrsen / iPython keybd shortcuts.md
Created October 5, 2018 15:13
IPython keyboard shortcuts
Shortcut Description
Ctrl-P or up-arrow  Search backward in command history for commands starting with currently-entered text
Ctrl-N or down-arrow  Search forward in command history for commands starting with currently-entered text
Ctrl-R  Readline-style reverse history search (partial matching)
Ctrl-Shift-V Paste text from clipboard
Ctrl-C  Interrupt currently-executing code
Ctrl-A Move cursor to beginning of line
Ctrl-E  Move cursor to end of line
Ctrl-K  Delete text from cursor until end of line
@csrsen
csrsen / iPython System Commands.md
Created October 5, 2018 15:12
IPython system commands

|iPython System Commands| |:---|:---| |!cmd |Execute cmd in the system shell | |output = !cmd args |Run cmd and store the stdout in output | |%alias alias_name cmd |Define an alias for a system (shell) command| |%bookmark |Utilize IPython’s directory bookmarking system | |%cd directory |Change system working directory to passed directory | |%pwd |Return the current system working directory | |%pushd directory |Place current directory on stack and change to target directory | |%popd |Change to directory popped off the top of the stack |

@csrsen
csrsen / NumPy set funcs.md
Created October 5, 2018 15:11
NumPy set funcs
Function Description
unique(x) Compute the sorted, unique elements in x
intersect1d(x, y)  Compute the sorted, common elements in x and y
union1d(x, y)  Compute the sorted union of elements
in1d(x, y)  Compute a boolean array indicating whether each element of x is contained in y
setdiff1d(x, y)  Set difference, elements in x that are not in y
setxor1d(x, y)  Set symmetric differences; elements that are in either of the arrays, but not both
@csrsen
csrsen / NumPy ufuncs.md
Created October 5, 2018 15:10
NumPy ufuncs
Function Description
abs, fabs  Compute the absolute value element-wise for integer, floating point, or complex values.
Use fabs as a faster alternative for non-complex-valued data
sqrt  Compute the square root of each element. Equivalent to arr ** 0.5
square  Compute the square of each element. Equivalent to arr ** 2
exp  Compute the exponent ex of each element
log, log10, log2, log1p  Natural logarithm (base e), log base 10, log base 2, and log(1 + x), respectively
sign  Compute the sign of each element: 1 (positive), 0 (zero), or -1 (negative)
ceil  Compute the ceiling of each element, i.e. the smallest integer greater than or equal to each element
@csrsen
csrsen / geopy_sample.py
Created September 26, 2018 20:36
Geopy geocoding
from geopy import GoogleV3
place = "221b Baker Street, London"
location = GoogleV3().geocode(place)
print(location.address)
print(location.location)
@csrsen
csrsen / concurrent_preprocessing.py
Created September 26, 2018 16:26
Concurrent preprocessing
import glob
import os
import cv2
import concurrent.futures
def load_and_resize(image_filename):
### Read in the image data
img = cv2.imread(image_filename)
@csrsen
csrsen / HWES.py
Created September 25, 2018 05:13
Holt Winters Exponential Smoothing
# HWES example
from statsmodels.tsa.holtwinters import ExponentialSmoothing
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = ExponentialSmoothing(data)
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data))
@csrsen
csrsen / SES.py
Created September 25, 2018 05:11
Simple Exponential Smoothing
# SES example
from statsmodels.tsa.holtwinters import SimpleExpSmoothing
from random import random
# contrived dataset
data = [x + random() for x in range(1, 100)]
# fit model
model = SimpleExpSmoothing(data)
model_fit = model.fit()
# make prediction
yhat = model_fit.predict(len(data), len(data))