Download Credit_Score_Classifier.py from roseyshi/creditscore: direct link, hf CLI and curl.
- Browser
- Download file 4.3 kB
-
https://huggingface.co/roseyshi/creditscore/resolve/main/Credit_Score_Classifier.py
- Command line
-
hf download hf://roseyshi/creditscore/Credit_Score_Classifier.py
-
curl -L -o Credit_Score_Classifier.py https://huggingface.co/roseyshi/creditscore/resolve/main/Credit_Score_Classifier.py
4.3 kB
| """i | |
| mport streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.model_selection import train_test_split | |
| from joblib import dump, load | |
| # Load and process data | |
| data = pd.read_csv("C:/Users/lenovo/Downloads/Create_score_model/clean_train.csv") | |
| X = data.drop('Credit_Score', axis=1) | |
| y = data['Credit_Score'] | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| X_train.head() | |
| # Train the model | |
| rf = RandomForestClassifier(random_state=42) | |
| rf.fit(X_train, y_train) | |
| # Save the model | |
| #dump(rf, 'credit_classifier.joblib', compress=('gzip', 9)) | |
| # Streamlit App | |
| # %%writefile scoring_app.py | |
| loaded_model = load(rf) | |
| # Streamlit interface | |
| st.title("Credit Score Prediction App") | |
| # Input fields | |
| feature1 = st.number_input("Outstanding Debt") | |
| feature2 = st.number_input("Credit Mix") | |
| feature3 = st.number_input("Credit History Age (in months)") | |
| feature4 = st.number_input("Monthly Balance") | |
| feature5 = st.number_input("Payment Behaviour") | |
| feature6 = st.number_input("Annual Income") | |
| feature7 = st.number_input("Number of Delayed Payments") | |
| # Define target names | |
| target_names = {0: "Good", 1: "Poor", 2: "Standard"} | |
| if st.button("Predict"): | |
| prediction = loaded_model.predict([[feature1, feature2, feature3, feature4, feature5, feature6, feature7]]) | |
| st.write(f"Predicted Class: {target_names[prediction[0]]}") | |
| """ | |
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.ensemble import RandomForestClassifier | |
| from sklearn.model_selection import train_test_split | |
| from joblib import dump, load | |
| def load_and_train_model(): | |
| try: | |
| # Load and process data | |
| data = pd.read_csv("clean_train.csv") | |
| except FileNotFoundError: | |
| st.error("Error: The file 'clean_train.csv' was not found. Please upload the file.") | |
| st.stop() | |
| # Splitting data into features and target | |
| X = data.drop('Credit_Score', axis=1) | |
| y = data['Credit_Score'] | |
| # Train-test split | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Train the Random Forest model | |
| rf = RandomForestClassifier(random_state=42) | |
| rf.fit(X_train, y_train) | |
| # Save the trained model to a file | |
| dump(rf, 'credit_classifier.joblib') | |
| return rf | |
| def load_model(): | |
| import os | |
| if os.path.exists('credit_classifier.joblib'): | |
| try: | |
| return load('credit_classifier.joblib') | |
| except Exception as e: | |
| st.warning(f"Error loading model: {e}. Retraining the model...") | |
| return load_and_train_model() | |
| else: | |
| st.warning("Model file not found. Training a new model...") | |
| return load_and_train_model() | |
| # Load the trained model | |
| loaded_model = load_model() | |
| # Streamlit App | |
| st.title("Credit Score Prediction App") | |
| st.markdown("Predict the credit score category based on user inputs.") | |
| # Input fields | |
| st.sidebar.header("Enter Feature Values:") | |
| feature1 = st.sidebar.number_input("Outstanding Debt", min_value=0.0, step=100.0) | |
| feature2 = st.sidebar.selectbox("Credit Mix (0=Bad, 1=Good, 2=Excellent)", [0, 1, 2]) | |
| feature3 = st.sidebar.number_input("Credit History Age (in months)", min_value=0.0, step=1.0) | |
| feature4 = st.sidebar.number_input("Monthly Balance", min_value=0.0, step=100.0) | |
| feature5 = st.sidebar.number_input("Payment Behaviour", min_value=0.0, step=1.0) | |
| feature6 = st.sidebar.number_input("Annual Income", min_value=0.0, step=1000.0) | |
| feature7 = st.sidebar.number_input("Number of Delayed Payments", min_value=0, step=1) | |
| # Define target names | |
| target_names = {0: "Good", 1: "Poor", 2: "Standard"} | |
| # Predict Button | |
| if st.button("Predict"): | |
| try: | |
| # Prepare the input as a 2D array | |
| inputs = np.array([[feature1, feature2, feature3, feature4, feature5, feature6, feature7]]) | |
| # Make prediction | |
| prediction = loaded_model.predict(inputs) | |
| # Display result | |
| st.success(f"Predicted Credit Score: {target_names[prediction[0]]}") | |
| except Exception as e: | |
| st.error(f"An error occurred during prediction: {e}") | |