Created
January 6, 2018 22:41
-
-
Save gurimusan/048afbe43dccf7f96bdbbdf2af6dd3ca to your computer and use it in GitHub Desktop.
確率的勾配降下法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import re | |
import urllib2 | |
import random | |
import numpy | |
def stochatic_gradient_descent(X, y, initial_theta, alpha, num_iters=1500): | |
u"""データセットに対して確率的勾配降下法を実行し | |
目的関数を最小化するθを求める。 | |
:param numpy.ndarray X: 説明変数Xのベクトル | |
:param numpy.ndarray y: 結果Yのベクトル | |
:param numpy.ndarray initial_theta: θの初期値 | |
:param float alpha: 学習率 | |
:param int num_iters: 繰り返し回数 | |
:return: 目的関数を最小化するθ | |
""" | |
theta = numpy.copy(initial_theta) | |
xp = numpy.copy(X) | |
xp = numpy.insert(xp, 0, 1, axis=1) | |
for i in xrange(num_iters): | |
indexes = list(range(len(xp))) | |
random.shuffle(indexes) | |
for index in indexes: | |
grad = (numpy.array(xp[index], ndmin=2).dot(theta) - y[index]) * \ | |
xp[index] | |
theta = theta - (alpha * grad).T | |
return theta | |
if __name__ == '__main__': | |
datasrc = 'https://archive.ics.uci.edu/ml/machine-learning-databases/housing/housing.data' | |
data = urllib2.urlopen(datasrc).read().replace('\\n', '').splitlines() | |
data = numpy.array([[float(v) for v in re.split(" +", row.strip())] | |
for row in data]) | |
X = data[:, :-1] | |
Y = data[:, -1:] | |
initial_theta = numpy.zeros((data.shape[1], 1)) | |
alpha = 0.01 | |
num_iters = 1500 | |
# Normalize | |
X = (X - numpy.mean(X, axis=0)) / numpy.std(X, axis=0) | |
print stochatic_gradient_descent(X, Y, initial_theta, alpha, num_iters) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment