Created
January 27, 2016 13:49
Star
You must be signed in to star a gist
This file contains 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
import random | |
def create_datas(): | |
datas = [] | |
for i in range(50): | |
a = random.random() | |
datas.append((a, 3 * a + 2 + (random.random() - 0.5))) | |
return datas | |
def regression_analysis(datas): | |
# x, yの平均値を求める | |
x_ave = sum([d[0] for d in datas]) / len(datas) | |
y_ave = sum([d[1] for d in datas]) / len(datas) | |
# x, yの分散、xyの共分散を求める | |
c_xy = sum([(d[0] - x_ave) * (d[1] - y_ave) for d in datas]) / len(datas) | |
d_x = sum([(d[0] - x_ave) ** 2 for d in datas]) / len(datas) | |
d_y = sum([(d[1] - y_ave) ** 2 for d in datas]) / len(datas) | |
# 回帰方程式のパラメータa, bを求める | |
a = (c_xy / d_x) | |
b = (y_ave - (c_xy / d_x) * x_ave) | |
print('y = {a}x + {b}'.format(a=a, b=b)) | |
# yの予測値hat_yの分散を求めたら決定係数を求める | |
hat_y = [a * d[0] + b for d in datas] | |
d_hat_y = sum([(hy - y_ave) ** 2 for hy in hat_y]) / len(datas) | |
r2 = d_hat_y / d_y | |
print('R^2 = {r2}'.format(r2=r2)) | |
ds = create_datas() | |
regression_analysis(ds) | |
# example | |
# | |
# => y = 2.953256222497783x + 2.058175130442012 | |
# => R^2 = 0.9059711404708862 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment