Skip to content

Instantly share code, notes, and snippets.

View Per48edjes's full-sized avatar
👉
This is a good point.

Ravi Dayabhai Per48edjes

👉
This is a good point.
View GitHub Profile
@Per48edjes
Per48edjes / dupe_checking_paradigm.py
Last active June 21, 2020 04:21
Dupe-check logging
import datetime
# Filename function for logging
def dt_filename(
filename: str,
extension: str,
path: str = "",
date: str = str(datetime.datetime.now()),
) -> str:
filename = "_".join([filename, date])
@Per48edjes
Per48edjes / pandas_dataframe_difference.py
Created April 23, 2020 01:15 — forked from toddbirchard/pandas_dataframe_difference.py
Helper function to compare two DataFrames and find rows which are unique or shared.
def dataframe_difference(df1, df2, which=None):
"""Find rows which are different."""
comparison_df = df1.merge(df2,
indicator=True,
how='outer')
if which is None:
diff_df = comparison_df[comparison_df['_merge'] != 'both']
else:
diff_df = comparison_df[comparison_df['_merge'] == which]
diff_df.to_csv('data/diff.csv')
@Per48edjes
Per48edjes / stacked_bar_chart.py
Created December 25, 2019 00:55
Function to plot a stacked bar chart
import numpy as np
import matplotlib.pyplot as plt
def plot_stacked_bar(data, series_labels, category_labels=None,
show_values=False, value_format="{}", y_label=None,
colors=None, grid=True, reverse=False):
"""Plots a stacked bar chart with the data and labels provided.
Keyword arguments:
data -- 2-dimensional numpy array or nested list
@Per48edjes
Per48edjes / ColumnTransform_pipeline_field_extraction.py
Created November 13, 2019 22:44
Get column names from ColumnTransformer with embedded pipelines (SKLearn)
## Split into holdout for purposes of imputation and encoding
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = .2, random_state=2019)
class_labels = df['segment_label'].cat.categories
## Preprocessing pipeline
# Define transforms on numeric types
numeric_features = X.select_dtypes(np.number).columns
numeric_transformer = Pipeline(steps=[
('imputer', SimpleImputer(strategy='median')),
@Per48edjes
Per48edjes / hide_cells
Last active November 11, 2018 04:27
Toggle code cell visibility in Jupyter Notebook
from IPython.display import HTML
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
@Per48edjes
Per48edjes / mergesort.py
Created January 6, 2018 03:37
Simple Mergesort
#!/usr/bin/python
def merge(l):
# Base case
if len(l) < 2:
return l
mid = len(l) // 2
y = merge(l[:mid])
@Per48edjes
Per48edjes / slice.py
Created September 30, 2017 03:04
Comparing in-memory locations of lists vs. slices of list vs. reassigning elements to list reference
x = [1,2,3]
def test():
print("x-slice id before",id(x[:]))
print("x-slice before", x[:])
x[:] += [4]
print("x-slice id after",id(x[:]))
print("x-slice after", x[:])
print("x id", id(x))
@Per48edjes
Per48edjes / TowerHanoi.py
Created August 27, 2017 20:03
Recursive solution to the Tower of Hanoi problem with N discs
def printMove(fr, to):
print('move from ' + str(fr) + ' to ' + str(to))
def Towers(n, fr, to, spare):
if n == 1:
printMove(fr, to)
else:
Towers(n-1, fr, spare, to)
Towers(1, fr, to, spare)
Towers(n-1, spare, to, fr)
@Per48edjes
Per48edjes / caesar_cipher_1
Created May 12, 2017 15:27
Caesar's Cipher (v.1)
#!/bin/python
import sys
n = int(raw_input().strip())
s = raw_input().strip()
k = int(raw_input().strip())
@Per48edjes
Per48edjes / pythonlinelength.json
Created March 3, 2017 19:08
Atom Beautify - Debugging Info
# Atom Beautify - Debugging information
The following debugging information was generated by `Atom Beautify` on `Fri Mar 03 2017 11:05:22 GMT-0800 (PST)`.
---
## Table Of Contents
- [Versions](#versions)
- [Original file to be beautified](#original-file-to-be-beautified)
- [Original File Contents](#original-file-contents)