Skip to content

Instantly share code, notes, and snippets.

View debonx's full-sized avatar
🍕
Food processing

Emanuele De Boni debonx

🍕
Food processing
View GitHub Profile
@debonx
debonx / wp_users_contact_methods.php
Last active October 21, 2018 16:52
Add contact methods to Wordpress User dashboard, like social media or external urls. Add this code to your theme's functions.php.
add_filter( 'user_contactmethods','huraji_contactmethods', 20, 1 );
function huraji_contactmethods( $contact_methods ) {
$contact_methods['linkedin'] = __( 'LinkedIn URL', 'text_domain' );
$contact_methods['github'] = __( 'Github URL', 'text_domain' );
$contact_methods['youtube'] = __( 'YouTube URL', 'text_domain' );
return $contact_methods;
}
@debonx
debonx / numpy_example.py
Created November 1, 2018 11:53
Using Numpy example. Records of weekly weather temperatures. More at www.numpy.org.
import numpy as np
#Get data from CSV
#Temperatures recorded 4 times a day, at 0:00, 6:00, 12:00, and 18:00. Last weeks data (Monday through Friday)
temperatures = np.genfromtxt('temperature_data.csv', delimiter=',')
#Adjust temperatures
temperatures_fixed = temperatures + 3.0
#Select Monday's Temperatures
@debonx
debonx / pandas_example.py
Created November 1, 2018 15:38
Using Pandas library example. More at https://pandas.pydata.org/
import pandas as pd
#Read CSV file
orders = pd.read_csv('shoefly.csv')
#Inspect first 5 lines
print(orders.head(5))
#Get a specific serie of data / column
emails = orders['email']
@debonx
debonx / wp_update_transient.php
Created November 2, 2018 17:49
How to update and check transients on Wordpress. More at https://xilab.co.
/*
* @param $tag = tag to use for this transient
* @param $element = value to associate the transient
* @param $duration / $expiration = time in seconds > 3600*24 for 1 day
* @return true > if needed to update | false > if still not expired
*/
function update_transient($tag, $element, $duration, $expiration){
$elements_in_tag = get_transient($tag);
@debonx
debonx / wp_debugging.php
Last active November 11, 2018 08:29
Fast debugging method with Wordpress.
/*
* Put this function in your plugin or functions.php and pass the parameter $log to be debugged.
* Example: wpui_write_log( $log ) where $log is the data to be debugged.
* It writes into /debug.log of your wp-content/
* Could be used as class method as well
*/
function wpui_write_log( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
@debonx
debonx / scikit_learn_example.py
Created November 12, 2018 10:00
Basic Example to work with Scikit-Learn for calculating Linear Regressions. More at https://scikit-learn.org/
#Import LinearRegression methods from Scikit-Learn
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
import numpy as np
#Data of Soup sales with their temperature
temperature = np.array(range(60, 100, 2))
temperature = temperature.reshape(-1, 1)
sales = [65, 58, 46, 45, 44, 42, 40, 40, 36, 38, 38, 28, 30, 22, 27, 25, 25, 20, 15, 5]
@debonx
debonx / scikit_predictive_power.py
Created November 12, 2018 11:00
Analyse the predictive power of your algorithm with Scikit-Learn. More at https://scikit-learn.org/
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score
#True labels and predicted classifications
labels = [1, 0, 0, 1, 1, 1, 0, 1, 1, 1]
guesses = [0, 1, 1, 1, 1, 0, 1, 0, 1, 0]
#Calculate accuracy, recall, precision and f1 score
print(accuracy_score(labels, guesses))
print(recall_score(labels, guesses))
print(precision_score(labels, guesses))
@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 / get_url_param.js
Last active November 21, 2018 15:31
JQuery / Javascript small function to get url parameters, for example https://your-link?param=value.
function get_url_param(name){
var results = new RegExp('[\?&]' + name + '=([^]*)').exec(window.location.href);
if (results==null){
return null;
} else {
if(results[1].indexOf("#")){
results = results[1].split("#");
results = results[0];
} else {
results = results[1] || 0;
@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)