-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d8af27
commit 862ff49
Showing
7 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,277 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Functions to Create Prediction Score Sheet\n", | ||
"\n", | ||
"This jupyter notebook was created for the purpose of querying the REST API programmatically and obtain results of the predction since there were a lot of Quarter's to be analyzed." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import pandas as pd\n", | ||
"import numpy as np\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"import seaborn as sns\n", | ||
"import datetime" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 20, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"linearregoutfile=r\"C:\\Users\\Manasi\\Desktop\\linearregoutfile.csv\"\n", | ||
"neuralnetworksoutfile=r\"C:\\Users\\Manasi\\Desktop\\neuralnetwokoutfile.csv\"\n", | ||
"boosteddecisiontreeoutfiler=r\"C:\\Users\\Manasi\\Desktop\\boosteddecisiontree.csv\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 21, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"df=pd.read_csv(r\"C:\\Users\\Manasi\\Desktop\\predictionTestCases.csv\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 22, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/html": [ | ||
"<div>\n", | ||
"<table border=\"1\" class=\"dataframe\">\n", | ||
" <thead>\n", | ||
" <tr style=\"text-align: right;\">\n", | ||
" <th></th>\n", | ||
" <th>OG_QUARTER_YEAR</th>\n", | ||
" <th>CREDITSCORE</th>\n", | ||
" <th>FIRST_HOME_BUYER_FLAG</th>\n", | ||
" <th>OG_UPB</th>\n", | ||
" <th>OG_LOANTERM</th>\n", | ||
" <th>SELLER_NAME</th>\n", | ||
" <th>SERVICE_NAME</th>\n", | ||
" <th>ORIGINAL_INTERESTRATE</th>\n", | ||
" </tr>\n", | ||
" </thead>\n", | ||
" <tbody>\n", | ||
" <tr>\n", | ||
" <th>0</th>\n", | ||
" <td>Q1 1999</td>\n", | ||
" <td>615</td>\n", | ||
" <td>Y</td>\n", | ||
" <td>48000</td>\n", | ||
" <td>360</td>\n", | ||
" <td>Other sellers</td>\n", | ||
" <td>Other servicers</td>\n", | ||
" <td>5.375</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>1</th>\n", | ||
" <td>Q1 1999</td>\n", | ||
" <td>791</td>\n", | ||
" <td>N</td>\n", | ||
" <td>284000</td>\n", | ||
" <td>312</td>\n", | ||
" <td>Other sellers</td>\n", | ||
" <td>Other servicers</td>\n", | ||
" <td>5.750</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>2</th>\n", | ||
" <td>Q2 1999</td>\n", | ||
" <td>574</td>\n", | ||
" <td>N</td>\n", | ||
" <td>162000</td>\n", | ||
" <td>325</td>\n", | ||
" <td>Other sellers</td>\n", | ||
" <td>Other servicers</td>\n", | ||
" <td>6.875</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>3</th>\n", | ||
" <td>Q2 1999</td>\n", | ||
" <td>716</td>\n", | ||
" <td>Y</td>\n", | ||
" <td>70000</td>\n", | ||
" <td>360</td>\n", | ||
" <td>NORWEST MORTGAGE, IN</td>\n", | ||
" <td>WELLSFARGOHOMEMORTGA</td>\n", | ||
" <td>7.125</td>\n", | ||
" </tr>\n", | ||
" <tr>\n", | ||
" <th>4</th>\n", | ||
" <td>Q3 1999</td>\n", | ||
" <td>642</td>\n", | ||
" <td>N</td>\n", | ||
" <td>178000</td>\n", | ||
" <td>329</td>\n", | ||
" <td>Other sellers</td>\n", | ||
" <td>Other servicers</td>\n", | ||
" <td>7.125</td>\n", | ||
" </tr>\n", | ||
" </tbody>\n", | ||
"</table>\n", | ||
"</div>" | ||
], | ||
"text/plain": [ | ||
" OG_QUARTER_YEAR CREDITSCORE FIRST_HOME_BUYER_FLAG OG_UPB OG_LOANTERM \\\n", | ||
"0 Q1 1999 615 Y 48000 360 \n", | ||
"1 Q1 1999 791 N 284000 312 \n", | ||
"2 Q2 1999 574 N 162000 325 \n", | ||
"3 Q2 1999 716 Y 70000 360 \n", | ||
"4 Q3 1999 642 N 178000 329 \n", | ||
"\n", | ||
" SELLER_NAME SERVICE_NAME ORIGINAL_INTERESTRATE \n", | ||
"0 Other sellers Other servicers 5.375 \n", | ||
"1 Other sellers Other servicers 5.750 \n", | ||
"2 Other sellers Other servicers 6.875 \n", | ||
"3 NORWEST MORTGAGE, IN WELLSFARGOHOMEMORTGA 7.125 \n", | ||
"4 Other sellers Other servicers 7.125 " | ||
] | ||
}, | ||
"execution_count": 22, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"df.head()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"import json\n", | ||
"import requests\n", | ||
"from flask import Flask, render_template, request, jsonify" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 35, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"def get_prediction(og_quarter_year,credit_score,og_first_time_home_buyer,og_upb,og_loan_term,og_seller_name,og_servicer_name,algoType):\n", | ||
"\n", | ||
" #print(str(algoType)+\"\\t\"+str(credit_score)+\"\\t\"+str(og_first_time_home_buyer)+\"\\t\"+str(og_upb)+\"\\t\"+str(og_loan_term)+\"\\t\"+str(og_quarter_year)+\"\\t\"+str(og_seller_name)+\"\\t\"+str(og_servicer_name))\n", | ||
" #make ai call\n", | ||
" if algoType==\"pred_df\":\n", | ||
" url=\"https://ussouthcentral.services.azureml.net/workspaces/43247f6706b64e68a8b7d22ff16a2a4f/services/89403771d86f46e887c4d27b6eb80908/execute?api-version=2.0&details=true\"\n", | ||
" api_key='4/1AxmSvNJyjoFfc1SRtNHf8Znsr7/51JGIcH4Pxb7tI+1NEWrXlXzwUwy5/k5rBDz9kLENVEpt/iY2E/Tkt3g=='\n", | ||
" elif algoType==\"pred_nn\":\n", | ||
" url=\"https://ussouthcentral.services.azureml.net/workspaces/43247f6706b64e68a8b7d22ff16a2a4f/services/964b03c9c6fa480ca1156bd093eb9ff4/execute?api-version=2.0&details=true\"\n", | ||
" api_key=\"A2fylieTrkGctlekKOCkblCgcEZkxq06AHi8fuE69A9MFfVeKHfVj2uMftzuN+utNqV3Wfuyhj1HmM8F4Fj/8g==\"\n", | ||
" elif algoType==\"pred_lr\":\n", | ||
" url=\"https://ussouthcentral.services.azureml.net/workspaces/43247f6706b64e68a8b7d22ff16a2a4f/services/1f553c369b314b9dad2097ff5c012200/execute?api-version=2.0&details=true\"\n", | ||
" api_key=\"cz3t27xidE9ehvF8jtVhP0+s97DKnKqOLaodqaXC8I3Vmn844xIqjoAN8Q/enkiA1HXaOIvSLZQgi/SjaXfX2Q==\"\n", | ||
" \n", | ||
" \n", | ||
" data = {\n", | ||
" \n", | ||
" \"Inputs\": {\n", | ||
" \n", | ||
" \"input1\":\n", | ||
" {\n", | ||
" \"ColumnNames\": [\"CREDIT_SCORE\", \"FIRST_HOME_BUYER_FLAG\", \"OG_UPB\", \"OG_LOANTERM\", \"SELLER_NAME\", \"SERVICE_NAME\", \"OG_QUARTERYEAR\"],\n", | ||
" \"Values\": [ [credit_score,og_first_time_home_buyer,og_upb,og_loan_term,og_seller_name,og_servicer_name,og_quarter_year]]\n", | ||
" }, },\n", | ||
" \"GlobalParameters\": {\n", | ||
" }\n", | ||
" }\n", | ||
" \n", | ||
" body = str.encode(json.dumps(data))\n", | ||
" \n", | ||
" #url = 'https://ussouthcentral.services.azureml.net/workspaces/5de0e8bd28f74cf9a40babb3f1799a53/services/300d6267d2f843c9a5975621ff077a09/execute?api-version=2.0&details=true'\n", | ||
" #api_key = 'wQWgTpa3GyVACzg7Q6jVDdwt5JEDnfdvqqG21PKDr+UHmZWRQJh1XfrtLVON846vEDEXoDgnruZ1s9zd4Drzyw==' # Replace this with the API key for the web service\n", | ||
" headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}\n", | ||
" \n", | ||
" response = requests.post(url, data=body,headers=headers)\n", | ||
" #print(response.content)\n", | ||
" \n", | ||
" response_json=json.loads(response.content)\n", | ||
" predicted_interest_rate=response_json['Results']['output1']['value']['Values'][0][7]\n", | ||
" \n", | ||
" return_dict = {\"predicted_interest_rate\":predicted_interest_rate}\n", | ||
" return predicted_interest_rate\n", | ||
" " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 36, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"\n", | ||
"def writePredOutputToFile(outfile,algo_type):\n", | ||
" with open(outfile,\"w\") as fil:\n", | ||
" for index, row in df.iterrows():\n", | ||
" fil.write(get_prediction(row.OG_QUARTER_YEAR,row.CREDITSCORE,row.FIRST_HOME_BUYER_FLAG,row.OG_UPB,row.OG_LOANTERM,row.SELLER_NAME,row.SERVICE_NAME,algo_type))\n", | ||
" fil.write(\"\\n\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 38, | ||
"metadata": { | ||
"collapsed": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"writePredOutputToFile(linearregoutfile,'pred_lr')\n", | ||
"writePredOutputToFile(neuralnetworksoutfile,'pred_nn')\n", | ||
"writePredOutputToFile(boosteddecisiontreeoutfiler,'pred_df')" | ||
] | ||
} | ||
], | ||
"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.6.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.