siddop commited on
Commit
a1cdb63
1 Parent(s): 4c5e687

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ %%writefile app.py
2
+ import joblib
3
+ import pandas as pd
4
+ import numpy as np
5
+ import streamlit as st
6
+ from sklearn.preprocessing import LabelEncoder
7
+ from lime.lime_tabular import LimeTabularExplainer
8
+
9
+ # -------------------------------------------------------------------------------------
10
+ df = pd.read_csv(r"C:\Users\bhati\Documents\MachineLearning\FreelanceProject\StudentPerformance\combined.csv", index_col=0)
11
+ df2 = df.copy()
12
+ obj_columns = df.select_dtypes(include=['object']).columns
13
+ num_columns = df.select_dtypes(include='number').columns
14
+ le_dict = {}
15
+ classes_dict = {}
16
+ for col in obj_columns:
17
+ le = LabelEncoder()
18
+ df2[col] = le.fit_transform(df[col])
19
+ le_dict[col] = le
20
+ classes_dict[col] = le.classes_
21
+ df2['G1'] = df2.pop('G1')
22
+ df2['G2'] = df2.pop('G2')
23
+ df2['G3'] = df2.pop('G3')
24
+ X = df2.iloc[:,:-1]
25
+ y = df2.iloc[:,-1]
26
+ allCol = X.columns
27
+
28
+ # -------------------------------------------------------------------------------------
29
+ # Load the model from the file
30
+ joblib_file = "xgb_model.joblib"
31
+ loaded_model = joblib.load(joblib_file)
32
+
33
+ # -------------------------------------------------------------------------------------
34
+ variableExpl = []
35
+ with open(r'C:\Users\bhati\Documents\MachineLearning\FreelanceProject\StudentPerformance\student.txt', 'r', encoding='utf-8') as file:
36
+ for line in file:
37
+ cleaned_line = line.strip()
38
+ # Append each cleaned line as a row to the list
39
+ variableExpl.append(cleaned_line)
40
+
41
+ variableExpl.pop(0)
42
+ for i in range(5):
43
+ variableExpl.pop(-1)
44
+ for i in range(2):
45
+ variableExpl.pop(-3)
46
+
47
+ variableExplDict = {}
48
+ for i in variableExpl:
49
+ variableExplDict[i.split()[1]] = i
50
+
51
+ # -------------------------------------------------------------------------------------
52
+ def predict_score(inputs):
53
+ if any(value == '' for value in inputs):
54
+ return "Please enter all the inputs."
55
+
56
+ #-------------------------------------------------------------------------------------------
57
+ # Create a dictionary for each input
58
+ input_df = pd.DataFrame(np.array(inputs).reshape(1, -1), columns=allCol)
59
+
60
+
61
+ #-------------------------------------------------------------------------------------------
62
+ # label encode each input
63
+ for col in obj_columns:
64
+ if col in input_df.columns:
65
+ input_df[col] = le_dict[col].transform(input_df[col])
66
+
67
+ #-------------------------------------------------------------------------------------------
68
+ # Make predictions
69
+ pred = loaded_model.predict(input_df)
70
+
71
+ # Ensure all columns are numeric
72
+ input_df = input_df.astype(float)
73
+ #-------------------------------------------------------------------------------------------
74
+ # Create a LIME explainer
75
+ explainer = LimeTabularExplainer(training_data=X.values, mode="regression", feature_names=allCol, verbose=True)
76
+
77
+ exp = explainer.explain_instance(data_row=input_df.iloc[0].to_numpy(), predict_fn=loaded_model.predict, num_features=33)
78
+
79
+ impacts = {}
80
+ for item in exp.as_list():
81
+ impacts[item[0]] = item[1]
82
+
83
+ explTable = pd.DataFrame(np.array(list(impacts.values())).reshape(1,-1), columns=impacts.keys()).T
84
+ explTable = explTable.rename(columns={0: 'ImpactOnPrediction'})
85
+ explTable['Positive/Negative'] = explTable['ImpactOnPrediction'].apply(lambda x: 'Negative' if x < 0 else 'Positive')
86
+
87
+ return pred, explTable
88
+
89
+ #-------------------------------------------------------------------------------------------
90
+ # Streamlit app
91
+ st.title("Student's Final Grade Prediction")
92
+
93
+ # Input
94
+ inputs = []
95
+ for variable in variableExplDict:
96
+ st.write(variableExplDict[variable])
97
+ if variable in obj_columns:
98
+ value = st.selectbox(variable, classes_dict[variable], key=variable) # Create a dropdown menu
99
+ else:
100
+ value = st.text_input(variable, key=variable)
101
+ inputs.append(value)
102
+
103
+ # Predict button
104
+ if st.button("Predict"):
105
+ score, explantn = predict_score(inputs)
106
+ st.write("Prediction: ", score)
107
+ st.write("Impact on prediction:", explantn)
108
+
109
+ # Clear button functionality
110
+ if st.button("Clear"):
111
+ st.experimental_rerun()
112
+
113
+ !streamlit run app.py