Skip to content

Instantly share code, notes, and snippets.

View alfredplpl's full-sized avatar

Alfred Increment alfredplpl

View GitHub Profile
import json
#make a test data.
testHash={"A":10,"B":20.0,"C":[30.0,40]}
#dump the variable as a file
with open("E:/test.json","w") as f:
json.dump(testHash,f)
#load from a json file
>>> import json
>>> testHash={"A":10,"B":20,"C":30.0}
with open("E:/test.json","w") as f:
json.dump(testHash,f)
>>> testHash
{'A': 10, 'C': 30.0, 'B': 20}
#delete the next # if rjson package is not installed in the system
#install.packages("rjson")
library("rjson")
#load from a json file
data=fromJSON(file="E:/test.json")
#check the data
print(data)
@alfredplpl
alfredplpl / MinibatchSGDRegressor.py
Last active August 29, 2015 14:21
ミニバッチSGDです。Kaggle見ても、作れとしか書かれてなかったので作りました。
# -*- coding: utf-8 -*-
# This code is distributed under the 3-Clause BSD license (New BSD license).
# 基本的に作者の名前を書いていただければ、商用利用も可能です。なお、保証はしません。
# 参考URL: http://osdn.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license
from sklearn import linear_model
import Image
import numpy as np
from sklearn.cross_validation import ShuffleSplit
class MinibatchSGDRegressor(linear_model.SGDRegressor):
@alfredplpl
alfredplpl / MultivariateMinibatchSGDR.py
Last active August 29, 2015 14:21
多変量版のミニバッチSGDです。マルチタスク学習でないので、効率はすごく悪いです
# -*- coding: utf-8 -*-
# This code is distributed under the 3-Clause BSD license (New BSD license).
# 基本的に作者の名前を書いていただければ、商用利用も可能です。なお、保証はしません。
# 参考URL: http://osdn.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license
from sklearn import linear_model
import Image
import numpy as np
from sklearn.cross_validation import ShuffleSplit
from sklearn.metrics import r2_score
@alfredplpl
alfredplpl / cmpPickle.py
Last active August 29, 2015 14:21
自動で圧縮してくれるPickleです。今までのコードに「from cmpPickle import CompressedPickle as pickle」と最初に書くと、そのまま使えます。Auto compressing pickle.
# -*- coding: utf-8 -*-
# This code is distributed under the 3-Clause BSD license (New BSD license).
# 日本の方へ
# 基本的に作者の名前を書いていただければ、商用利用も可能です。なお、保証はいたしかねます。
# 参考URL: http://osdn.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license
# References: https://docs.python.org/3/library/gzip.html
# http://henrysmac.org/blog/2010/3/15/python-pickle-example-including-gzip-for-compression.html
@alfredplpl
alfredplpl / shareMemory.py
Last active August 29, 2015 14:22
Pythonでよく迷うプロセス間のメモリ共有のサンプルを書きました。
# -*- coding: utf-8 -*-
# This code is distributed under the 3-Clause BSD license (New BSD license).
# 基本的に作者の名前を書いていただければ、商用利用も可能です。なお、保証はいたしかねます。
from multiprocessing import Manager,Pool
def f(param):
l,s=param
for i in range(10):
t=[x+i+s for x in l]
@alfredplpl
alfredplpl / randomForestImp.R
Last active December 23, 2015 03:49
How to use the variable important of Random forest.
library(randomForest)
#「フィッシャーのあやめ」データセットを読み出す
data(iris)
#データセットを特徴量とラベルに分割
features<-iris[1:4]
labels<-iris[5]
#ラベルを因子化
@alfredplpl
alfredplpl / randomForestImpResult.R
Last active December 23, 2015 03:49
Sample of variable importance.
setosa versicolor virginica MeanDecreaseAccuracy MeanDecreaseGini
Sepal.Length 7.277437 7.0994514 8.087167 10.842432 9.553025
Sepal.Width 5.029336 -0.4402422 2.381594 3.438372 2.528796
Petal.Length 22.909121 32.9889332 30.209000 35.201262 43.785432
Petal.Width 21.741775 30.5797963 31.309059 32.724397 43.400080
@alfredplpl
alfredplpl / BagOfFeaturesGMM.py
Last active December 25, 2015 02:19
This is a class of Bag-of-Features by GMM
#see also: http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GMM.html
import numpy as np
from sklearn import mixture,preprocessing
class BagOfFeaturesGMM:
"""This is a class of Bag-of-Features by GMM """
codebookSize=0
classifier=None
def __init__(self, codebookSize):