グリッドサーチ
from sklearn.svm import SVC from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=0) print("Size of training set: {} size of test set: {}".format(X_train.shape[0], X_test.shape[0])) best_score = 0 for gamma in [0.001, 0.01, 0.1, 1, 10, 100]: for C in [0.001, 0.01, 0.1, 1, 10, 100]: svm = SVC(gamma=gamma, C=C) svm.fit(X_train, y_train) score = svm.score(X_test, y_test) if score > best_score: best_score = score best_parameters = {'C': C, 'gamma': gamma} print("Best score: {:.2f}".format(best_score)) print("Best parameters: {}".format(best_parameters))
パラメータの過剰適合の危険性と検証セット
- 過剰適合を防ぐために,データを以下のように分ける.
- 訓練セット(train): モデルを構築するために用いる
- 検証セット(valid): モデルのパラメータを選択するために用いる
- テストセット(test): 選択したパラメータの性能を評価するために用いる
交差検証を用いたグリッドサーチ
from sklearn.model_selection import cross_val_score import numpy as np for gamma in [0.001, 0.01, 0.1, 1, 10, 100]: for C in [0.001, 0.01, 0.1, 1, 10, 100]: svm = SVC(gamma=gamma, C=C) scores = cross_val_score(svm, X_trainval, y_trainval, cv=5) score = np.mean(scores) if score > best_score: best_score = score best_parameters = {'C': C, 'gamma': gamma} svm = SVC(**best_parameters) svm.fit(X_trainval, y_trainval)
参考
グリッドサーチ
from sklearn.svm import SVC from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=0) print("Size of training set: {} size of test set: {}".format(X_train.shape[0], X_test.shape[0])) best_score = 0 for gamma in [0.001, 0.01, 0.1, 1, 10, 100]: for C in [0.001, 0.01, 0.1, 1, 10, 100]: svm = SVC(gamma=gamma, C=C) svm.fit(X_train, y_train) score = svm.score(X_test, y_test) if score > best_score: best_score = score best_parameters = {'C': C, 'gamma': gamma} print("Best score: {:.2f}".format(best_score)) print("Best parameters: {}".format(best_parameters))
参考
グリッドサーチ
from sklearn.svm import SVC from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=0) print("Size of training set: {} size of test set: {}".format(X_train.shape[0], X_test.shape[0])) best_score = 0 for gamma in [0.001, 0.01, 0.1, 1, 10, 100]: for C in [0.001, 0.01, 0.1, 1, 10, 100]: svm = SVC(gamma=gamma, C=C) svm.fit(X_train, y_train) score = svm.score(X_test, y_test) if score > best_score: best_score = score best_parameters = {'C': C, 'gamma': gamma} print("Best score: {:.2f}".format(best_score)) print("Best parameters: {}".format(best_parameters))
pythonでファイルから文字列を読み込む
pythonでファイルから文字列を読み込む
- スペース区切りの単語が入ったファイルから読み込みリストへ格納する
with open(file, "r") as fh: words_lst = fh.read().rstrip().split(" ")
- 簡潔にかけてかっこいい
業務日誌をgoogle driveに残す~Gdrive~
目的
研究日誌と研究時間を残したい。 以下のような条件で残したい。
- google drive に.txtで残したい
- 作業時間も残したい
- 手元のエディタで編集して、uploadしたい
方法
gdriveを使う
gdrive gdriveはgoogledrive周りをCLIで実行できる。
gdriveの使い方
インストール
brew install gdrive
google drive のIDをURLから抜き出す
tokenがdriveのURLに埋め込まれている。 upload先のdriveのフォルダをブラウザで開いて、"drive.google.com/path/to/file/folders/hogehogehoge"というURLの最後を抜き出してくる。
uploadする
gdrive upload {ファイル名} -p {ID}
注意点
同じファイルをuploadコマンドでアップロードすると、別ファイルとして認識される。
shell script
uploadを自動化したい。shellscriptを書けば、時間指定で実行すれば、自動でuploadされる。今回は自動化まではしない。
#!/bin/sh # ファイルパス(google drive のフォルダurlの最後がid) ID=hoge # 日付 DATE=`date "+%Y-%m-%d"` # アップロード # 引数をファイル名で取る # 引数がない場合、当日の日誌をuploadする if [$# -ne 1];then # ファイルがない場合、作成する事もできるが、作成はしない。 if [ -e ${DATE}.txt ];then gdrive upload ${DATE}.txt -p ${ID} fi exit 1 else # 引数をuploadする gdrive upload $1 -p ${ID} fi exit 0
結論
gdiveはディレクトリごと同期するsyncコマンドがある。これをローカルが優先する設定にすれば、ローカルの変更がdriveに反映される。 今回はシンプルなuploadオプションを使った。
リモートでgit commitしてからリモート(github)へpushする方法
リモートでgit commitしてからリモート(github)へpushする方法
普通は、githubでnewRepositoryを押して、git init @hogeをターミナルで実行する。
しかし、今回はローカルで
git init git commit -m "" git push
したことによりエラーが出た。
エラー
要はRepositoryを作れというエラー
git push [master] fatal: No configured push destination. Either specify the URL from the command-line or configure a remote repository using git remote add <name> <url> and then push using the remote name git push <name>
解決策
git remote add origin git@github.com:alexpchin/<reponame>.git git push -u origin master
gitのみで完結できないかと調べたけど面倒なので終わり。