2024 하계 모각코

모각코 4회차 - 7/24일, 14시~17시

nanocat 2024. 7. 24. 14:03

목표

1. 유방암 데이터셋으로 데이터 분석. 머신러닝 모델 학습

2. 학습한 모델 저장 후 웹과 연결

 

결과

1. 유방암 데이터셋으로 데이터 분석. 머신러닝 모델 학습

import xgboost
from xgboost import XGBClassifier
import lightgbm as lgb

dtrain = xgboost.DMatrix(data=X_train, label=y_train)
dtest = xgboost.DMatrix(data=X_test)

#model = XGBClassifier(eta=0.2, max_depth=4, gamma=1, random_state=42)
#model = lgb.LGBMClassifier(num_leaves=30, objective='binary', random_state=42)

params = {
         'n_estimators' : 200,
         'max_depth' : 3,
         'gamma' : 4.376,
         'eta' : 0.2, 
         'subsample' : 0.9818,
         'objective' : 'binary:logistic',
         'eval_metric' : 'logloss'}

model = xgboost.train(params = params, dtrain = dtrain, num_boost_round = 400, 
                        early_stopping_rounds = 100, evals = [(dtrain, 'train')])

#model.fit(X_train, y_train) # 모델 학습

predict = model.predict(dtest) # 예측

y_pred = (predict >= 0.5).astype(int)

# 평가
print('Accuracy:', accuracy_score(y_test, y_pred))
print('Confusion Matrix:\n', confusion_matrix(y_test, y_pred))
print('F1 Score:', f1_score(y_test, y_pred))

 

[399] train-logloss:0.38401
Accuracy: 0.7597402597402597
Confusion Matrix:
[[81 18]
[19 36]]
F1 Score: 0.6605504587155963

 

2. 학습한 모델 저장 후 웹과 연결

# /app.py

from flask import Flask, request, render_template
import xgboost as xgb
import numpy as np

app = Flask(__name__)

# 모델 로드
model = xgb.XGBClassifier()
model.load_model("diabetes_model.json")

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
    # HTML 폼에서 데이터 추출
    features = [float(x) for x in request.form.values()]
    input_data = np.array(features).reshape(1, -1)

    # 예측 수행
    prediction = model.predict(input_data)
    
    # 결과 반환
    result = '당뇨병 있을 가능성 높음' if prediction[0] == 1 else '당뇨병 있을 가능성 낮음'
    return render_template('index.html', prediction_text=f'예측 결과: {result}')

if __name__ == '__main__':
    app.run(debug=True)

 

<!-- /templates/index.html -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>이진 분류 예측</title>
</head>
<body>
    <h1>이진 분류 예측</h1>
    <form action="/predict" method="post">
        <label for="Pregnancies">Pregnancies:</label>
        <input type="text" id="Pregnancies" name="Pregnancies"><br><br>
        <label for="Glucose">Glucose:</label>
        <input type="text" id="Glucose" name="Glucose"><br><br>
        <label for="BloodPressure">BloodPressure:</label>
        <input type="text" id="BloodPressure" name="BloodPressure"><br><br>
        <label for="SkinThickness">SkinThickness:</label>
        <input type="text" id="SkinThickness" name="SkinThickness"><br><br>
        <label for="Insulin">Insulin:</label>
        <input type="text" id="Insulin" name="Insulin"><br><br>
        <label for="BMI">BMI:</label>
        <input type="text" id="BMI" name="BMI"><br><br>
        <label for="DiabetesPedigreeFunction">DiabetesPedigreeFunction:</label>
        <input type="text" id="DiabetesPedigreeFunction" name="DiabetesPedigreeFunction"><br><br>
        <label for="Age">Age:</label>
        <input type="text" id="Age" name="Age"><br><br>
        <input type="submit" value="예측">
    </form>
    <h2>{{ prediction_text }}</h2>
</body>
</html>