Skip to content

Instantly share code, notes, and snippets.

@filipwodnicki
filipwodnicki / binomial-distribution.r
Created August 30, 2017 16:44
R Tutorial For Beginners
# Here we view that many random trials result in a Gaussian Distribution.
# first we create random vector and resize it be a matrix
d2 <- runif(4000,-1,2)
dim(d2) <- c(100,40)
#wanna see my d2?
head(d2)
#now we bind the a new column rowsum to the original matrix (Column 41!)
pip install --upgrade pip
pip install jupyter
@filipwodnicki
filipwodnicki / heroku-dump-pg-restore.sh
Last active June 10, 2019 14:06
use heroku database dump to populate local database
# Download heroku database dump and run pg restore on it to populate local db
## Source: https://medium.com/coding-blocks/creating-user-database-and-adding-access-on-postgresql-8bfcd2f4a91e
## link: https://data.heroku.com/datastores/
# prep: download the heroku dump from heroku > Databases > durability
psql
postgres=# drop database if exists replace_this_with_your_database_name;
postgres=# create database replace_this_with_your_database_name;
@filipwodnicki
filipwodnicki / install_macos.sh
Created June 10, 2019 18:30
install libpostal mac
# install libpostal v1.1-alpha. Environment: MacOS 10.14 Python 3.6
# libpostal library instructions didn't work for me, got error:
# ERR Error loading transliteration module, dir=(null) at libpostal_setup_datadir (libpostal.c:266) errno: No such file or directory
# Here is a workaround:
git clone https://github.com/openvenues/libpostal
cd libpostal
./bootstrap.sh
@filipwodnicki
filipwodnicki / how_to_named_tuple.py
Last active June 13, 2019 16:32
How to use Named Tuples in Python
# source: https://pymotw.com/2/collections/namedtuple.html
# doc: https://docs.python.org/3.7/library/collections.html#collections.namedtuple
import collections
Person = collections.namedtuple('Person', field_names=['name', 'age', 'gender'])
filip = Person(name='Filip', age=101, gender='undisclosed')
print(filip.name)
@filipwodnicki
filipwodnicki / conda_cheatsheet.sh
Last active July 9, 2019 10:38
Conda cheatsheet
# Conda cheatsheet
# Conda create new environment
conda create -n myenvname python=3.7
# Add conda environment to jupyter
python -m ipykernel install --user --name myenvname --display-name "Python (myenvname)"
# Conda create new environment and install datascience packages
conda create -n myenvname python=3.7 jupyter ipykernel nb_conda pandas geopandas shapely numpy scipy
# pip cheatsheet
# pip install from requirements
pip install -r requirements.txt
import keras
# Integer to categorical
keras.utils.to_categorical(4, num_classes=5, dtype='int')
@filipwodnicki
filipwodnicki / good_code.py
Created November 4, 2019 10:00
Good code practices
# 1. Use f-strings
#instead of this
name = 'projects/{}/locations/us-central1/models/{}'.format(project_id, model_id)
# do this
name = f'projects/{project_id}/locations/us-central1/models/{model_id}'
# 2. Explicitly name errors:
# instead of this
try:
@filipwodnicki
filipwodnicki / pandas_cheatsheet.py
Created November 4, 2019 10:03
Pandas cheatsheet
# 1. Apply works with multiple arguments
## instead of this
binary_test["prediction"] = binary_test["sentence"].apply(lambda x: get_prediction(x, PROJECT_ID, BINARY_MODEL_ID))
## do this
binary_test["prediction"] = binary_test["sentence"].apply(get_predictions, project_id=PROJECT_ID, model_id=BINARY_MODEL_ID