Skip to content

Instantly share code, notes, and snippets.

KUROYANAGI KEIICHI Keiku

Block or report user

Report or block Keiku

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Keiku
Keiku / convert_number_strings.py
Created Jan 31, 2017
Convert number strings with commas in pandas DataFrame to float.
View convert_number_strings.py
import pandas as pd
import locale
from locale import atof
df = pd.DataFrame([['1,200', '4,200'], ['7,000', '-0.03'], ['5', '0']])
# 0 1
# 0 1,200 4,200
# 1 7,000 -0.03
# 2 5 0
locale.setlocale(locale.LC_NUMERIC, '')
@Keiku
Keiku / roc_auc.py
Last active Oct 24, 2018
Plot ROC curve.
View roc_auc.py
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc
import seaborn as sns
sns.set('talk', 'whitegrid', 'dark', font_scale=1.5, font='Ricty',
rc={"lines.linewidth": 2, 'grid.linestyle': '--'})
fpr, tpr, _ = roc_curve([1, 0, 1, 0, 1, 0, 0], [0.9, 0.8, 0.7, 0.7, 0.6, 0.5, 0.4])
roc_auc = auc(fpr, tpr)
@Keiku
Keiku / read_copytext.py
Created Jan 19, 2018
Read copy text to pandas DataFrame.
View read_copytext.py
import pandas as pd
from io import StringIO
def read_copytext(text):
text1 = StringIO(text)
df = pd.read_table(text1)
df.columns = ["col1"]
df["col1"] = df["col1"].str.replace("\s+", ",")
@Keiku
Keiku / command.sh
Last active Jun 23, 2017
A list of linux commands.
View command.sh
# compress/decompress zip file.
zip file.csv.zip file.csv
unzip file.csv.zip
# compress/decompress gz file.
gzip file.csv
gzip -d file.csv.gz
# compress/decompress bz2 file.
bzip2 file.csv
@Keiku
Keiku / split_KFold.py
Last active May 2, 2017
Split K-fold validation dataset.
View split_KFold.py
import string
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold, StratifiedKFold
X_train = np.random.random((10, 2))
y_train = np.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0])
column = "pred"
n_fold = 5
@Keiku
Keiku / get_wordnet_synonyms.py
Created Apr 28, 2017
Extract the synonyms by using wordnet.
View get_wordnet_synonyms.py
from itertools import chain
from nltk.corpus import wordnet
synonyms = wordnet.synsets('change')
lemmas = set(chain.from_iterable([word.lemma_names() for word in synonyms]))
lemmas
# Out[31]:
# {'alter',
# 'alteration',
# 'change',
@Keiku
Keiku / stack_sparse_matrix.py
Created Apr 28, 2017
Stack the sparse matrices.
View stack_sparse_matrix.py
import numpy as np
import scipy as sp
import pandas as pd
df1 = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
df2 = pd.DataFrame({"C": [5, 6]})
X1 = sp.sparse.csr_matrix(df1.values)
X1_dense = X1.todense()
# Out[28]:
View list_operations.py
import numpy as pd
# Python
list(map(lambda x: x + 1, range(1, 6, 1)))
# Out[1]: [2, 3, 4, 5, 6]
# Numpy
list(np.array(range(1, 6, 1)) + 1)
# Out[2]: [2, 3, 4, 5, 6]
@Keiku
Keiku / tmux.sh
Created Apr 17, 2017
tmux command reference.
View tmux.sh
# show prefix
tmux show-options -g prefix
# new session
tmux
tmux work
# check sessions
tmux ls
@Keiku
Keiku / OrderedDict_sample.py
Last active Apr 13, 2017
Get keys/values from sorted OrderedDict.
View OrderedDict_sample.py
from collections import OrderedDict
d = {'A': 3,
'B': 2,
'C': 1}
OrderedDict(sorted(d.items(), key=lambda x: x[0])).values()
# Out[1]: odict_values([3, 2, 1])
OrderedDict(sorted(d.items(), key=lambda x: x[1])).values()
# Out[2]: odict_values([1, 2, 3])
You can’t perform that action at this time.