File size: 3,396 Bytes
0855a17 |
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 |
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import json
#Load All Files
with open('list_num_cols.txt', 'r') as file_1:
list_num_col = json.load(file_1)
with open('list_cat_cols.txt', 'r') as file_2:
list_cat_col = json.load(file_2)
with open('model_scaler.pkl', 'rb') as file_3:
model_scaler = pickle.load(file_3)
with open('model_encoder.pkl', 'rb') as file_4:
model_encoder = pickle.load(file_4)
with open('model_lin_reg.pkl', 'rb') as file_5:
model_lin_reg = pickle.load(file_5)
def run():
with st.form('form_fifa_2022'):
#Field Nama
name = st.text_input('Name', value = '')
#Field Umur
age = st.number_input('Age', min_value = 16, max_value = 60, value = 25, step = 1, help = 'Usia Pemain')
#Field Tinggi badan
height = st.slider('Height', 100, 250, 170)
#Field Weight
weight = st.number_input('weight', 50, 150, 70)
#field price
price = st.number_input('Price', value = 0)
st.markdown('----')
#Field Attacking Work Rate
attacking_work_rate = st.selectbox('Attacking Work Rate', ('Low', 'Medium', 'High'), index = 1)
#Field Defensive Work Rate
devensive_work_rate = st.selectbox('Devensive Work Rate', ('Low', 'Medium', 'High'), index = 1)
#Field Pace Total
pace_total = st.number_input('Pace', min_value = 0, max_value=100, value = 50)
#Field Shooting Total
shooting_total = st.number_input('Shooting', min_value = 0, max_value=100, value = 50)
#Field Passing Total
passing_total = st.number_input('Passing', min_value = 0, max_value=100, value = 50)
#Field Dribbling Total
dribbling_total = st.number_input('Dribbling', min_value = 0, max_value=100, value = 50)
#Field Defending Total
defending_total = st.number_input('Defending', min_value = 0, max_value=100, value = 50)
#Field Physicality Total
physicality = st.number_input('Physicality', min_value = 0, max_value=100, value = 50)
#bikin submit button
submitted = st.form_submit_button('Predict')
#Inference
data_inf = {
'Name' : name,
'Age' : age,
'Height' : height,
'Weight' : weight,
'Price' : price,
'AttackingWorkRate' : attacking_work_rate,
'DefensiveWorkRate' : devensive_work_rate,
'PaceTotal' :pace_total,
'ShootingTotal': shooting_total,
'PassingTotal' : passing_total,
'DribblingTotal' :dribbling_total,
'DefendingTotal' :defending_total,
'PhysicalityTotal':physicality,
}
data_inf = pd.DataFrame([data_inf])
st.dataframe(data_inf)
#Logic ketika predic button ditekan
if submitted:
#split between numerical and categorical columns
data_inf_num = data_inf[list_num_col]
data_inf_cat = data_inf[list_cat_col]
#Scaling & Encoding
data_inf_num_scaled = model_scaler.transform(data_inf_num)
data_inf_cat_encoded = model_encoder.transform(data_inf_cat)
data_inf_final = np.concatenate([data_inf_num_scaled, data_inf_cat_encoded], axis = 1)
#predict using linear reg model
y_pred_inf = model_lin_reg.predict(data_inf_final)
st.write('## Rating : ', str(int(y_pred_inf)))
if __name__ == '__main__':
run() |