{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "de47e40f", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "from sklearn.model_selection import train_test_split\n" ] }, { "cell_type": "code", "execution_count": 37, "id": "3a7108a5", "metadata": {}, "outputs": [], "source": [ "def load_data(filepath, data_name = None):\n", " if data_name=='german':\n", " data = pd.read_csv(filepath)\n", " gender_dict = {\n", " \"'male single'\": \"male\",\n", " \"'female div/dep/mar'\": \"female\",\n", " \"'male div/sep'\": \"male\",\n", " \"'male mar/wid'\": \"male\"}\n", "\n", " data['gender'] = data['personal_status'].map(gender_dict)\n", " del data[\"personal_status\"]\n", " \n", " S= data['gender']\n", " X = data[['checking_status', 'duration', 'credit_history', 'purpose',\n", " 'credit_amount', 'savings_status', 'employment',\n", " 'installment_commitment', 'other_parties',\n", " 'residence_since', 'property_magnitude', 'age', 'other_payment_plans',\n", " 'housing', 'existing_credits', 'job', 'num_dependents', 'own_telephone',\n", " 'foreign_worker']]\n", " y = data['class']\n", " \n", " \n", " return S, X, y\n", " \n", " elif data_name =='loan_predictions':\n", " data = pd.read_csv(filepath)\n", " S= data['Gender']\n", " X = data[['Loan_ID', 'Married', 'Dependents', 'Education',\n", " 'Self_Employed', 'ApplicantIncome', 'CoapplicantIncome', 'LoanAmount',\n", " 'Loan_Amount_Term', 'Credit_History', 'Property_Area']]\n", " y = data[ 'Loan_Status']\n", " \n", " return S, X, y\n", " \n", " else:\n", " data = pd.read_excel(file_path)\n", " return data\n", "\n", " \n", "\n", " \n", " " ] }, { "cell_type": "code", "execution_count": 45, "id": "10500e08", "metadata": {}, "outputs": [], "source": [ "# def load_dataset(file_path):\n", "# \"\"\"Load the dataset from an Excel file.\"\"\"\n", "# return pd.read_excel(file_path)\n", "\n", "# # Provide the path to your dataset\n", "# file_path = 'data/ghana_loan.xls'\n", "# df = load_dataset(file_path)\n" ] }, { "cell_type": "code", "execution_count": 46, "id": "14526f43", "metadata": {}, "outputs": [], "source": [ "# df" ] }, { "cell_type": "code", "execution_count": 41, "id": "68d4a3d6", "metadata": {}, "outputs": [ { "ename": "ValueError", "evalue": "too many values to unpack (expected 3)", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)", "Input \u001b[0;32mIn [41]\u001b[0m, in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m filepath \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdata/ghana_loan.xls\u001b[39m\u001b[38;5;124m'\u001b[39m\n\u001b[0;32m----> 2\u001b[0m S, X, y \u001b[38;5;241m=\u001b[39m load_data(filepath, data_name \u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m)\n", "\u001b[0;31mValueError\u001b[0m: too many values to unpack (expected 3)" ] } ], "source": [ "filepath = 'data/ghana_loan.xls'\n", "S, X, y = load_data(filepath, data_name =None)" ] }, { "cell_type": "code", "execution_count": 36, "id": "97314cec", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Loan_IDGenderMarriedDependentsEducationSelf_EmployedApplicantIncomeCoapplicantIncomeLoanAmountLoan_Amount_TermCredit_HistoryProperty_Area
0LP001002MaleNo0GraduateNo58490.0NaN360.01.0Urban
1LP001003MaleYes1GraduateNo45831508.0128.0360.01.0Rural
2LP001005MaleYes0GraduateYes30000.066.0360.01.0Urban
3LP001006MaleYes0Not GraduateNo25832358.0120.0360.01.0Urban
4LP001008MaleNo0GraduateNo60000.0141.0360.01.0Urban
.......................................
609LP002978FemaleNo0GraduateNo29000.071.0360.01.0Rural
610LP002979MaleYes3+GraduateNo41060.040.0180.01.0Rural
611LP002983MaleYes1GraduateNo8072240.0253.0360.01.0Urban
612LP002984MaleYes2GraduateNo75830.0187.0360.01.0Urban
613LP002990FemaleNo0GraduateYes45830.0133.0360.00.0Semiurban
\n", "

614 rows × 12 columns

\n", "
" ], "text/plain": [ " Loan_ID Gender Married Dependents Education Self_Employed \\\n", "0 LP001002 Male No 0 Graduate No \n", "1 LP001003 Male Yes 1 Graduate No \n", "2 LP001005 Male Yes 0 Graduate Yes \n", "3 LP001006 Male Yes 0 Not Graduate No \n", "4 LP001008 Male No 0 Graduate No \n", ".. ... ... ... ... ... ... \n", "609 LP002978 Female No 0 Graduate No \n", "610 LP002979 Male Yes 3+ Graduate No \n", "611 LP002983 Male Yes 1 Graduate No \n", "612 LP002984 Male Yes 2 Graduate No \n", "613 LP002990 Female No 0 Graduate Yes \n", "\n", " ApplicantIncome CoapplicantIncome LoanAmount Loan_Amount_Term \\\n", "0 5849 0.0 NaN 360.0 \n", "1 4583 1508.0 128.0 360.0 \n", "2 3000 0.0 66.0 360.0 \n", "3 2583 2358.0 120.0 360.0 \n", "4 6000 0.0 141.0 360.0 \n", ".. ... ... ... ... \n", "609 2900 0.0 71.0 360.0 \n", "610 4106 0.0 40.0 180.0 \n", "611 8072 240.0 253.0 360.0 \n", "612 7583 0.0 187.0 360.0 \n", "613 4583 0.0 133.0 360.0 \n", "\n", " Credit_History Property_Area \n", "0 1.0 Urban \n", "1 1.0 Rural \n", "2 1.0 Urban \n", "3 1.0 Urban \n", "4 1.0 Urban \n", ".. ... ... \n", "609 1.0 Rural \n", "610 1.0 Rural \n", "611 1.0 Urban \n", "612 1.0 Urban \n", "613 0.0 Semiurban \n", "\n", "[614 rows x 12 columns]" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [] }, { "cell_type": "code", "execution_count": 21, "id": "422d9c7c", "metadata": {}, "outputs": [], "source": [ "# S = df['Gender']\n", "# X = df[['Loan_ID', 'Gender', 'Married', 'Dependents', 'Education',\n", "# 'Self_Employed', 'ApplicantIncome', 'CoapplicantIncome', 'LoanAmount',\n", "# 'Loan_Amount_Term', 'Credit_History', 'Property_Area']]\n", "# y = df[ 'Loan_Status']" ] }, { "cell_type": "code", "execution_count": 22, "id": "4bb62629", "metadata": {}, "outputs": [], "source": [ "S_train, S_test, X_train, X_test, y_train, y_test = train_test_split(S, X, y, test_size=0.2, random_state=42)" ] }, { "cell_type": "code", "execution_count": 23, "id": "0fab58f1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Datasets saved to train.csv and test.csv\n" ] } ], "source": [ "def save_to_csv(S_train, S_test, X_train, X_test, y_train, y_test, train_file_name, test_file_name):\n", " \"\"\"Save the train and test sets to CSV files.\"\"\"\n", " train = pd.concat([S_train, X_train, y_train], axis=1)\n", " test = pd.concat([S_test, X_test, y_test], axis=1)\n", " \n", " train.to_csv(f'train/{train_file_name}', index=False)\n", " test.to_csv(f'test/{test_file_name}', index=False)\n", "\n", "# Specify the file names\n", "train_file_name = 'loanpred_train.csv'\n", "test_file_name = 'loanpred_test.csv'\n", "# Save the datasets to CSV files\n", "save_to_csv(S_train, S_test, X_train, X_test, y_train, y_test, train_file_name, test_file_name)\n", "\n", "print(\"Datasets saved to train.csv and test.csv\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "adccf7b2", "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.12" } }, "nbformat": 4, "nbformat_minor": 5 }