dansbecker/cross-validation

参考

メモ

いつ使うか?

端的には、データ量が少ないときの効果が大きい。 逆に、データ量が大きいときは、用いなくてもよいかもしれない。

結果論で言えば、クロスバリデーションして結果が書く回で変わらなかったとき、 それはtrain-testスプリットで十分とも言える。

共有

dansbecker/partial-dependence-plots

参考

メモ

PDPはモデルが学習されたあとに計算可能

ただし、様々なモデルに適用可能。

PDPの計算方法

以下のような感じ。

1
2
3
4
5
my_plots = plot_partial_dependence(my_model,       
features=[0, 1, 2], # column numbers of plots we want to show
X=X, # raw predictors data.
feature_names=['Distance', 'Landsize', 'BuildingArea'], # labels on graphs
grid_resolution=10) # number of values to plot on x axis

注意点として挙げられていたのは、grid_resolutionを細かくしたときに、 乱れたグラフが見られたとしても、その細かな挙動に対して文脈を考えすぎること。 どうしてもランダム性があるので、細かな挙動にいちいち気にしているとミスリードになる。

なお、partial_dependenceという関数を用いると、グラフを出力するのではなく、数値データそのものを得られる。

1
2
3
4
my_plots2 = partial_dependence(my_model,       
target_variables=[0, 1, 2], # column numbers of plots we want to show
X=X, # raw predictors data.
grid_resolution=10) # number of values to plot on x axis

なお、微妙にオプションが異なることに注意…。

タイタニックの例

PDPを見ることで、年齢や支払い料金と生存結果の関係を解釈する例が記載されていた。

「考察」に関する議論

PDPで得られた結果を考察すること自体について、議論があるようだ。 意味のある・なし、という点において。

共有

dansbecker/xgboost

参考

メモ

XGBoost自体については、XGBoostの概要がわかりやすい。

チューニングパラメータ

  • n_estimators
    • 大きさは100〜1000の間の値を用いることが多い
  • early_stopping_rounds
    • n_estimators を大きめにしておいて、本パラメータで学習を止めるのが良さそう
  • learning_rate
    • 加減しながら・・・。例では0.05あたりを使っていた。
  • n_jobs
    • 並列処理(コア数の指定)
共有

dansbecker/using-categorical-data-with-one-hot-encoding

参考

メモ

one hot encodingについて。 Kaggleのラーニングコースを読んでみた。

カテゴリ値の判定にカージナリティを利用

コメントにもやや恣意的な…と書かれてはいたが、 カージナリティが低く、dtypeが objectであるカラムをカテゴリ値としたようだ。

1
2
3
4
5
low_cardinality_cols = [cname for cname in candidate_train_predictors.columns if 
candidate_train_predictors[cname].nunique() < 10 and
candidate_train_predictors[cname].dtype == "object"]
numeric_cols = [cname for cname in candidate_train_predictors.columns if
candidate_train_predictors[cname].dtype in ['int64', 'float64']]

カラムのdtype確認

こんな感じで確かめられる。

1
train_predictors.dtypes.sample(10)

結果の例

1
2
3
4
5
6
LandContour     object
ScreenPorch int64
BsmtFinSF2 int64
SaleType object
BedroomAbvGr int64
(snip)

Pandasのget_dummies

1
one_hot_encoded_training_predictors = pd.get_dummies(train_predictors)

学習データとテストデータの列の並びを揃える

学習データとテストデータをそれぞれone hot encodingすると、 それぞれの列の並びが揃わないことがある。 scikit-learnは、列の並びに敏感である。 そこで、DataFrame#alignを用いて並びを揃える。

1
2
3
4
5
one_hot_encoded_training_predictors = pd.get_dummies(train_predictors)
one_hot_encoded_test_predictors = pd.get_dummies(test_predictors)
final_train, final_test = one_hot_encoded_training_predictors.align(one_hot_encoded_test_predictors,
join='left',
axis=1)

なお、joinオプションはSQLにおけるJOINのルールと同等だと考えれば良い。

共有

dansbecker/handling-missing-values

参考

メモ

Kaggleのhandling-missing-values の内容から気になった箇所をメモ。

欠落のあるカラムの排除

最もシンプルな方法として、欠落のあるカラムを排除する方法が挙げられていた。

1
2
3
4
cols_with_missing = [col for col in X_train.columns 
if X_train[col].isnull().any()]
reduced_X_train = X_train.drop(cols_with_missing, axis=1)
reduced_X_test = X_test.drop(cols_with_missing, axis=1)

X_train[col].isnull().any()のところで、カラム内の値のいずれかがNULL値かどうかを確認している。

欠落を埋める

欠落を埋める。

1
2
3
4
5
from sklearn.impute import SimpleImputer

my_imputer = SimpleImputer()
imputed_X_train = my_imputer.fit_transform(X_train)
imputed_X_test = my_imputer.transform(X_test)

なお、SimpleImputer#fit_transformメソッドの戻り値は、numpy.ndarrayである。

また、scikit-learnのimputeの説明を見る限り、カテゴリ値にも対応しているようだ。

欠落を埋めつつ、欠落していたことを真理値として保持

1
2
3
4
5
6
7
8
imputed_X_train_plus = X_train.copy()
imputed_X_test_plus = X_test.copy()

cols_with_missing = (col for col in X_train.columns
if X_train[col].isnull().any())
for col in cols_with_missing:
imputed_X_train_plus[col + '_was_missing'] = imputed_X_train_plus[col].isnull()
imputed_X_test_plus[col + '_was_missing'] = imputed_X_test_plus[col].isnull()
共有

Kaggle入門

参考

メモ

エントリポイント

Kaggleの環境やお作法?になれる目的で、Faster Data Science Educationを見始めた。

共有

Word2Vec with Python

共有

Init git log of an existing repository

参考

メモ

ある文書群をクラスタリングする際、TF-IDFの代わりにWord2Vecを使っていることにした。

共有

Singularization with Python

参考

メモ

condaコマンドではインストールできなかったので、pipコマンドでインストールした。 使い方は、公式ウェブサイトの通り。

なお、関係ない単語にもそのまま適用してみたが動くは動くようだ。

共有

Stopword with Python

参考

メモ

今回はConda環境だったので、 conda コマンドでインストールした。

1
$ conda install nltk

ストップワードをダウンロードする。

1
2
import nltk
nltk.download("stopwords")

ここでは、仮に対象となる単語のリストall_wordsがあったとする。 そのとき、以下のようにリストからストップワードを取り除くと良い。

1
2
symbol = ["'", '"', ':', ';', '.', ',', '-', '!', '?', "'s"]
words_wo_stopwords = [w.lower() for w in all_words if not w in stop_words + symbol]

ストップワードの中には記号が含まれていないので、ここでは、symbolを定義して一緒に取り除いた。 次に頻度の高い単語を30件抽出してみる。

1
clean_frequency = nltk.FreqDist(words_wo_stopwords)

これを可視化する。

1
2
plt.figure(figsize=(10, 7))
clean_frequency.plot(30,cumulative=True)
共有