Nam model

This commit is contained in:
qudwns245
2025-04-30 15:28:50 +09:00
parent 8de5238395
commit 477fc5e159
4 changed files with 88 additions and 690 deletions

45
Nam/Final_code 1.py Normal file
View File

@@ -0,0 +1,45 @@
import pandas as pd
import pickle
from tensorflow.keras.models import load_model
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
from url_preprocessing import preprocess_url_dataframe # 너가 만든 전처리 모듈
# 1. 원본 데이터 불러오기
df = pd.read_csv("train.csv") # 또는 적절한 파일명으로 수정
print("원본 데이터 불러옴")
# 2. 전처리 적용
df_processed = preprocess_url_dataframe(df)
print(" 전처리 완료")
# 3. 피처/레이블 분리
X = df_processed.drop(columns=['label', 'URL', 'URL_clean'], errors='ignore') # 'label' 없으면 자동 무시
y = df_processed['label'] if 'label' in df_processed.columns else None
# 4. 스케일러 불러오기
with open("scaler.pkl", "rb") as f:
scaler = pickle.load(f)
X_scaled = scaler.transform(X)
print(" 스케일링 완료")
# 5. 모델 불러오기
model = load_model("best_model.h5")
print(" 모델 불러오기 완료")
# 6. 예측
y_pred_proba = model.predict(X_scaled).ravel()
best_threshold = 0.34 # 여기에 저장된 값이 있다면 pickle로 불러올 수 있음
y_pred = (y_pred_proba > best_threshold).astype(int)
# 7. 결과 출력
if y is not None:
print("예측 결과 (테스트셋 평가)")
print("Accuracy:", accuracy_score(y, y_pred))
print("F1 Score:", f1_score(y, y_pred))
print("Precision:", precision_score(y, y_pred))
print("Recall:", recall_score(y, y_pred))
else:
print("예측 완료! 라벨이 없어 평가 생략")
print("예측 결과 샘플:", y_pred[:10])