복습: 정리하고 예측하기
Coddy Machine Learning 여정의 지도 학습 섹션에 포함된 레슨. 58개 중 49번째.
챌린지
중급어떤 캠퍼들이 다시 올까요? camp.csv에는 단어, 숫자, 결측값이 섞여 있습니다. 시작 코드에서는 데이터를 나누고 캠퍼 한 명의 정보를 읽습니다.
- 이 장의 전체 파이프라인을 만드세요.
cabin과activity는 원-핫 인코딩하고, 숫자에는 결측값을 중앙값으로 채운 다음 스케일링하는 경로를 적용한 뒤, 로지스틱 회귀로 마무리하세요. - 학습 데이터 행을 사용한 5겹 교차 검증(CV) 정확도의 평균과 표준 편차(std)를 출력하세요.
- 학습 데이터 행으로 모델을 학습시키고, 테스트 정확도와 캠퍼가 돌아올 확률을 출력하세요.
다른 캠프의 캠퍼 정보: 10, lakeside, archery, 21, -, 4:
CV accuracy: mean 0.707, std 0.065
Test accuracy: 0.740
Chance of coming back: 0.91대시는 누락된 평점입니다. 파이프라인은 이를 다른 결측값과 마찬가지로 채웁니다.
직접 해보기
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
# 지난여름 캠프 참가자들: 올해 다시 왔나요 (1), 아니면 오지 않았나요 (0)?
campers = pd.read_csv("camp.csv")
X = campers.drop(columns="came_back")
y = campers["came_back"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0)
# 새 캠프 참가자에 대한 답변을 한 줄에 하나씩 입력합니다. 대시 (-)는 누락된 숫자를 뜻합니다
answers = []
for i in range(6):
answers.append(input())
numbers = []
for i in [0, 3, 4, 5]:
if answers[i] == "-":
numbers.append(np.nan)
else:
numbers.append(float(answers[i]))
camper = pd.DataFrame({"age": [numbers[0]], "cabin": [answers[1]], "activity": [answers[2]],
"nights": [numbers[1]], "rating": [numbers[2]], "friends_made": [numbers[3]]})
# 아래에 코드를 작성하세요
지도 학습의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러