Skip to content

Instantly share code, notes, and snippets.

View megpay's full-sized avatar

Megan Payne megpay

  • Amsterdam, Netherlands
View GitHub Profile
@megpay
megpay / create_venv.sh
Created February 6, 2023 17:13
Create, check, activate, and delete virtual environments
# create a virtual env called myvirtualenv
virtualenv myvirtualenv
# activate the virtual environment and install everything from requirements
source myvirtualenv/bin/activate
pip install -r requirements.txt
# check which venv you are in
which python
@megpay
megpay / gen_locale_for_R.sh
Created August 15, 2022 11:42
Generating new locales for R to access
# When working with R files created using locales that are not installed, this is how you generate new ones.
# Works on Ubuntu 20.04.4 LTS
# prints out locales that are already created and accessible.
locale -a
# generate a new one, for example French
locale-gen fr
# Update the locales so that R can access them
@megpay
megpay / conda.yaml
Created July 9, 2022 12:23
Example conda yaml file to create a conda environment
name: data-visualization
dependencies:
- python=3.9.7
- pandas
- numpy
- matplotlib
- seaborn
- pip
- pip:
@megpay
megpay / numpy_functions.py
Created July 9, 2022 10:58
Useful numpy functionality
import numpy as np
## Make a table of counts of each value in a numpy array.
# This returns the unique values and their counts in a numpy array.
x = np.array([1, 1, 1, 2, 2, 2, 7, 14, 2, 1])
unique, counts = np.unique(x, return_counts=True)
print(np.asarray((unique, counts)).T) # Use transpose for better visual
## Sort it
@megpay
megpay / manual_gc.py
Created February 22, 2021 22:30
Manual garbage collection when RAM is limited
# Manual garbage collection due to limited RAM - useful in Kaggle competitions
import sys
for i in dir():
if sys.getsizeof(eval(i)) > 10000:
print("the size of {} is:".format(i), sys.getsizeof(eval(i))/1e+9)
import gc
del train # delete a data frame
gc.collect() # manual garbage collection
@megpay
megpay / useful_psql_commands.sql
Last active September 29, 2020 08:30
Useful postgresql commands
/* Basic viewing the structure commands */
\c database_name --connect to a database.
\dt --view the tables. this filters out non-table relations.
\d --view the tables. Includes non-table relations.
\d tablename --view the columns of the table.
/* Create a table and import from a csv into that table. */
CREATE TABLE employees (
id SERIAL,
@megpay
megpay / sed_commands.sh
Last active September 24, 2020 16:58
Useful sed commands
#!/bin/sh
# Where I am storing useful sed commands for later use.
# Removing characters from a csv or text file. This removes a double-quote.
sed 's/"//g' filename.csv
sed 's/"//g' filename.csv > filename_new.csv
# Not sed, but using cut to remove columns of a csv file.
# This takes a pipe-delimited file and removes columns 2, 3, and 4.
@megpay
megpay / SF_crime_map.ipynb
Last active June 10, 2024 19:46
Visualizing the crime rate in San Francisco using python and folium
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@megpay
megpay / avg_salary.py
Created June 29, 2020 01:04
Returning the mean of a list of salaries without the max or min salaries.
# From this problem on leetcode.
# https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/
# Given an array of unique integers salary where salary[i] is the salary of the employee i.
# Return the average salary of employees excluding the minimum and maximum salary.
# Example:
# Input: salary = [4000,3000,1000,2000]
@megpay
megpay / topsal.sql
Last active June 23, 2020 07:28
Top Employee Salary by Department
/*
Given 2 tables in a MySQL database, Employee and Department, find the employees (including ties) with the highest
salaries for each department.
Employee table:
Id (auto incremented integer)
Name (Varchar, employee name)
DepartmentId (integer and key to Department table)
Salary (Integer)