{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "e27f22a3-f39e-4007-a048-56ccc9af915e", "metadata": {}, "outputs": [], "source": [ "import pickle" ] }, { "cell_type": "code", "execution_count": 2, "id": "bd40a9e0-faab-4999-9ad5-f74e7ae8b272", "metadata": {}, "outputs": [], "source": [ "with open('models/Credit_Reporting_model.pkl', 'rb') as f:\n", " trained_model_cr= pickle.load(f)\n", "\n", "with open('models/Credit_Prepaid_Card_model.pkl', 'rb') as f:\n", " trained_model_cp= pickle.load(f)\n", "\n", "with open('models/Checking_saving_model.pkl', 'rb') as f:\n", " trained_model_cs=pickle.load(f)\n", "\n", "with open('models/loan_model.pkl', 'rb') as f:\n", " trained_model_l= pickle.load(f)\n", "\n", "with open('models/Debt_model.pkl', 'rb') as f:\n", " trained_model_d= pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 3, "id": "d1ad5fb9-36bf-4637-a137-17fca19224f6", "metadata": {}, "outputs": [], "source": [ "with open('models/Product_model.pkl', 'rb') as f:\n", " product_model= pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 9, "id": "b946427b-b259-4eb2-a40b-ed7b7e476354", "metadata": {}, "outputs": [], "source": [ "from sklearn.pipeline import Pipeline\n", "\n", "# Define the pipeline steps\n", "trained_product_model=product_model\n", "\n", "\n", "# Define a function to select the appropriate subproduct prediction model based on the predicted product\n", "def select_subproduct_model(predicted_product):\n", " if predicted_product == 'Credit Reporting' :\n", " return trained_model_cr\n", " elif predicted_product == 'Credit/Prepaid Card':\n", " return trained_model_cp\n", " elif predicted_product == 'Checking or savings account':\n", " return trained_model_cs\n", " elif predicted_product == 'Loans / Mortgage':\n", " return trained_model_l\n", " elif predicted_product == 'Debt collection':\n", " return trained_model_d\n", " else:\n", " raise ValueError(\"Invalid predicted product category\")\n", "\n", "def custom_predict(narrative):\n", " # Predict product category\n", " predicted_product = product_model.predict([narrative])[0]\n", " \n", " # Load the appropriate subproduct prediction model\n", " subproduct_model = select_subproduct_model(predicted_product)\n", " \n", " # Predict subproduct category using the selected model\n", " predicted_subproduct = subproduct_model.predict([narrative])\n", " return predicted_product, predicted_subproduct" ] }, { "cell_type": "code", "execution_count": 11, "id": "982521ea-364e-4521-889e-fe586c186701", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Predicted product: Credit/Prepaid Card\n", "Predicted subproduct: ['Checking account']\n" ] } ], "source": [ "narrative = \"I have a problem with my credit card bill.\"\n", "#narrative = \"it is absurd that i have consistently made timely payments for this account and have never been overdue. i kindly request that you promptly update my account to reflect this accurately.\"\n", "predicted_product, predicted_subproduct = custom_predict(narrative)\n", "print(\"Predicted product:\", predicted_product)\n", "print(\"Predicted subproduct:\", predicted_subproduct)" ] }, { "cell_type": "code", "execution_count": 7, "id": "88529ef1-6ed2-41b9-a266-e550a50b831f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Product Prediction Accuracy: 0.9110859728506787\n", "Product Prediction Precision: 0.6634108079865927\n", "Subproduct Prediction Accuracy: 0.8377989657401422\n", "Subproduct Prediction Precision: 0.5058767033148038\n" ] } ], "source": [ "from sklearn.metrics import accuracy_score, precision_score\n", "import pandas as pd\n", "\n", "# Load the test dataset\n", "test_data = pd.read_csv('../data_splits/test-data-split.csv') \n", "\n", "# Initialize lists to store predicted and actual labels\n", "predicted_products = []\n", "predicted_subproducts = []\n", "actual_products = test_data['Product']\n", "actual_subproducts = test_data['Sub-product']\n", "\n", "# Iterate over each complaint narrative in the test set\n", "for narrative in test_data['Consumer complaint narrative']:\n", " # Predict product and subproduct using the custom_predict function\n", " predicted_product, predicted_subproduct = custom_predict(narrative)\n", " \n", " # Append predicted labels to lists\n", " predicted_products.append(predicted_product)\n", " predicted_subproducts.append(predicted_subproduct)\n", "\n", "# Calculate accuracy and precision\n", "accuracy_product = accuracy_score(actual_products, predicted_products)\n", "precision_product = precision_score(actual_products, predicted_products, average='macro',zero_division=1)\n", "accuracy_subproduct = accuracy_score(actual_subproducts, predicted_subproducts)\n", "precision_subproduct = precision_score(actual_subproducts, predicted_subproducts, average='macro',zero_division=1)\n", "\n", "# Print the results\n", "print(\"Product Prediction Accuracy:\", accuracy_product)\n", "print(\"Product Prediction Precision:\", precision_product)\n", "print(\"Subproduct Prediction Accuracy:\", accuracy_subproduct)\n", "print(\"Subproduct Prediction Precision:\", precision_subproduct)\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ce982e0a", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.19" } }, "nbformat": 4, "nbformat_minor": 5 }