features와 targets 만들기
이제 Machine Learning에 바로 쓸 수 있는 features와 targets을 거의 준비했어요. 현재 가격 변화(5d_close_pct)와 각종 지표(이동 평균과 RSI)에서 features를 만들었고, 미래 가격 변화(5d_close_future_pct)를 target으로 만들었습니다. 이제 이것들을 각각 별도의 numpy 배열로 분리해 Machine Learning 알고리즘에 넣을 수 있게 해야 해요.
지표 계산 때문에 DataFrame의 앞부분에는 결측치가 생깁니다. 이 데이터를 뒤로 채우거나(backfill), 하나의 값으로 채우거나, 해당 행을 삭제할 수 있어요. 행을 삭제하는 방법이 좋습니다. 그래야 Machine Learning 알고리즘이 backfill이나 0으로 채운 데이터에 혼란을 겪지 않아요. pandas에는 결측치가 있는 행을 삭제하는 .dropna() 함수가 있으니 이를 사용하겠습니다.
이 연습은 강의의 일부입니다
Python으로 배우는 금융 분야 Machine Learning
연습 안내
- pandas의
.dropna()로lng_df에서 결측치를 삭제하세요. - 우리의 target인
'5d_close_future_pct'값을 담는 변수를 만드세요. - 상관관계를 확인할 수 있도록 target(
5d_close_future_pct)과 feature(기존 리스트feature_names에 포함)를 모두 담은 DataFrame을 만드세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Drop all na values
lng_df = lng_df.____
# Create features and targets
# use feature_names for features; '5d_close_future_pct' for targets
features = lng_df[feature_names]
targets = lng_df[____]
# Create DataFrame from target column and feature columns
feature_and_target_cols = ['5d_close_future_pct'] + ____
feat_targ_df = lng_df[feature_and_target_cols]
# Calculate correlation matrix
corr = feat_targ_df.corr()
print(corr)