Spaces:
Runtime error
Runtime error
Isabel Gwara commited on
Commit ·
5c2fff1
1
Parent(s): 1b929ef
Update app.py
Browse files69 unmodified lines70717273747576777879808182837 unmodified lines9192939495969798991004 unmodified lines10510610710810911011111211311411511611711811912012112212312412512612712811 unmodified lines14014114214314414514614714822 unmodified lines17117217317417517617769 unmodified linescat_value_dicts[colname] = new_dictdata[colname] = transformed_col_vals### -------------------------------- ###### model training ###### -------------------------------- ###def train_model():# select features and predicton; automatically selects last column as predictioncols = len(data.columns)num_features = cols - 1x = data.iloc[: , :num_features]7 unmodified linesmodel.fit(x_train, y_train.values.ravel())y_pred = model.predict(x_test)# save the model to filewith open('model.pkl', 'wb') as f:pkl.dump(model, f)with open('acc.txt', 'w+') as f:acc = metrics.accuracy_score(y_test, y_pred)f.write(str(round(acc * 100, 1)) + '%')4 unmodified lines### rerun logic ###### -------------------------------- ###try:with open('model.pkl', 'rb') as f:model = pkl.load(f)except FileNotFoundError as e:model = train_model()with open('acc.txt', 'r') as f:acc = f.read()### ------------------------------- ###### interface creation ###### ------------------------------- #### predictor for generic number of featuresdef general_predictor(input_list):features = []11 unmodified linesdef get_feat():feats = [abs(x) for x in model.coef_[0]]# max_val = max(feats)# idx = feats.index(max_val)return str(feats) # data.columns[idx]form = st.form('ml-inputs')22 unmodified linescol1, col2 = st.columns(2)col1.metric("Number of Different Possible Results", len(cat_value_dicts[final_colname]))col2.metric("Model Accuracy", acc)# st.metric("Most Important Question", get_feat())with open('info.md') as f:69 unmodified lines70717273747576777879808182837 unmodified lines9192939495969798991001014 unmodified lines10610710810911011111211311411511611711811912012112212312412512612712812913013113213311 unmodified lines14514614714814915015115215322 unmodified lines17617717817918018118269 unmodified linescat_value_dicts[colname] = new_dictdata[colname] = transformed_col_vals### -------------------------------- ###### model training ###### -------------------------------- ###def train_model():# select features and prediction; automatically selects last column as predictioncols = len(data.columns)num_features = cols - 1x = data.iloc[: , :num_features]7 unmodified linesmodel.fit(x_train, y_train.values.ravel())y_pred = model.predict(x_test)# save the model to file using the pickle packagewith open('model.pkl', 'wb') as f:pkl.dump(model, f)# save model accuracy to file using the pickle packagewith open('acc.txt', 'w+') as f:acc = metrics.accuracy_score(y_test, y_pred)f.write(str(round(acc * 100, 1)) + '%')4 unmodified lines### rerun logic ###### -------------------------------- #### check to see if this is the first time running the script,# if the model has already been trained and saved, load ittry:with open('model.pkl', 'rb') as f:model = pkl.load(f)# if this is the first time running the script, train the model# and save it to the file model.pklexcept FileNotFoundError as e:model = train_model()# read the model accuracy from filewith open('acc.txt', 'r') as f:acc = f.read()### ------------------------------- ###### interface creation ###### ------------------------------- #### uses the logistic regression to predict for a generic number# of featuresdef general_predictor(input_list):features = []11 unmodified linesdef get_feat():feats = [abs(x) for x in model.coef_[0]]max_val = max(feats)idx = feats.index(max_val)return data.columns[idx]form = st.form('ml-inputs')22 unmodified linescol1, col2 = st.columns(2)col1.metric("Number of Different Possible Results", len(cat_value_dicts[final_colname]))col2.metric("Model Accuracy", acc)st.metric("Most Important Question", get_feat())with open('info.md') as f:
app.py+21-16