Skip to content

Instantly share code, notes, and snippets.

View data-enhanced's full-sized avatar

David Cochran data-enhanced

View GitHub Profile
# Format using Markdown ================================================
# Format Jupyter Code Output using Markdown
# https://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html?highlight=display_markdown#IPython.display.display_markdown
from IPython.display import display_markdown
markdowntext = 'Markdown Heading Level 3'
display_markdown(f'### Code Output Formatted as {markdowntext}', raw=True)
display_markdown(f'_Code output italicized using_ `display_markdown`', raw=True)
@data-enhanced
data-enhanced / model_reports.py
Created February 25, 2022 02:31
Custom function to generate a report for a binary classification model that includes the model, scores, and confusion matrix.
# Function for generating model scores and confusion matrices with custom colors and descriptive labels
# https://stackoverflow.com/questions/70097754/confusion-matrix-with-different-colors
# https://medium.com/@dtuk81/confusion-matrix-visualization-fc31e3f30fea
def report_scores(model, features, labels):
'''
Generating model scores and confusion matrices with custom colors and descriptive labels
model = model variable
features = features of desired split
labels = labels of desired split
@data-enhanced
data-enhanced / pandas_describe_formatted.md
Last active February 12, 2021 14:51
Format output of pandas describe() method

Pandas .describe() formatted

Format numbers output from the pandas df.describe() method. For instance, instead of outputting scientific notation, we can have numbers with thousands separators and a desired number of decimals.

When using .describe with an entire dataframe, use .apply and a lambda function to apply the formatting to every number.

  • To change the number of decimals, change the number before the f
  • To remove the thousands separator remove the comma

df.describe().apply(lambda s: s.apply('{:,.0f}'.format))

When using .describe with a single column or a series, use the .map method instead:

@data-enhanced
data-enhanced / Remove_json_from_TMDB_fields.py
Last active March 6, 2020 00:42
Remove json formatting from TMDB fields
# Import ast for ast.literal_eval
import ast
# Remove JSON from TMDB fields
# for genres, spoken_languages, production_companies, production_countries
# Works only with non-null values, so filter out null values before applying
# Requires import ast -- or use simply eval vs ast.literal_eval
def remove_json(content):
# Interpret the content as a Python list of dictionaries
content = ast.literal_eval(content)
@data-enhanced
data-enhanced / apply_function_pd_column.py
Created February 17, 2020 20:57
Apply a function to a pandas column
# Define a function to get the name field from the first item in a dictionary list
def get_genre1(x):
x = json.loads(x)
if len(x) > 0:
return x[0]['name']
# Now use pandas.apply to use the function on one column
# In thise case create a new column called genres1 to hold the new data
movies['genre1'] = movies['genres'].apply(get_genre1)
@data-enhanced
data-enhanced / lists_of_animals.py
Last active January 28, 2020 02:44
Lists of animals for practice with list methods
AussieAnimals = ['kangaroo', 'cassowary', 'wombat', 'possum', 'echidna', 'ibis', 'wallaby', 'koala',
'tasmanian devil', 'kookaburra', 'numbat', 'platypus', 'lyre bird', 'quokka', 'quoll',
'sugar glider', 'bandicoot', 'thorny devil', 'dingo', 'wallaroo', 'yabby', 'bilby']
GalapagosAnimals = ['rice rat', 'hoary bat', 'bottlenose dolphin', 'beaked whale', 'lava lizard',
'tortoise', 'flightless cormorant', 'green sea turtle', 'blue-footed booby',
'marine iguana', 'pink land iguana', 'darwins finches', 'brown noddy']
@data-enhanced
data-enhanced / jupyter_default_browser.md
Last active December 18, 2020 13:23
Change default browser for Jupyter Notebooks in Mac OS X

Change the Default Browser for Jupyter Notebooks in OS X

Step 1. Create an editable config file for Jupyter notebooks.

To do this, open Terminal and type:

jupyter notebook --generate-config

This generates the file:

~/.jupyter/jupyter_notebook_config.py

@data-enhanced
data-enhanced / reveal_library.txt
Created August 12, 2018 23:31
Reveal the Library Folder in OS X local user directory or a backup on another volume
# Using Mac OS X Terminal
# High Sierra
# August 2018
# Reveal Library folder in current user directory
chflags nohidden ~/Library/
# Reveal Library folder in backup user directory
chflags nohidden /Volumes/Volume_Name/Users/username/Library/
@data-enhanced
data-enhanced / crypto-scraper.R
Last active January 10, 2018 14:13
Cryptocurrency data scraper in R -- script to utilize JesseVent/crypto
# Install and use crypto scraper from
# https://github.com/JesseVent/crypto
# This script is written to be used by running the desired line(s) separately, often one line at a time
# Install jessevent/crypto package in Rstudio
# If you do not have devtools installed, install devtools first
install.packages("devtools")
# Now install jessevent/crypto
devtools::install_github("jessevent/crypto")
@data-enhanced
data-enhanced / risk_taxonomy.json
Created July 4, 2017 20:31
Cyber Security Risk Taxonomy
{
"name": "Threat Taxonomy",
"children": [
{
"name": "Actions of People",
"children": [
{
"name": "Inadvertent",
"children": [
{"name": "Mistakes", "size": 1},