-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp1.py
More file actions
60 lines (45 loc) · 2.02 KB
/
app1.py
File metadata and controls
60 lines (45 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import streamlit as st
import pandas as pd
import numpy as np
import pickle
st.set_page_config(page_title="OMP Predictor", layout="wide")
st.title("🎯 OMP Feature Selection & Predictor")
st.write("Aplikasi ini menggunakan model Orthogonal Matching Pursuit (OMP) yang telah dilatih.")
# 1. Load Model
@st.cache_resource
def load_model():
with open("omp_model_pack1.pkl", "rb") as f:
return pickle.load(f)
try:
data_pack = load_model()
model = data_pack["model"]
scaler = data_pack["scaler"]
all_features = data_pack["feature_names"]
selected_idx = data_pack["selected_features_idx"]
selected_features = [all_features[i] for i in selected_idx]
st.success(f"Model dimuat! Model ini menggunakan {len(selected_features)} fitur terbaik.")
# 2. Input Form
st.sidebar.header("Input Nilai Fitur")
user_inputs = {}
# Hanya buat input untuk fitur yang dipilih oleh OMP
for feat in selected_features:
user_inputs[feat] = st.sidebar.number_input(f"Masukkan nilai untuk {feat}", value=0.0)
if st.button("Prediksi Sekarang"):
# Buat template data lengkap (150 fitur) diisi 0 dulu
input_full = np.zeros(len(all_features))
# Masukkan input user ke posisi index yang benar
for i, feat in enumerate(selected_features):
input_full[selected_idx[i]] = user_inputs[feat]
# Reshape untuk scaling
input_array = input_full.reshape(1, -1)
# SCALING (Wajib karena model dilatih dengan data terskala)
input_scaled = scaler.transform(input_array)
# PREDIKSI
prediction = model.predict(input_scaled)
# Tampilkan Hasil
st.metric(label="Hasil Prediksi Target", value=f"{prediction[0]:.4f}")
with st.expander("Lihat Detail Input"):
st.write("Fitur yang digunakan oleh model:")
st.json(user_inputs)
except FileNotFoundError:
st.error("File 'omp_model_pack.pkl' tidak ditemukan. Jalankan notebook dulu untuk export model.")