import os
import numpy as np
# An example in that book, the training set and parameters' sizes are fixed
training_set = np.array([[[3, 3], 1], [[4, 3], 1], [[1, 1], -1]])
a = np.array([0, 0, 0])
b = 0
Gram = np.array([])
y = np.array(range(len(training_set))).reshape(1, 3)# used for labels
x = np.array(range(len(training_set)) * 2).reshape(3, 2)# used for features
# calculate the Gram matrix
def cal_gram():
g = np.array([[0, 0, 0], [0, 0, 0], [0, 0, 0]])
for i in range(len(training_set)):
for j in range(len(training_set)):
g[i][j] = np.dot(training_set[i][0], training_set[j][0])
return g
# update parameters using stochastic gradient descent
def update(i):
global a, b
a[i] = a[i] + 1
b = b + training_set[i][1]
# print a, b # you can uncomment this line to check the process of stochastic gradient descent
# calculate the judge condition
def cal(key):
global a, b, x, y
i = 0
for item in training_set:
y[0][i] = item[1]
i = i + 1
temp = a * y
res = np.dot(temp, Gram[key])
res = (res + b) * training_set[key][1]
return res[0]
# check if the hyperplane can classify the examples correctly
def check():
global a, b, x, y
flag = False
for i in range(len(training_set)):
if cal(i) <= 0:
flag = True
update(i)
if not flag:
i = 0
for item in training_set:
y[0][i] = item[1]
x[i] = item[0]
i = i + 1
temp = a * y
w = np.dot(temp, x)
print "RESULT: w: " + str(w) + " b: "+ str(b)
os._exit(0)
flag = False
if __name__=="__main__":
Gram = cal_gram()# initialize the Gram matrix
for i in range(1000):
check()
print "The training_set is not linear separable. "
2016年11月29日火曜日
【メモ】svm実装
import numpy
from matplotlib import pyplot
import sys
def f(x, y):
return x - y
if __name__ == '__main__':
param = sys.argv
numpy.random.seed()
N = 30
d = 2
X = numpy.random.randn(N, d)
T = numpy.array([1 if f(x, y) > 0 else - 1 for x, y in X])
alpha = numpy.zeros(N)
beta = 1.0
eta_al = 0.0001 # update ratio of alpha
eta_be = 0.1 # update ratio of beta
itr = 1000
for _itr in range(itr):
for i in range(N):
delta = 1 - (T[i] * X[i]).dot(alpha * T * X.T).sum() - beta * T[i] * alpha.dot(T)
alpha[i] += eta_al * delta
for i in range(N):
beta += eta_be * alpha.dot(T) ** 2 / 2
index = alpha > 0
w = (alpha * T).T.dot(X)
b = (T[index] - X[index].dot(w)).mean()
if '-d' in param or '-s' in param:
seq = numpy.arange(-3, 3, 0.02)
pyplot.figure(figsize = (6, 6))
pyplot.xlim(-3, 3)
pyplot.ylim(-3, 3)
pyplot.plot(seq, -(w[0] * seq + b) / w[1], 'k-')
pyplot.plot(X[T == 1,0], X[T == 1,1], 'ro')
pyplot.plot(X[T == -1,0], X[T == -1,1], 'bo')
if '-s' in param:
pyplot.savefig('graph.png')
if '-d' in param:
pyplot.show()
from matplotlib import pyplot
import sys
def f(x, y):
return x - y
if __name__ == '__main__':
param = sys.argv
numpy.random.seed()
N = 30
d = 2
X = numpy.random.randn(N, d)
T = numpy.array([1 if f(x, y) > 0 else - 1 for x, y in X])
alpha = numpy.zeros(N)
beta = 1.0
eta_al = 0.0001 # update ratio of alpha
eta_be = 0.1 # update ratio of beta
itr = 1000
for _itr in range(itr):
for i in range(N):
delta = 1 - (T[i] * X[i]).dot(alpha * T * X.T).sum() - beta * T[i] * alpha.dot(T)
alpha[i] += eta_al * delta
for i in range(N):
beta += eta_be * alpha.dot(T) ** 2 / 2
index = alpha > 0
w = (alpha * T).T.dot(X)
b = (T[index] - X[index].dot(w)).mean()
if '-d' in param or '-s' in param:
seq = numpy.arange(-3, 3, 0.02)
pyplot.figure(figsize = (6, 6))
pyplot.xlim(-3, 3)
pyplot.ylim(-3, 3)
pyplot.plot(seq, -(w[0] * seq + b) / w[1], 'k-')
pyplot.plot(X[T == 1,0], X[T == 1,1], 'ro')
pyplot.plot(X[T == -1,0], X[T == -1,1], 'bo')
if '-s' in param:
pyplot.savefig('graph.png')
if '-d' in param:
pyplot.show()
2016年11月14日月曜日
Postgresqlでto_chorを使用してintegerからString変換
■
select to_char(5, '09');
実行した結果は
_05
である。スペースが入れている。
■
select to_char(5, 'FM09');
実行した結果は
05
である。スペースが入れていない。
select to_char(5, '09');
実行した結果は
_05
である。スペースが入れている。
■
select to_char(5, 'FM09');
実行した結果は
05
である。スペースが入れていない。
java日付比較時millisecond注意
Date dt3 = dateFormat("20161001", "yyyyMMdd");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(1475247600132L));
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = 1;
cal.set(year, month, day, 0, 0, 0);
Date dt4 = cal.getTime();
System.out.println(dt3.getTime()); //出力:1475247600000
System.out.println(dt4.getTime()); //出力:1475247600132
Calendar calnoMillSecond = Calendar.getInstance();
calnoMillSecond.setTime(new Date(1475247600132L));
int year1 = cal.get(Calendar.YEAR);
int month1 = cal.get(Calendar.MONTH);
int day1 = 1;
calnoMillSecond.set(year1, month1, day1, 0, 0, 0);
calnoMillSecond.set(Calendar.MILLISECOND, 0);
Date dt5 = calnoMillSecond.getTime();
System.out.println(dt3.getTime()); //出力:1475247600000
System.out.println(dt5.getTime()); //出力:1475247600000
ミリ秒の0設定を注意。
讲座PPT
http://toritris.weebly.com/
http://www.massey.ac.nz/~mjjohnso/notes/59302/l10.html
https://cw.fel.cvut.cz/wiki/_media/courses/ae3m33ui/lectures/linclass-slides.pdf
http://wenku.baidu.com/link?url=V3cS4pGEbT0N1BUdgFtprvwVL7C5VpmLNbfk-TorMJAu_oXPi_jL7mNL23aYjEkGH4nFUDGvtTy062ROrOOVeiba0G8gjLNyPtEjIEtHyAK
java source資料
http://lcn.epfl.ch/tutorial/english/
https://datasciencelab.wordpress.com/2014/01/10/machine-learning-classics-the-perceptron/
https://appliedgo.net/perceptron/
https://www.reddit.com/r/MachineLearning/comments/15zrpp/please_explain_support_vector_machines_svm_like_i/
why sigmoid not step
http://fedc.wiwi.hu-berlin.de/fedc_homepage/xplore/tutorials/sfehtmlnode96.html
点到超平面计算
https://segmentfault.com/a/1190000005129669
http://blog.csdn.net/han_xiaoyang/article/details/50856583
Must Know Tips/Tricks in Deep Neural Networks
http://www.roguewave.com/getattachment/80bd5b86-ea29-457a-a0e1-db07e4cc5157/Neural-Networks-An-Introductiontarget=
https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/
http://www.massey.ac.nz/~mjjohnso/notes/59302/l10.html
https://cw.fel.cvut.cz/wiki/_media/courses/ae3m33ui/lectures/linclass-slides.pdf
http://wenku.baidu.com/link?url=V3cS4pGEbT0N1BUdgFtprvwVL7C5VpmLNbfk-TorMJAu_oXPi_jL7mNL23aYjEkGH4nFUDGvtTy062ROrOOVeiba0G8gjLNyPtEjIEtHyAK
java source資料
http://lcn.epfl.ch/tutorial/english/
https://datasciencelab.wordpress.com/2014/01/10/machine-learning-classics-the-perceptron/
https://appliedgo.net/perceptron/
https://www.reddit.com/r/MachineLearning/comments/15zrpp/please_explain_support_vector_machines_svm_like_i/
why sigmoid not step
http://fedc.wiwi.hu-berlin.de/fedc_homepage/xplore/tutorials/sfehtmlnode96.html
点到超平面计算
https://segmentfault.com/a/1190000005129669
http://blog.csdn.net/han_xiaoyang/article/details/50856583
Must Know Tips/Tricks in Deep Neural Networks
http://www.roguewave.com/getattachment/80bd5b86-ea29-457a-a0e1-db07e4cc5157/Neural-Networks-An-Introductiontarget=
https://mattmazur.com/2015/03/17/a-step-by-step-backpropagation-example/
登録:
投稿 (Atom)