Skip to content

Instantly share code, notes, and snippets.

View jaradc's full-sized avatar

jaradc

View GitHub Profile
@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 / 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()
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()
"""
@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
class Jarad:
def __init__(self, **kwargs):
for k,v in kwargs.items():
setattr(self, k, v)
@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"
@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