File size: 5,830 Bytes
08151c9 4e46dbd 08151c9 197bcc4 |
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
import streamlit as st
import pickle
import pandas as pd
import joblib
loaded_model = pickle.load(open('trained_model.sav', 'rb'))
preprocessor = joblib.load("preprocessor.pkl")
def obj_to_int(data):
# Mengganti nilai pada kolom planting_period
one_hot_enc = pd.get_dummies(data, columns=['planting_period', 'variety'])
return one_hot_enc
def transform_normalize(data, normalizer):
normalize_data = pd.DataFrame(normalizer.transform(data))
normalize_data.columns = data.columns
normalize_data.index = data.index
return normalize_data
def preprocess(data):
# 1. Encoding Categorical Value
X_encoded = obj_to_int(data)
required_features = ['planting_period_MT1',
'planting_period_MT2',
'planting_period_MT3',
'variety_Umum',
'variety_Hibrida'
]
existing_features = set(X_encoded.columns)
# Ambil perbedaan antara fitur yang dibutuhkan dan yang sudah ada
missing_features = set(required_features) - existing_features
list_missing_features = list(missing_features)
for feature in list_missing_features:
X_encoded[feature] = 0
# urutkan column sesuai data train
column_order = ['completion_rate',
'complete_time',
'profit','harvest',
'planting_period_MT1',
'planting_period_MT2',
'planting_period_MT3',
'variety_Hibrida',
'variety_Umum'
]
X_encoded = X_encoded[column_order]
# 2. Normalization
X_clean = transform_normalize(data = X_encoded,
normalizer = preprocessor['normalizer'])
return X_clean
def calculate_harvest_score(row):
variety = row['variety']
harvest = row['harvest']
variety_umum = ['Umum', 'Sintanur', 'Mentik']
variety_hibrida = ['Hibrida', 'Mapan']
if variety in variety_umum:
if harvest < 3000:
score_harvest = 46.79
elif 3000 <= harvest <= 5000:
score_harvest = 64.24
elif 5001 <= harvest <= 7000:
score_harvest = 93.57
else:
score_harvest = 102.83
elif variety in variety_hibrida:
if harvest < 5000:
score_harvest = 40.55
elif 5001 <= harvest <= 7000:
score_harvest = 54.02
elif 7001 <= harvest <= 9000:
score_harvest = 68.34
else:
score_harvest = 85.17
else:
score_harvest = 0
return score_harvest
def calculate_profit_score(row):
profit = row['profit']
if profit <= 0:
score_profit = 0
elif 1 <= profit <= 2000000:
score_profit = 27.84
else:
score_profit = 39.5
return score_profit
def calculate_complete_time_score(row):
complete_time = row['complete_time']
if complete_time <= 3:
score_complete_time = 10.21
return score_complete_time
elif 4 <= complete_time <= 7:
score_complete_time = 6.69
return score_complete_time
else:
score_complete_time = 1
return score_complete_time
def calculate_completion_rate_score(row):
completion_rate = row['completion_rate']
if completion_rate <= 60:
score_completion_rate = 17.31
return score_completion_rate
elif 61 <= completion_rate <= 70:
score_completion_rate = 21.04
return score_completion_rate
elif 71 <= completion_rate <= 80:
score_completion_rate = 25.21
return score_completion_rate
elif 81 <= completion_rate <= 90:
score_completion_rate = 30.1
return score_completion_rate
else:
score_completion_rate = 34.4
return score_completion_rate
def total_score(data):
harvest_score = data.apply(calculate_harvest_score, axis=1)
profit_score = data.apply(calculate_profit_score, axis=1)
completion_rate_score = data.apply(calculate_completion_rate_score, axis=1)
complete_time_score = data.apply(calculate_complete_time_score, axis=1)
score = harvest_score + profit_score + complete_time_score + completion_rate_score
return score
def map_class_to_label(class_score):
class_labels = {
0: "Sangat Baik",
1: "Baik",
2: "Cukup",
3: "Buruk",
4: "Sangat Buruk"
}
return class_labels.get(class_score, "Unknown")
def calculate_scoring(data):
score = total_score(data)
clean_data = preprocess(data)
hasil = loaded_model.predict(clean_data)
# Mapping nilai pada hasil ke label yang diinginkan
hasil_labels = [map_class_to_label(pred_class) for pred_class in hasil]
print('Hasil adalah', hasil_labels,
'\nTotal Score: ', score)
return hasil_labels, score
def main():
st.title("Project Scoring")
# Membuat tombol untuk upload file CSV
uploaded_file = st.file_uploader("Upload CSV file", type=["csv"])
if uploaded_file is not None:
# Membaca file CSV menjadi DataFrame
data = pd.read_csv(uploaded_file)
# Menampilkan preview dari data yang diupload
st.write("Preview Data:")
st.write(data.head())
# Membuat tombol "Calculate Scoring"
if st.button("Calculate Scoring"):
# Memanggil fungsi untuk menghitung scoring
hasil_scoring = calculate_scoring(data)
# Menampilkan hasil scoring
st.write("Hasil Scoring:")
hasil_df = pd.DataFrame({
'Kategori': hasil_scoring[0],
'Total Score': hasil_scoring[1]
})
st.table(hasil_df)
if __name__ == "__main__":
main()
|