선형 회귀

Python으로 시작하는 TensorFlow

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

선형 회귀란?

이 이미지는 주택 면적(제곱피트)의 자연로그와 주택 가격(달러)의 자연로그 간 산점도를 보여 줍니다.

Python으로 시작하는 TensorFlow

선형 회귀란?

이 이미지는 주택 면적과 가격의 자연로그 산점도에 회귀선을 적합한 모습을 보여 줍니다.

Python으로 시작하는 TensorFlow

선형 회귀 모델

  • 선형 회귀 모델은 선형 관계를 가정합니다:
    • $price = intercept + size*slope + error$
  • 이는 단변량 회귀 예시입니다.
    • 특성은 size 하나뿐입니다.
  • 다중 회귀는 특성이 둘 이상입니다.
    • 예: size, location
Python으로 시작하는 TensorFlow

TensorFlow로 선형 회귀

# Define the targets and features
price = np.array(housing['price'], np.float32)
size = np.array(housing['sqft_living'], np.float32)

# Define the intercept and slope
intercept = tf.Variable(0.1, np.float32)
slope = tf.Variable(0.1, np.float32)
# Define a linear regression model
def linear_regression(intercept, slope, features = size):
    return intercept + features*slope
# Compute the predicted values and loss
def loss_function(intercept, slope, targets = price, features = size):
    predictions = linear_regression(intercept, slope)
    return tf.keras.losses.mse(targets, predictions)
Python으로 시작하는 TensorFlow

TensorFlow로 선형 회귀

# Define an optimization operation
opt = tf.keras.optimizers.Adam()
# Minimize the loss function and print the loss
for j in range(1000):
    opt.minimize(lambda: loss_function(intercept, slope),\
    var_list=[intercept, slope])
    print(loss_function(intercept, slope))
tf.Tensor(10.909373, shape=(), dtype=float32)
...
tf.Tensor(0.15479447, shape=(), dtype=float32)
# Print the trained parameters
print(intercept.numpy(), slope.numpy())
Python으로 시작하는 TensorFlow

연습해 봅시다!

Python으로 시작하는 TensorFlow

Preparing Video For Download...