Skip to content

Instantly share code, notes, and snippets.

View jaradc's full-sized avatar

jaradc

View GitHub Profile
@jaradc
jaradc / Django Unique Slug
Last active April 20, 2022 22:26
Simple-stupid way to guarantee slug uniqueness
from django.db import models
from django.utils.text import slugify
class Content(models.Model):
title = models.CharField(max_length=100)
slug = models.SlugField(max_length=100)
def save(self, *args, **kwargs):
# simple-stupid way to guarantee slug uniqueness
n = 1
@jaradc
jaradc / inlineformset_factory_html_output.html
Created February 16, 2020 17:57
An example of what the output looks like for inlineformset_factory with 3 filled-out module forms containing Title, Description and 2 extra forms in the formset.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post">
<input type="hidden" name="csrfmiddlewaretoken"
class Jarad:
def __init__(self, **kwargs):
for k,v in kwargs.items():
setattr(self, k, v)
"""
@author: Claudio Bellei
Site: http://www.claudiobellei.com/2016/11/15/changepoint-frequentist/
"""
import numpy as np
import pandas as pd
import csv
import matplotlib
import matplotlib.pyplot as plt
import difflib
"""
This simple piece of code can help detect similar strings using SequenceMatcher
"""
data = ['temporary tatoos', 'temporary tatto', 'tempoary tatoo', 'tempoary tattoo', 'temporary tattoos']
for line in data:
for word in line.split():
i = difflib.SequenceMatcher(None, word, 'tattoo').ratio()
@jaradc
jaradc / keyword_grouping_in_python.py
Created July 31, 2019 05:00
Basic Keyword Clustering Example in Python
import pandas as pd
import numpy as np
from nltk.stem import PorterStemmer, WordNetLemmatizer
from nltk.corpus import stopwords
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import cluster
stemmer = PorterStemmer()
@jaradc
jaradc / functiontransformer_example_with_text_data.py
Created August 2, 2018 16:32
A pipeline example showing how to use kwargs in a function, and how to use FunctionTransformer from sklearn to decode URLs
def fit_model(X, y, **kwargs):
print(kwargs)
print(kwargs['max_iter'])
pipeline = Pipeline([
('decode', FunctionTransformer(func=lambda x: x.apply(
lambda url: parse.unquote(parse.unquote(url))), validate=False)),
('cvect', CountVectorizer(binary=True, max_features=1000, stop_words='english',
token_pattern=r'\b\w[\w\.\-\,]+\b')),
('clf', MLPClassifier(verbose=1, solver='sgd', max_iter=kwargs['max_iter'] or 1000,
tol=0.00001, learning_rate='adaptive', learning_rate_init=0.05)),
@jaradc
jaradc / scale_between_two_numbers.py
Created January 5, 2018 21:36
This function scales a numpy array between two values (ex: 0.05, 1.25)
def scale_between(series, min_amt, max_amt):
series_min = series.min()
series_max = series.max()
return (((max_amt - min_amt)*(series - series_min)) / (series_max - series_min)) + min_amt
@jaradc
jaradc / make_moving_avg_plot.py
Last active December 21, 2017 09:44
This lets you create subplots of n rows and 1 column of a metric within a time series.
def ma_subplots(df, window, title=None):
fig, ax = plt.subplots(df.shape[1], 1, figsize=(12, 7))
ax = ax.ravel()
for i,col in enumerate(df):
mean = df[col].mean()
ma = df[col].rolling(window).mean()
mstd = df[col].rolling(window).std()
ax[i].plot(df.index, df[col], color='k', label=col)
ax[i].plot(ma.index, ma, 'b', label='Moving Avg.')
ax[i].fill_between(mstd.index, ma - 2*mstd, ma + 2*mstd, color='b', alpha=0.2)