Skip to content

Instantly share code, notes, and snippets.

View enamoria's full-sized avatar
💭
vohox

Kính Phan enamoria

💭
vohox
  • Viet Nam
View GitHub Profile
"""
A weighted version of categorical_crossentropy for keras (2.0.6). This lets you apply a weight to unbalanced classes.
@url: https://gist.github.com/wassname/ce364fddfc8a025bfab4348cf5de852d
@author: wassname
"""
from keras import backend as K
def weighted_categorical_crossentropy(weights):
"""
A weighted version of keras.objectives.categorical_crossentropy
@enamoria
enamoria / unicode_to_ascii.py
Last active December 16, 2018 10:39
Convert unicode character to the nearest (removed tone, accent, ... that feature a specific language) ASCII character. For ex: "Тгцмр" will be converted to "Tgtsmr", not Trump
# Source: https://stackoverflow.com/a/518232/4102479
# This is better approach, compared to unidecode, though unidecode provide acceptable result in almost every cases
import unicodedata
import string
all_letters = string.ascii_letters + " .,;'"
nb_letter = len(all_letters)
print(all_letters, len(all_letters))
from google.colab import drive
drive.mount('/content/drive')
!rm *.json
from google.colab import files
files.upload() # upload kaggle.json
!pip install kaggle
!mkdir -p ~/.kaggle
@enamoria
enamoria / specshow.py
Created October 2, 2018 04:00
Create audio spectrogram (with legend) from wav
#!/usr/bin/env python
# coding: utf-8
""" This work is licensed under a Creative Commons Attribution 3.0 Unported License.
Frank Zalkow, 2012-2013 """
# Source: http://www.frank-zalkow.de/en/code-snippets/create-audio-spectrograms-with-python.html?ckattempt=1&i=1
import numpy as np
from matplotlib import pyplot as plt
import scipy.io.wavfile as wav
@enamoria
enamoria / dataframe_reduce_memory.py
Created October 17, 2018 03:32
Reduce pandas dataframe memory usage
# This function is used to reduce memory of a pandas dataframe
# The idea is cast the numeric type to another more memory-effective type
# For ex: Features "age" should only need type='np.int8'
# Source: https://www.kaggle.com/gemartin/load-data-reduce-memory-usage
def reduce_mem_usage(df):
""" iterate through all the columns of a dataframe and modify the data type
to reduce memory usage.
"""
start_mem = df.memory_usage().sum() / 1024**2
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
@enamoria
enamoria / useful_bash.sh
Last active July 3, 2019 03:51
Sweet bashscript o' mine
# copy a list of file (from file_id_list) from src to dest
cat file_id_list | awk '{system("cp " $1 " <dest_dir")}'
# total time of audio file within a directory
soxi -D *.wav | awk '{sum+=$1} END {print sum}'
@enamoria
enamoria / .ideavimrc
Created December 18, 2018 04:38
IdeaVim configuration: .ideavimrc
nnoremap <S-Left> :action EditorLeftWithSelection<CR>
nnoremap <S-Right> :action EditorRightWithSelection<CR>
nnoremap <S-Up> :action EditorUpWithSelection<CR>
nnoremap <S-Down> :action EditorDownWithSelection<CR>
inoremap <S-Left> <C-O>:action EditorLeftWithSelection<CR>
inoremap <S-Right> <C-O>:action EditorRightWithSelection<CR>
inoremap <S-Up> <C-O>:action EditorUpWithSelection<CR>
inoremap <S-Down> <C-O>:action EditorDownWithSelection<CR>
#WINE with error (OLE error 80040154)
wget http://winetricks.org/winetricks
chmod +x winetricks
winetricks -v vcrun6 winetricks -v msxml4
@enamoria
enamoria / frequency_estimator.py
Created August 21, 2019 03:09 — forked from endolith/frequency_estimator.py
Frequency estimation methods in Python
from __future__ import division
from numpy.fft import rfft
from numpy import argmax, mean, diff, log, nonzero
from scipy.signal import blackmanharris, correlate
from time import time
import sys
try:
import soundfile as sf
except ImportError:
from scikits.audiolab import flacread
@enamoria
enamoria / NlpUtils.java
Created October 10, 2019 09:13 — forked from behitek/NlpUtils.java
Chuẩn hóa cách gõ dấu câu về kiểu gõ cũ
/*
Copyright @ nguyenvanhieu.vn
Thằng code java này vẫn giữ được lower/upper case
Code này ngon hơn, check đúng trường hợp cần thêm dấu thì mới thêm
Tuy nhiên code python ở dưới không check nhưng vẫn chưa thấy bugs nào hết :v
*/
package utils;
import java.util.*;