備忘録的な

プログラミングや機械学習に関する備忘録

PyPIへのパッケージ登録

  • .pypircをホームディレクトリ(Win8ならC:\Users\Owner)

※パスワード直書きなので注意

[distutils] # this tells distutils what package indexes you can push to
index-servers =
    pypi # the live PyPI
    pypitest # test PyPI

[pypi] # authentication details for live PyPI
repository: https://pypi.python.org/pypi
username: {{your_username}}
password: {{your_password}}

[pypitest] # authentication details for test PyPI
repository: https://testpypi.python.org/pypi
username: {{your_username}}
  • testpypiへの登録
python setup.py register -r https://testpypi.python.org/pypi
  • ファイルのアップロード
python setup.py sdist --formats=gztar,zip bdist_wininst upload -r pypitest
  • pypitestをpypiに変えて実行

※ファイルを変更するにはサイト上で一度removeするか,バージョン番号を上げる
※uploadまえに単にsdistでパッケージを作成し確認すべし
※pyファイル以外をアップロードするには,setup.pyに

package_data={"パッケージDir": ["ファイルへの相対パス"]}

を追記するだけでなく,MANIFEST.inに

include パッケージDir/ファイルへの相対パス

を記載する必要有

READMEのようなメタデータを再度インデクスに提出するには,

python setup.py register

GitHubに関する参考サイト

サルでもわかるGit入門 〜バージョン管理を使いこなそう〜 | どこでもプロジェクト管理バックログ

git status
git add .
git commit -m "comment"
git push -u <name> master

チケット駆動開発

  1. github上でissueを作成する
  2. トピックブランチを作成する
git checkout -b issues/#?
  1. 開発する
  2. リモートリポジトリにpushする
git add .
git commit -m "comments"
git push hoge issues/#?
  1. github上でpull requestを作成する
  2. github上でpull requestをmergeする
  3. github上でbrnchをdeleteする
  4. ローカルのmasterを最新にする
git checkout master
git pull --prune
  1. ローカルのブランチを削除する
git branch -d issues/#?

ファイルの削除

  • ファイルの削除
git rm hoge.py
  • コミット情報は削除するがローカルのファイルは残す
git rm --cached hoge.py
  • ディレクトリごと削除
git rm -r --cached foo

Pythonのパッケージ化に関する参考サイト

モジュールのインポート方法いろいろ — Python School 2.0.0 documentation

GithubにPythonのライブラリをあげてpipでインストールする | junion blog

tkamishima (Toshihiro Kamishima) · GitHub

[Python] setuptools - SumiTomohikoの日記

https://pypi.python.org/pypi?%3Aaction=list_classifiers

Python - setup.py作成 - Qiita

pip install https://github.com/user/project/zipball/master
pip install -U https://github.com/user/project/zipball/master
pip uninstall project
  • branch名のissue番号のように#を含む場合,#ではなく%23とする
pip install https://github.com/user/project/zipball/issues/%2320

scikit-learn 0.15.0

scikit-learnが0.15.0になってRandom Forestが速くなったと言っているので試してみました.

Huge speed and memory improvements to random forests (and extra trees) that also benefit better from parallel computing

ベンチマーク用のコードは以下

total = 0.0
for i in xrange(5):
    st = time.clock()
    clf = RandomForestClassifier(n_estimators=100, random_state=1, n_jobs=5)
    clf.fit(x, y)
    pred = clf.predict(x)
    total += time.clock() - st

print total / 5

xは(52704, 21)です.

結果.

0.14.1 0.15.0
処理時間[sec] 8.249 3.726

倍程度速くなってますね.これは嬉しい.

なを,scikit-learnを0.15.0にしたところ,

ImportError: cannot import name inplace_column_scal

というエラーがでるようになってしまいました.
これは,
C:\Python27\Lib\site-packages\sklearn\utils\sparsefuncs.pyd
を削除することで解決しました.

pandas備忘録

data = pd.io.parsers.read_csv('c:\test.csv',
                                sep=',', header=None, na_values=['-'],
                                dtype={0:'str', 1:'str', 2:'str', 3:'str'})
  • 文字列置換
data[0].replace('(\d{4})(\d{2})(\d{2})', '\\1-\\2-\\3', regex=True, inplace=True)
  • groupby
data = data.groupby(0)[6].agg({'mean':np.mean, 'std':np.std,
                            'max':lambda x:np.max(x, axis=1),
                            'min':lambda x:np.min(x, axis=1)})