Skip to content

Instantly share code, notes, and snippets.

View Syncrossus's full-sized avatar

Syncrossus

View GitHub Profile
@Syncrossus
Syncrossus / BayesLearning_apprenantBayes.py
Last active December 17, 2017 13:59
Small Single-Attribute Bayesian network classifier. Sorry code is in French, intended recepient is French.
class Apprenant:
"""
Classe modelisant un apprenant bayesien
Attributs :
classes : classes identifiees dans la base d'apprentissage
valAttr : valeurs que peut prendre l'attribut des objets classables
tableau : Chaque ligne est une classe, chaque colonne une valeur d'attribut.
Chaque cellule contient l'effectif de la base d'apprentissage correspondant.
ex :
|1.6|1.7|1.8|1.9
@Syncrossus
Syncrossus / google_glat_q1.pro
Last active July 3, 2018 09:58
Solution to the problem WWWDOT - GOOGLE = DOTCOM (GLAT question 1) in prolog
% solution to the problem WWWDOT - GOOGLE = DOTCOM in prolog
carry(0).
carry(1).
digit(A) :- carry(A).
digit(2).
digit(3).
digit(4).
digit(5).
digit(6).

Keybase proof

I hereby claim:

  • I am syncrossus on github.
  • I am syncrossus (https://keybase.io/syncrossus) on keybase.
  • I have a public key ASDJ1fvJbb42jvF3Gn51iZ6dvz4d2IwfAfbQWb870hixSQo

To claim this, I am signing this object:

@Syncrossus
Syncrossus / relabel.R
Last active September 5, 2019 08:42
Cluster relabeling function (useful for confusion tables with clusters labeled with k-means for example). runs in complexity O(n^2*m^2) with n the length of labels1 and m the number of clusters.
# arguments :
# labels1 : list of labels corresponding to the wrong clusters
# labels2 : list of labels corresponding to the right clusters
# return :
# labels1 relabeled
relabel <- function(labels1, labels2){
newlabels <- labels1
levels1 <- levels(as.factor(labels1))
levels2 <- levels(as.factor(labels2))
for(i in levels1){
@Syncrossus
Syncrossus / fisher_dynamic_programming.R
Last active September 5, 2019 08:42
Fisher's dynamic programming algorithm for time series clustering in R
# Fisher's criterion computation function
# arguments :
# clusters : list as returned by clustfisher (see below)
# return :
# the value of the criterion
fisherCriterion <- function(clusters){
diameterSum <- 0
if(length(clusters$instants)==1){
diameterSum <- sum(clusters$D[clusters$instants[1:length(clusters$instants)-1], clusters$instants[2:length(clusters$instants)]-1])
}
@Syncrossus
Syncrossus / levenshtein.py
Last active September 18, 2019 13:03
Levenshtein distance in Python for lists of objects comparable with the `!=` operator. Uses the Wagner-Fischer algorithm.
import numpy as np
def levenshtein(l1, l2):
""" Computes Levenshtein distance between two list-like objects
Args:
l1, l2: subscriptable objects compatible with len()
containing objects comparable with the `!=` operator.
"""
# initializing the distance matrix
@Syncrossus
Syncrossus / score2rank.py
Last active September 5, 2019 08:41
Function that transforms scores to ranks : takes a matrix with 2 columns with each row being of the form [object, score] and turns each row into the form [object, rank]. Highest score => rank 1
import numpy as np
def score2rank(m):
""" args:
- m, a numpy matrix with 2 columns the 2nd of which should be numeric
returns:
- m with the second column's numbers switched for ranks
example:
m = [[a,5.8], return = [[a,2],
@Syncrossus
Syncrossus / identicon.py
Last active July 9, 2021 07:51
A simple identicon creation utility module
""" This module contains a simple identicon creation utility function.
Original color palette : coolors.co/659bd8-ff776d-ffd793-9eefae-c99dba
"""
import hashlib
import random
import png # requires pypng (version 0.0.19 recommended)
import sys
palette = {0: (255, 255, 255),
1: (101, 155, 216),
@Syncrossus
Syncrossus / dichotomy.py
Last active December 6, 2019 16:17
A simple function to carry out a dichotomy search (the optimal search for an item's index in a sorted list), because it does not exist in the standard library.
def dichotomy(ls, v, _i=0):
""" Dichotomy search, or the optimal search for an item's index
in a sorted list.
Arguments:
- (list) ls: the list in which to search, must be sorted for
this to work
- v: value to search for in the list, must be comparable to the
items in the list
- (int) _i: should not be used, is reserved for recursive purposes
Returns:
@Syncrossus
Syncrossus / useful_regexes.pl
Last active October 22, 2021 13:55
A collection of useful regular expressions, in PCRE and Python syntax
# Garbage matcher
# matches any string consisting of only |, <, >, +, *, ^, #, =, and hyphen chains.
# this is to identify patterns like ++<===> ######## <---->^^ which serve no purpose but to decorate the text
/(\||(--+)|(__+)|<|>|\+|\*|\^|#|=|~)+|(\\|_|\/){2,}/
# Garbage matcher 2
# matches anything that isn't a letter, space or basic punctuation.
# this is typically useful for cleaning up emojis
/.(?<!([a-zA-Z0-9]|,|\.|'| |\?|\!))/