본문 바로가기
IT 공부/Machine Learning

머신 러닝 학습_2

by 랜턴K 2024. 6. 18.
반응형

Train 데이터를 통해 학습을 했기 때문에,

Train 데이터에 대한 예측은 매우 정확할 수 밖에 없다. 

다른 말로, Train 데이터를 갖고 예측하는 것은 너무 당연한 결과라서 의미가 없다는 말이다. 

어떤 학생에게 정답을 이미 알려주었는데,

그 학생이 시험을 100점 맞았다고 해서,

그 학생이 공부를 열심히해서, 모든 내용에 통달해서 100점 맞았다라고 얘기할 수 없는 것과 같다.

 

따라서, 갖고 있는 데이터 중 Train 데이터 셋과 Test 셋을 나누고 

Train 데이터 셋으로 학습을 시킨 후

학습시키지 않은 Test 데이터 셋으로 평가를 진행한다. 

 

sklearn 라이브러리 model_selection 모듈에서 train_test_split 함수를 임포트한다 

Train 데이터 셋과 Test 데이터 셋을 분리한다 

Train 데이터 셋으로만 학습을 시키고,

Test 데이터 셋의 인풋 데이터만을 이용 예측한다.

예측된 값을 Test 데이터 셋의 실제 값과 비교하여 정확도를 측정한다 

from sklearn.model_selection import train_test_split

# split data into training and validation data, for both features and target
# The split is based on a random number generator. Supplying a numeric value to
# the random_state argument guarantees we get the same split every time we
# run this script.
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 0)
# Define model
melbourne_model = DecisionTreeRegressor()
# Fit model
melbourne_model.fit(train_X, train_y)

# get predicted prices on validation data
val_predictions = melbourne_model.predict(val_X)
print(mean_absolute_error(val_y, val_predictions))
반응형

'IT 공부 > Machine Learning' 카테고리의 다른 글

머신 러닝 학습 5. 범주형 데이터 (Categorial Variable)  (0) 2024.07.01
머신 러닝 학습_4  (0) 2024.06.25
머신 러닝 학습_3  (0) 2024.06.20
머신러닝 학습_1  (0) 2024.06.17
머신 러닝 공부 시작  (1) 2024.06.16