Skip to content

Instantly share code, notes, and snippets.

@tkazusa
tkazusa / file0.txt
Last active November 3, 2016 08:21
Pythonで実装 PRML 第1章 ベイズ推定 ref: http://qiita.com/tkazusa/items/d4234d7c2ebb56a604d7
p(t| x, {\bf x}, {\bf t}) = N(t| m(x), s^2(x)) (1.69)
@tkazusa
tkazusa / commit_message_example.md
Created September 30, 2018 12:57 — forked from mono0926/commit_message_example.md
[転載] gitにおけるコミットログ/メッセージ例文集100

gitにおけるコミットログ/メッセージ例文集100の転載


gitにおけるコミットログ/メッセージ例文集100

私はコミットログの書き方に悩む英語の苦手な人間である。実際、似たような人は世の中に結構いるようで、頻出単語を集計したりまとめたものは既にあって役に立つのだけれど、これらはあくまで単語の話であり、具体的な文を構成する過程でやっぱり困る部分がかなりあった。

要するに、どういう時にどういう文が使われているのか、ということを示した例文集が欲しいのである。ググると他にも「例文集があればいいのに」みたいな声はあるくせして、しかし誰も作ろうとしない。何なんだお前ら。それじゃ私が楽できないじゃないか。

@tkazusa
tkazusa / PandasBasics.ipynb
Last active October 17, 2018 15:41
DataPreprocess
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@tkazusa
tkazusa / encoding.py
Last active November 2, 2018 12:32
カテゴリカル変数のEncoding
import pandas as pd
import numpy as np
"""
http://docs.h2o.ai/h2o/latest-stable/h2o-docs/data-munging/target-encoding.html
"""
class FreqencyEncoder:
"""Encode categorical integer features to numerical features correspondig to its frequency."""
def load_train_data():
paths = sorted(glob.glob('../data/train/*.csv.gz'))
with Pool() as p:
df = pd.concat(p.map(read_csv, paths), ignore_index=True, axis=0, copy=False)
logger.info('data size {}'.format(df.shape))
return df
def load_val_data():
def negative_down_sampling(data, random_state, target_variable):
positive_data = data[data[target_variable] == 1]
positive_ratio = float(len(positive_data)) / len(data)
negative_data = data[data[target_variable] == 0].sample(
frac=positive_ratio / (1 - positive_ratio), random_state=random_state)
return pd.concat([positive_data, negative_data])
def multi_class_downsampling(data, random_state, target_variable):
data0 = data[data[target_variable] == 0]
@tkazusa
tkazusa / TargetEncoding_with_confidence.py
Created December 2, 2018 08:53
出現回数の少ないカテゴリに関しては、TargetEncodingの信頼性が少なくなる。ので、そうなるようにそのカテゴリの出現回数(view_cat)をlog(n), 例えばn=100000で割ることで、その信頼度を割り引いてやる。nは任意。https://www.kaggle.com/nanomathias/feature-engineering-importance-testing
for cols in ATTRIBUTION_CATEGORIES:
 # Aggregation function
def rate_calculation(x):
"""Calculate the attributed rate. Scale by confidence"""
rate = x.sum() / float(x.count())
conf = np.min([1, np.log(x.count()) / log_group])
return rate * conf
# Perform the merge
X_train = X_train.merge(
# set class weights
classdist = pd.value_counts(val['target'])
weights = {i: round(np.sum(classdist) / classdist[i]) for i in classdist.index}
arr = np.empty((0,3), int)
arr = np.append(arr, np.array([[1, 2, 3],[4, 5, 6]]), axis=0)
arr = np.append(arr, np.array([[7, 8, 9],[10, 11, 12]]), axis=0)
arr.reshape(-1,2,3)
>array([[[ 1, 2, 3],
> [ 4, 5, 6]],
> [[ 7, 8, 9],
> [10, 11, 12]]])
"""
クラスごとにサブプロットで散布図見た。
"""
for Keys, labels in zip(SampleKeys, classlabels):
w = 10
n = len(Keys)
h = math.floor(n/w) if n%w==0 else math.floor(n/w) + 1
fig, ax = plt.subplots(h, w, figsize=(15, 2*h))
ax = ax.reshape(-1,)