Skip to content

Instantly share code, notes, and snippets.

View debonx's full-sized avatar
🍕
Food processing

Emanuele De Boni debonx

🍕
Food processing
  • adidas ///
  • Amsterdam
View GitHub Profile
@debonx
debonx / woocommerce_api_manager_call.php
Created November 17, 2018 08:56
Woocommerce API manager call example. Retrieves informations about digital products and subscriptions.
/**
*
* Check Status Of License Activation
* More at https://docs.woocommerce.com/document/woocommerce-api-manager/
* @author: huraji (https://xilab.com/u/huraji)
* @param: $Request (string)
* @param: $Endpoint (string)
* @return: JSON decoded formatted data
*
**/
@debonx
debonx / naive_bayes_classifier_ex1.py
Last active December 11, 2018 09:34
Leverage Bayes' Theorem to create a supervised machine learning algorithm, thanks to sklearn.
# REQUIREMENTS
# - A tagged dataset is necessary to calculate the probabilities used in Bayes' Theorem.
# - In order to apply Bayes' Theorem, we assume that these features are independent.
# - Using Bayes' Theorem, we can find P(class|data point) for every possible class. The class with the highest probability will be the algorithm’s prediction.
# MORE Improvements for the Natural Language Processing.
# - Remove punctuation from the training set (great! into great)
# - Lowercase every word in the training set (Great into great)
# - Use a bigram or trigram model, makes the assumption of independence more reasonable ("this is" or "is great" instead of single words)
@debonx
debonx / naive_bayes_classifier.py
Last active November 28, 2020 11:05
Naive Bayes classifier in Python
#Import datasets and libraries
from sklearn.datasets import fetch_20newsgroups
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer
#Category selection to compare different datasets of emails and evaluate how hard is to distinguish those.
the_categories = ['comp.sys.ibm.pc.hardware', 'rec.sport.hockey']
train_emails = fetch_20newsgroups(categories = the_categories, subset = 'train', shuffle = True, random_state = 108)
test_emails = fetch_20newsgroups(categories = the_categories, subset = 'test', shuffle = True, random_state = 108)
@debonx
debonx / accuracy_recall_precision_f1.py
Created December 11, 2018 10:23
Accuracy, Recall, Precision and F1 score with sklearn.
# To be reminded
# 1) Classifying a single point can result in a true positive (truth = 1, guess = 1), a true negative (truth = 0, guess = 0), a false positive (truth = 0, guess = 1), or a false negative (truth = 1, guess = 0).
# 2) Accuracy measures how many classifications your algorithm got correct out of every classification it made.
# 3) Recall measures the percentage of the relevant items your classifier was able to successfully find.
# 4) Precision measures the percentage of items your classifier found that were actually relevant.
# 5) Precision and recall are tied to each other. As one goes up, the other will go down.
# 6) F1 score is a combination of precision and recall.
# 7) F1 score will be low if either precision or recall is low.
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score
@debonx
debonx / brast_cancer_classifier.py
Created December 15, 2018 10:56
Using Python and sklearn to create a Breast Cancer Classifier and predict malignant or benign tumours, based on features list.
# Importing breast cancer data and features.
# Importing training model, classifier and matplotlib
import codecademylib3_seaborn
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
# Split data
@debonx
debonx / linear_svc_sklearn.py
Last active December 19, 2018 10:53
Example of Supervised Machine Learning with Support Vector Classifier from sklearn.
from sklearn.svm import SVC
from graph import points, labels
# Create SVC classifier (linear decision boundary)
classifier = SVC(kernel = 'linear')
# Training with points and labels
classifier.fit(points, labels)
# Print the prediction
@debonx
debonx / kmeans.py
Last active August 21, 2019 10:20
Example of Unsupervised Machine Learning with KMeans (sklearn).
import matplotlib.pyplot as plt
from sklearn import datasets
# From sklearn.cluster, import Kmeans class
from sklearn.cluster import KMeans
# Load data
iris = datasets.load_iris()
samples = iris.data
@debonx
debonx / perceptron_logic_gates.py
Last active December 23, 2018 09:56
Example of Perceptron Logic Gates AND, OR and XOR to build Artificial Neural Networks.
from sklearn.linear_model import Perceptron
import matplotlib.pyplot as plt
import numpy as np
from itertools import product
data = [[0, 0], [0, 1], [1, 0], [1, 1]]
# Set labels basing on gate type (AND, OR)
# Multiple perceptrons can solve XOR gates
labels = [0, 0, 0, 1]
@debonx
debonx / set_url_param.js
Last active March 27, 2019 10:07
JQuery / Javascript small function to set URL parameters, for example https://your-link?param=value.
function setUrlParam(key,value) {
if (history.pushState) {
var params = new URLSearchParams(window.location.search);
params.set(key, value);
var newUrl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?' + params.toString();
window.history.pushState({path:newUrl},'',newUrl);
}
}
@debonx
debonx / rock-paper-scissors.js
Created July 27, 2019 16:54
Rock, paper or scissors? A simple Javascript game.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
return userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ? userInput : console.log('Invalid input');
}
const getComputerChoice = () => {
let whole = Math.floor(Math.random() * 3);
switch( whole ) {
case 0: