Skip to content

Instantly share code, notes, and snippets.

@firmai
Created November 23, 2021 16:11
Show Gist options
  • Save firmai/b1eaa531d07a22429e729fe82e4e1e60 to your computer and use it in GitHub Desktop.
Save firmai/b1eaa531d07a22429e729fe82e4e1e60 to your computer and use it in GitHub Desktop.
Interest Rate Setting.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.5"
},
"colab": {
"name": "Interest Rate Setting.ipynb",
"provenance": [],
"include_colab_link": true
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/firmai/b1eaa531d07a22429e729fe82e4e1e60/interest-rate-setting.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OdF3tLb2f_h2"
},
"source": [
"import pandas as pd\n",
"import numpy as np #Importing the required libraries fr the project\n",
"import matplotlib.pyplot as plt\n",
"import joblib\n",
"df=pd.read_csv('https://raw.githubusercontent.com/firmai/random-assets-two/master/mlfin/Credit%20Data.csv') #Importing th dataset from local drive \n",
"df['Saving accounts'].fillna(df['Saving accounts'].mode().iloc[0],inplace=True) #Taking care of categorical null values in Saving accounts \n",
"df['Checking account'].fillna(df['Checking account'].mode().iloc[0],inplace=True)#Taking care of categorical null values in Checking account\n"
],
"execution_count": 2,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "h-TWXu1Zf_h9",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "611507cf-bb82-4f82-e503-59816821a932"
},
"source": [
"from sklearn.compose import ColumnTransformer\n",
"from sklearn.preprocessing import OneHotEncoder\n",
"from sklearn.preprocessing import LabelEncoder\n",
"lb=LabelEncoder()\n",
"ct=ColumnTransformer([(\"on\",OneHotEncoder(),[1,3,4,5,8])],remainder='passthrough')\n",
"df=ct.fit_transform(df)\n",
"df\n",
"joblib.dump(ct,'onehot.save') # Saving the column transformation"
],
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['onehot.save']"
]
},
"metadata": {},
"execution_count": 6
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "rXfSVffrf_h_"
},
"source": [
"from scipy import stats\n",
"z=np.abs(stats.zscore(df))\n",
"threshold=3\n",
"df_no_outliers=df[(z<=threshold).all(axis=1)]\n",
"x=df[:,:-1]\n",
"y=df[:,-1]"
],
"execution_count": 7,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "N27qyUiLf_iB",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "f59b59b3-d51b-40ad-be9f-2b9840108559"
},
"source": [
"from sklearn.preprocessing import StandardScaler\n",
"sc=StandardScaler()\n",
"x=sc.fit_transform(x)\n",
"joblib.dump(sc,'Scalar.save') #Saving Scalar transformation"
],
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"['Scalar.save']"
]
},
"metadata": {},
"execution_count": 8
}
]
},
{
"cell_type": "raw",
"metadata": {
"id": "mi05mT9Ff_iB"
},
"source": [
"Splitting into test and train datasets"
]
},
{
"cell_type": "code",
"metadata": {
"id": "Ok76bXekf_iB"
},
"source": [
"from sklearn.model_selection import train_test_split\n",
"x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.33,random_state=45)"
],
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "IMtGPfNCf_iD"
},
"source": [
"from sklearn.model_selection import GridSearchCV\n",
"from sklearn.neighbors import KNeighborsRegressor"
],
"execution_count": 10,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "QasC_cHjf_iE"
},
"source": [
"knn_grid=GridSearchCV(estimator=KNeighborsRegressor(),param_grid={'n_neighbors':np.arange(1,20)},cv=5)"
],
"execution_count": 11,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "HGX80lFSf_iE",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "249d56b4-41fb-4f42-aa63-4824a932a836"
},
"source": [
"knn_grid.fit(x_train,y_train)"
],
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"GridSearchCV(cv=5, estimator=KNeighborsRegressor(),\n",
" param_grid={'n_neighbors': array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,\n",
" 18, 19])})"
]
},
"metadata": {},
"execution_count": 12
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "qIwOaMXUf_iE",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "e4219b90-c664-4e2e-a440-0d3f66e14912"
},
"source": [
"knn_grid.best_params_[\"n_neighbors\"]"
],
"execution_count": 14,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"10"
]
},
"metadata": {},
"execution_count": 14
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "LQ9BLwZyf_iF",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "5c99499e-d292-4cbf-c20b-a94d1f621c52"
},
"source": [
"knn=KNeighborsRegressor(n_neighbors=knn_grid.best_params_[\"n_neighbors\"])\n",
"knn.fit(x_train,y_train)"
],
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"KNeighborsRegressor(n_neighbors=10)"
]
},
"metadata": {},
"execution_count": 15
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "kSmj3lxwf_iF",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "c4fd89a5-283b-4aba-bb9b-b337a133555d"
},
"source": [
"y_pred=knn.predict(x_test)\n",
"y_pred"
],
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([0.08513972, 0.26742264, 0.17359173, 0.23738981, 0.40577597,\n",
" 0.42799787, 0.30332392, 0.41560373, 0.24831413, 0.39457872,\n",
" 0.27044913, 0.31939348, 0.16411026, 0.22322586, 0.41969191,\n",
" 0.1592932 , 0.18931551, 0.30942813, 0.36404963, 0.46734986,\n",
" 0.26068379, 0.4077047 , 0.19726592, 0.25209923, 0.3783297 ,\n",
" 0.32341196, 0.22128024, 0.28230724, 0.40018512, 0.37757221,\n",
" 0.35505169, 0.38189696, 0.31653039, 0.29291725, 0.21881856,\n",
" 0.25882594, 0.13229492, 0.23721519, 0.17476178, 0.44070788,\n",
" 0.26751124, 0.23586186, 0.40292945, 0.42061071, 0.23462114,\n",
" 0.42698973, 0.32644082, 0.35763603, 0.23883988, 0.33487805,\n",
" 0.13625966, 0.35271792, 0.41891049, 0.45384875, 0.3566709 ,\n",
" 0.33767763, 0.46493834, 0.27920022, 0.19676686, 0.3058886 ,\n",
" 0.12094813, 0.20909474, 0.37062912, 0.20420895, 0.14800512,\n",
" 0.32744583, 0.26925395, 0.28438308, 0.14932195, 0.26238076,\n",
" 0.30754778, 0.28119087, 0.28175347, 0.29798882, 0.28676527,\n",
" 0.2394352 , 0.13171688, 0.26021795, 0.47227544, 0.13055382,\n",
" 0.21732478, 0.1888944 , 0.11074841, 0.20996175, 0.28440472,\n",
" 0.26909631, 0.25104007, 0.28837999, 0.34618683, 0.23470731,\n",
" 0.23875179, 0.19346281, 0.30091894, 0.39747719, 0.50469314,\n",
" 0.15827739, 0.22489704, 0.3861736 , 0.28902946, 0.32455572,\n",
" 0.3861736 , 0.46508148, 0.27831573, 0.28324463, 0.07784112,\n",
" 0.24895125, 0.18124899, 0.37153619, 0.37163036, 0.44190859,\n",
" 0.12216033, 0.23392806, 0.27917148, 0.18137399, 0.27530406,\n",
" 0.22857762, 0.3189215 , 0.48392287, 0.18957067, 0.47227544,\n",
" 0.24272182, 0.29670616, 0.22703591, 0.1837002 , 0.23038028,\n",
" 0.3287914 , 0.37384715, 0.34538488, 0.42703411, 0.3311826 ,\n",
" 0.47227544, 0.23447528, 0.45820657, 0.37529553, 0.11255458,\n",
" 0.23883988, 0.39455283, 0.41777572, 0.23694717, 0.1837002 ,\n",
" 0.45404566, 0.33076259, 0.27479915, 0.47069873, 0.34525179,\n",
" 0.25233792, 0.28453381, 0.18322529, 0.20534957, 0.43029855,\n",
" 0.23682737, 0.43747372, 0.16411026, 0.35580866, 0.2361107 ,\n",
" 0.12433424, 0.40456104, 0.40898128, 0.2587815 , 0.22857762,\n",
" 0.14432778, 0.35785557, 0.10014628, 0.39102879, 0.21734986,\n",
" 0.22781352, 0.1804474 , 0.26894254, 0.37464786, 0.31601031,\n",
" 0.1940596 , 0.22583347, 0.18283707, 0.27552191, 0.24681791,\n",
" 0.20894925, 0.28356052, 0.3755526 , 0.24346295, 0.37600633,\n",
" 0.09074003, 0.47227544, 0.3405849 , 0.09481593, 0.10511075,\n",
" 0.14898315, 0.24264418, 0.18158626, 0.36348727, 0.34858611,\n",
" 0.27044913, 0.33614164, 0.24106084, 0.38852936, 0.2535036 ,\n",
" 0.30273271, 0.33305279, 0.21792068, 0.48323197, 0.30700865,\n",
" 0.25687618, 0.28780128, 0.27378854, 0.2851307 , 0.31349386,\n",
" 0.2918358 , 0.26021795, 0.23653587, 0.15424716, 0.2122888 ,\n",
" 0.2295308 , 0.15053538, 0.31458546, 0.35824661, 0.22255727,\n",
" 0.30541253, 0.34569675, 0.18373269, 0.33076259, 0.25668453,\n",
" 0.31709657, 0.56775492, 0.43422872, 0.15959946, 0.26334402,\n",
" 0.33614164, 0.40659217, 0.15328546, 0.36360154, 0.43180395,\n",
" 0.36669268, 0.20708347, 0.25063256, 0.21802728, 0.44060694,\n",
" 0.3861736 , 0.25394755, 0.20747798, 0.40928056, 0.21182584,\n",
" 0.42387654, 0.31335459, 0.32832707, 0.45570528, 0.08859136,\n",
" 0.28581899, 0.23188143, 0.24790985, 0.5189891 , 0.07975462,\n",
" 0.34756293, 0.44736404, 0.47612591, 0.12051325, 0.13964039,\n",
" 0.28813933, 0.29480992, 0.31872631, 0.19278699, 0.28717761,\n",
" 0.32003558, 0.20044426, 0.33038262, 0.23817281, 0.22857762,\n",
" 0.23415143, 0.20413921, 0.20747798, 0.17085211, 0.31209573,\n",
" 0.47005977, 0.30118782, 0.21132674, 0.43745082, 0.22857762,\n",
" 0.19859822, 0.4194761 , 0.38570997, 0.25200123, 0.22157052,\n",
" 0.31291552, 0.50738655, 0.36312485, 0.39802638, 0.1368117 ,\n",
" 0.3146686 , 0.11195093, 0.31025295, 0.52566783, 0.17483662,\n",
" 0.09396211, 0.13229492, 0.43237279, 0.18158626, 0.38576399,\n",
" 0.46739523, 0.48115713, 0.22482146, 0.27898573, 0.24511447,\n",
" 0.32115779, 0.20510764, 0.45715345, 0.29993439, 0.35876095,\n",
" 0.23619179, 0.28259913, 0.1592932 , 0.37897247, 0.34173011,\n",
" 0.44322653, 0.17782107, 0.30799379, 0.3861736 , 0.13025713,\n",
" 0.39386032, 0.19303535, 0.31332996, 0.33305279, 0.5551261 ,\n",
" 0.24329056, 0.25087352, 0.24024346, 0.1567539 , 0.2743237 ,\n",
" 0.29220932, 0.22070701, 0.33690258, 0.25989799, 0.22009245])"
]
},
"metadata": {},
"execution_count": 16
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "ZwzlQkptf_iL"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment