Logistic Regression 모델 학습
데이터에 대한 레이블과 특징을 만든 후, 이제 그로부터 학습할 수 있는 모델을 구축할 준비가 되었어요(학습). 하지만 모델을 학습시키기 전에, 이 연습 문제의 마지막 파트에서는 데이터를 학습용과 테스트용으로 분할하고, 학습용 데이터로 Logistic Regression 모델을 실행한 다음, 학습된 모델의 정확도를 확인해 보겠습니다.
워크스페이스에는 SparkContext sc와 samples 변수가 이미 준비되어 있다는 점을 기억하세요.
이 연습은 강의의 일부입니다
PySpark로 배우는 빅데이터 기초
연습 안내
- 결합된 데이터를 80:20 비율로 학습용과 테스트용으로 분할하세요.
- 학습용 데이터셋으로 Logistic Regression 모델을 학습하세요.
- 테스트 데이터셋에 대해 학습된 모델로 예측 레이블을 생성하세요.
zip함수를 사용해 테스트 데이터셋의 레이블과 예측 데이터셋의 레이블을 결합하세요.- 원래 레이블과 예측 레이블을 사용해 학습된 모델의 정확도를 계산하고 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Split the data into training and testing
train_samples,test_samples = samples.____([0.8, 0.2])
# Train the model
model = LogisticRegressionWithLBFGS.train(____)
# Create a prediction label from the test data
predictions = model.____(test_samples.map(lambda x: x.features))
# Combine original labels with the predicted labels
labels_and_preds = test_samples.map(lambda x: x.label).zip(____)
# Check the accuracy of the model on the test data
accuracy = labels_and_preds.filter(lambda x: x[0] == x[____]).count() / float(test_samples.count())
print("Model accuracy : {:.2f}".format(____))