Gradient Decent를 이용한 로지스틱 회귀 구현 (2)

Numpy를 이용한 구현

데이터 불러오기

import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
data = pd.read_csv('assignment2.csv')
data.head()

Label

bias

experience

salary

0

1

1

0.7

48000

1

0

1

1.9

48000

2

1

1

2.5

60000

3

0

1

4.2

63000

4

0

1

6.0

76000

Train Test 데이터 나누기

X_train, X_test, y_train, y_test = train_test_split(data.iloc[:, 1:], data.iloc[:, 0], random_state = 0)
X_train.shape, X_test.shape, y_train.shape, y_test.shape
((150, 3), (50, 3), (150,), (50,))

데이터 스케일링

experience와 salary를 스케일링한다.

bias

experience

salary

71

1

5.3

48000

124

1

8.1

66000

184

1

3.9

60000

97

1

0.2

45000

149

1

1.1

66000

bias

experience

salary

0

1

0.187893

-1.143335

1

1

1.185555

0.043974

2

1

-0.310938

-0.351795

3

1

-1.629277

-1.341220

4

1

-1.308600

0.043974

bias

experience

salary

0

1

-1.344231

-0.615642

1

1

0.508570

0.307821

2

1

-0.310938

0.571667

3

1

1.363709

1.956862

4

1

-0.987923

-0.747565

1. sigmoid

2. log likelihood

3. gradient Ascent

get_gradients는 cost function(log likelihood)상에서 각각의 beta 계수들로 편미분했을 때, 각각의 기울기를 구하는 함수이다.

step은 구한 기울기를 바탕으로 다음 학습을 진행할 지점을 지정하는 함수이다.

4. Fitting

cf.) Step Size를 고르는 기법으로는 다음 세가지 방법이 있다.

  • Fixed step size

  • Backtracking line search

  • Exact line search

참고 : https://wikidocs.net/18088

5. 예측

6. confusion_matrix

데이터 출처: Data Science from Scratch: First Principles with Python (2015)

Last updated

Was this helpful?