[TensorFlow1.0] Multiple Linear Regression Start

BioinformaticsAndMe










[TensorFlow] Multiple Linear Regression(MLR)?


: TensorFlow 1.0 에서 수행되는 다중선형회귀분석(Multiple Linear Regression;MLR) 과정

*Multiple Linear Regression - 설명변수(독립변수)가 2개 이상이고, 종속변수가 1개인 회귀모형을 모델링하는 기법

: 다중선형회귀분석은 여러 독립변수를 도입함으로써, 오차값을 줄이고 결과 정확도를 높임


: 아래 표를 기반해 'Profit'을 종속변수(Dependent variable)로, 나머지를 독립변수(Independent variable)에 두는 다중회귀모형을 만들 수 있음





[TensorFlow] MLR 예제


: 텐서플로우 MLR 분석을 위한, 기업 공채 지원자 점수 예제표

지원자

1차 서류

2차 인적성 

3차 면접 

최종 점수

65

90

85

240

80

88

92

260

70

75

95

240

75

85

65

225

90

85

85

260

# '__future__' : python 2에서 python 3 문법 사용 가능

from __future__ import absolute_import, division, print_function

# 텐서플로우 라이브러리 임포트 import tensorflow as tf

# 예제 데이터

x_data = [[65., 90., 85.], [80., 88., 92.], [70., 75., 95.], [75., 85., 65.], [90., 85., 85.]] y_data = [[240.], [260.], [240.], [225.], [260.]]

# 프로그램 실행 순간에 변수값을 입력하기 위해 placedholder 함수 사용 X = tf.placeholder(tf.float32, shape=[None, 3]) Y = tf.placeholder(tf.float32, shape=[None, 1])

# 텐서플로우에서 학습될 W(Weight) 및 b(bias) 값을 변수(Variable) 노드로 정의

# W와 b 값의 초기값 정보가 없기에 랜덤하게 값을 설정 W = tf.Variable(tf.random_normal([3,1]), name='weight') b = tf.Variable(tf.random_normal([1]), name='bias')

# 다중회귀모형 가설 정의 (matmul 함수로 행렬곱 수행)

hypothesis = tf.matmul(X, W) + b


# cost function 정의 (reduce_mean 함수로 평균 계산) cost = tf.reduce_mean(tf.square(hypothesis - Y))


# 최적화를 위한 경사하강법 정의

optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5)


# minimize 함수를 사용해 cost 함수의 최소값을 얻음 train = optimizer.minimize(cost)

# 세션을 생성하고 그래프의 모든 전역변수를 초기화 sess = tf.Session() sess.run(tf.global_variables_initializer())

# 그래프를 실행하고 feed_dict를 통해 실행 초기값을 입력함 for step in range(1001): cost_val, hy_val, _ = sess.run( [cost, hypothesis, train], feed_dict={X: x_data, Y: y_data} ) if step%200==0: print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val)

0 Cost: 8397.166 Prediction: [[169.02368] [165.52513] [160.76976] [130.99226] [146.38747]] 200 Cost: 93.65354 Prediction: [[254.29305] [260.94043] [245.62996] [218.28816] [246.3491 ]] 400 Cost: 53.146355 Prediction: [[251.37286] [260.50125] [243.09482] [221.05096] [249.46597]] 600 Cost: 31.487442 Prediction: [[249.10394] [260.2357 ] [241.52197] [222.79031] [251.7963 ]] 800 Cost: 19.353357 Prediction: [[247.3255 ] [260.0807 ] [240.56805] [223.86734] [253.5586 ]] 1000 Cost: 12.246923 Prediction: [[245.92062] [259.99527] [240.00885] [224.5187 ] [254.90598]]





#Reference

1) https://github.com/aymericdamien/TensorFlow-Examples/tree/master/tensorflow_v2

2) https://medium.com/@manjabogicevic/multiple-linear-regression-using-python-b99754591ac0

3) http://contents.kocw.net/document/ch5_6.pdf





[TensorFlow1.0] Multiple Linear Regression End

BioinformaticsAndMe

+ Recent posts