This project aims to train an Artificial Intelligence (AI) model to predict the credit score of new customers based on historical data. The model used is a Random Forest classifier.
- Objective
- Technologies Used
- Project Structure
- Data Preprocessing
- Model Training
- Credit Score Prediction
- How to Run
- Expected Results
- Conclusion
The objective of this project is to:
- Train an AI model capable of predicting the credit score of new customers based on historical data.
- Predict the credit score for new customers using the trained model.
This project uses classification techniques to predict whether a customer will have a "Good", "Standard", or "Poor" credit score based on their profile.
- Python: Programming language used to develop the code.
- Pandas: For data manipulation and analysis.
- Scikit-learn: For implementing the AI model and data preprocessing.
- LabelEncoder: To transform categorical variables into numerical variables for model analysis.
- RandomForestClassifier: A decision tree-based algorithm used to predict the credit score.
The project is divided into the following steps:
- Data Import: Loading and displaying customer data.
- Data Preprocessing: Transforming categorical variables into numerical variables using
LabelEncoder. - Model Training: Splitting the data into training and testing sets and training the model to predict credit scores.
- Credit Score Prediction: Using the trained model to predict the credit score of new customers.
Before training the model, the data needs to be preprocessed to ensure all features are numeric, making them suitable for analysis by the model.
- Data Import: The customer data is loaded from a CSV file.
tabela = pd.read_csv("clientes.csv")
display(tabela)- Categorical Variables Transformation: We used
LabelEncoderto transform variables such as "profession", "credit mix", and "payment behavior" into numerical values.
from sklearn.preprocessing import LabelEncoder
coder_profession = LabelEncoder()
tabela["profissao"] = coder_profession.fit_transform(tabela["profissao"])
coder_mix_credits = LabelEncoder()
tabela["mix_credito"] = coder_mix_credits.fit_transform(tabela["mix_credito"])
coder_payment = LabelEncoder()
tabela["comportamento_pagamento"] = coder_payment.fit_transform(tabela["comportamento_pagamento"])After preprocessing, the next step is to train the AI model. We use the Random Forest algorithm, which is a supervised learning algorithm based on multiple decision trees.
- We define the target variable (
score_credito) as the dependent variable (y), and the remaining columns as independent variables (X). - We split the data into training and testing sets.
from sklearn.model_selection import train_test_split
y = tabela["score_credito"]
X = tabela.drop(columns=["score_credito", "id_cliente"])
x_train, x_test, y_train, y_test = train_test_split(X, y)- We train the model using the training data.
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model.fit(x_train, y_train)- We evaluate the model's performance using the test data.
from sklearn.metrics import accuracy_score
predictions = model.predict(x_test)
accuracy = accuracy_score(y_test, predictions)
print(f"Model Accuracy: {accuracy}")Once the model is trained, we can use it to predict the credit score for new customers.
- Load the new customers' data.
- Perform the same transformation on categorical variables as was done for the training data.
- Use the trained model to predict the credit score.
# Load new customer data
tabela_new_client = pd.read_csv("novos_clientes.csv")
# Transform categorical variables
tabela_new_client["profissao"] = coder_profession.transform(tabela_new_client["profissao"])
tabela_new_client["mix_credito"] = coder_mix_credits.transform(tabela_new_client["mix_credito"])
tabela_new_client["comportamento_pagamento"] = coder_payment.transform(tabela_new_client["comportamento_pagamento"])
# Predict credit score for new customers
new_predict = model.predict(tabela_new_client)
display(new_predict)-
Clone this repository to your local machine:
git clone https://github.com/seu-repositorio.git
-
Install the necessary dependencies:
pip install -r requirements.txt
-
Run the script to train the model and predict the credit score for new customers:
python predict_credit_score.py
The model should be able to predict the credit score for new customers accurately, with an expected accuracy of around 82%, based on the historical data provided. The prediction will be made on a scale of 3 classes: "Good", "Standard", and "Poor".
This project demonstrates how machine learning algorithms can be used to predict a customer's credit score. Further improvements could be made by tuning the model or incorporating additional features. The next step would be to apply cross-validation techniques to improve the model's robustness.