오늘은 머신러닝 과제 제출일.
풀이 자체는 수요일에 끝냈지만
팀원들과의 코드리뷰가 있어서 제출은 오늘 했다.
베이직(1~3)과 챌린지(4~6번)으로 나눠서 TIL에 풀이를 리뷰해 보기로 한다.
#1. 데이터 불러오기
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
#2. train 데이터와 test 데이터 분리하기
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, shuffle=True, random_state=42, test_size = .3, stratify=y)
#shuffle = True : 데이터를 무작위로 섞어서 test와 train으로 나누겠다
#random_state = 42 : 난수 생성기의 시드 고정
#test_size = .3 : 테스트 데이터의 비율을 30%로 하겠다 (= 학습 데이터는 70%)
#stratify=y : train과 test로 분리될 때, 원 데이터 y의 분포를 그대로 따라가도록 분리 (이거 넣으면 .93 나오고 빼면 1 나옴)
#3. train 데이터와 test 데이터가 7:3 비율로 잘 쪼개졌는지 확인
#print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)
#4. Logistic Regression 모델 불러오기
from sklearn.linear_model import LogisticRegression
model_lor = LogisticRegression(max_iter=150)
#5. model_lor에 X_train과 y_train을 기준으로 학습시키기
model_lor.fit(X_train,y_train)
#6. model_lor은 학습이 끝났으니 X_test데이터를 기반으로 예측해 보기
y_pred_test = model_lor.predict(X_test)
#7. 모델 평가
from sklearn.metrics import accuracy_score, f1_score
print(accuracy_score(y_test,y_pred_test).round(3))
#2. train 데이터와 test 데이터 분리하기
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, shuffle=True, random_state=42, test_size = .3, stratify=y)
#shuffle = True : 데이터를 무작위로 섞어서 test와 train으로 나누겠다
#random_state = 42 : 난수 생성기의 시드 고정
#test_size = .3 : 테스트 데이터의 비율을 30%로 하겠다 (= 학습 데이터는 70%)
#stratify=y : train과 test로 분리될 때, 원 데이터 y의 분포를 그대로 따라가도록 분리 (이거 넣으면 .93 나오고 빼면 1 나옴)
Logistic Regression 모델 불러오기
from sklearn.linear_model import LogisticRegression
model_lor = LogisticRegression(max_iter=150)
이 문제에서는 예측하려는 y(붓꽃의 종류)가 범주형 데이터이므로 logistic regression을 쓴다.
어떤 모델을 쓸지는 추정하려는 종속변수가 뭐냐에 따라 결정하면 된다.
#5. model_lor에 X_train과 y_train을 기준으로 학습시키기
model_lor.fit(X_train,y_train)
#6. model_lor은 학습이 끝났으니 X_test데이터를 기반으로 예측해 보기
y_pred_test = model_lor.predict(X_test)
#7. 모델 평가
from sklearn.metrics import accuracy_score, f1_score
print(accuracy_score(y_test,y_pred_test).round(3))
#1. 데이터 불러오기
from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
X, y = housing['data'], housing['target']
#2. train 데이터와 test 데이터 분리하기
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, shuffle=True, random_state=42, test_size = .3)
#3. Linear Regression 모델 불러오기
from sklearn.linear_model import LinearRegression
model_lr = LinearRegression()
#4. model_lr에 X_train과 y_train을 기준으로 학습시키기
model_lr.fit(X_train,y_train)
#5. model_lr 학습이 끝났으니 X_test데이터를 기반으로 예측해 보기
y_pred_test = model_lr.predict(X_test)
#6. MSE 구해보기
from sklearn.metrics import mean_squared_error, r2_score
print(mean_squared_error(y_test,y_pred_test).round(3))
print(r2_score(y_test,y_pred_test).round(3))
#1. 데이터 불러오기
from sklearn.datasets import load_iris
iris = load_iris()
X, y = iris.data, iris.target
#2. 훈련 데이터 / 테스트 데이터 분리
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state = 42, stratify = y)
#3. 의사결정나무 불러오기 + 학습시키기
from sklearn.tree import DecisionTreeClassifier, plot_tree
model_dt = DecisionTreeClassifier(random_state = 42)
model_dt.fit(X_train,y_train)
#4. X를 기반으로 예측치 y_dt_pred 만들기
y_dt_pred = model_dt.predict(X_test)
#5. 정확도 평가
from sklearn.metrics import accuracy_score, f1_score
print(accuracy_score(y_test,y_dt_pred).round(3))
print(f1_score(y_test,y_dt_pred,average='weighted').round(3))
데이터를 불러오고 훈련 데이터와 테스트 데이터로 나누는 과정까지는 똑같다.
#3. 의사결정나무 불러오기 + 학습시키기
from sklearn.tree import DecisionTreeClassifier, plot_tree
model_dt = DecisionTreeClassifier(random_state = 42)
model_dt.fit(X_train,y_train)
#4. X를 기반으로 예측치 y_dt_pred 만들기
y_dt_pred = model_dt.predict(X_test)
#5. 정확도 평가
from sklearn.metrics import accuracy_score, f1_score
print(accuracy_score(y_test,y_dt_pred).round(3))
print(f1_score(y_test,y_dt_pred,average='weighted').round(3))