|
| 1 | +# Dash Interest Rate Modeling (with Snowflake) |
| 2 | + |
| 3 | +This app shows how to query loan data from a Snowflake Data Warehouse, and train and analyze a Ridge regression model using Dash. |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +## Setting up Synapse |
| 8 | + |
| 9 | +This app uses an Azure Synapse Analytics database for querying the data used to train the model. |
| 10 | + |
| 11 | +### Datasets |
| 12 | + |
| 13 | +The data used was retrieved and modified from the [Lending Club Loan Data](https://www.kaggle.com/wendykan/lending-club-loan-data) from Kaggle. By using this dataset, you are agreeing to Kaggle's terms of use, as well as the original data license. You can download the modified files here: |
| 14 | +* `clean_loan.csv`: [Link](https://plotly-tutorials.s3-us-west-1.amazonaws.com/dash-sample-apps/snowflake-demos/clean_loan.csv) |
| 15 | +* `loan_desc.csv`: [Link](https://plotly-tutorials.s3-us-west-1.amazonaws.com/dash-sample-apps/snowflake-demos/loan_desc.csv) |
| 16 | + |
| 17 | +### Installion ODBC driver |
| 18 | + |
| 19 | +[This tutorial](https://docs.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15) shows how to install ODBC driver for SQL Server on Linux. You will need to install ODBC 17 to use this app - if you need another version, you will have to update the selected drivers by modifying the source code. |
| 20 | + |
| 21 | +### SQL Setup from CSV file |
| 22 | + |
| 23 | +Use the Azure Storage Explorer application to uploading `clean_loan.csv` to Azure Blob Storage. See [tutorial](https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-storage-explorer). |
| 24 | + |
| 25 | + |
| 26 | +The following steps require Synapse Studio. |
| 27 | + |
| 28 | +<!-- Connect to your Azure Blob Storage by selecting Data > "+" > Connect to external data > "Azure Blog Storage". |
| 29 | +
|
| 30 | + |
| 31 | + --> |
| 32 | + |
| 33 | + |
| 34 | +Create a database called loan: |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +Inside the SQL Editor, select "loan" for "Use database". |
| 39 | + |
| 40 | +Create a new data source linking to the Azure Blob Storage containing the CSV file you previously uploaded. Execute the following (change the location if needed): |
| 41 | +```sql |
| 42 | +USE [loan]; |
| 43 | +GO |
| 44 | + |
| 45 | +CREATE EXTERNAL DATA SOURCE sampleapps |
| 46 | +WITH ( LOCATION = 'https://sampleappsdata.blob.core.windows.net/'); |
| 47 | +``` |
| 48 | + |
| 49 | +Create a new format called "QuotedCsvWithHeaderFormat". Execute the following: |
| 50 | + |
| 51 | +```sql |
| 52 | +USE [loan]; |
| 53 | +GO |
| 54 | + |
| 55 | +CREATE EXTERNAL FILE FORMAT CsvWithHeaderFormat |
| 56 | +WITH ( |
| 57 | + FORMAT_TYPE = DELIMITEDTEXT, |
| 58 | + FORMAT_OPTIONS ( FIELD_TERMINATOR = ',', FIRST_ROW = 2 ) |
| 59 | +); |
| 60 | +GO |
| 61 | +``` |
| 62 | + |
| 63 | +Finally, you can create your table: |
| 64 | +```sql |
| 65 | +USE [loan]; |
| 66 | +GO |
| 67 | + |
| 68 | +CREATE EXTERNAL TABLE cleanLoan |
| 69 | +( |
| 70 | + loan_amnt INT, |
| 71 | + funded_amnt INT, |
| 72 | + revol_bal INT, |
| 73 | + policy_code INT, |
| 74 | + funded_amnt_inv FLOAT, |
| 75 | + int_rate FLOAT, |
| 76 | + installment FLOAT, |
| 77 | + annual_inc FLOAT, |
| 78 | + dti FLOAT, |
| 79 | + delinq_2yrs FLOAT, |
| 80 | + inq_last_6mths FLOAT, |
| 81 | + open_acc FLOAT, |
| 82 | + pub_rec FLOAT, |
| 83 | + revol_util FLOAT, |
| 84 | + total_acc FLOAT, |
| 85 | + out_prncp FLOAT, |
| 86 | + out_prncp_inv FLOAT, |
| 87 | + total_pymnt FLOAT, |
| 88 | + total_pymnt_inv FLOAT, |
| 89 | + term VARCHAR (100) COLLATE Latin1_General_BIN2, |
| 90 | + grade VARCHAR (100) COLLATE Latin1_General_BIN2, |
| 91 | + emp_length VARCHAR (100) COLLATE Latin1_General_BIN2, |
| 92 | + home_ownership VARCHAR (100) COLLATE Latin1_General_BIN2, |
| 93 | + verification_status VARCHAR (100) COLLATE Latin1_General_BIN2, |
| 94 | + loan_status VARCHAR (100) COLLATE Latin1_General_BIN2, |
| 95 | + pymnt_plan VARCHAR (100) COLLATE Latin1_General_BIN2, |
| 96 | + purpose VARCHAR (100) COLLATE Latin1_General_BIN2, |
| 97 | + initial_list_status VARCHAR (100) COLLATE Latin1_General_BIN2, |
| 98 | + application_type VARCHAR (100) COLLATE Latin1_General_BIN2 |
| 99 | +) |
| 100 | +WITH ( |
| 101 | + LOCATION = 'dash-sample-apps-data/clean_loan.csv', |
| 102 | + DATA_SOURCE = sampleapps, |
| 103 | + FILE_FORMAT = CsvWithHeaderFormat |
| 104 | +); |
| 105 | +``` |
| 106 | + |
| 107 | +The columns can be generated with the following code (run after you downloaded `clean_loan.csv`): |
| 108 | +```python |
| 109 | +import pandas as pd |
| 110 | +import utils |
| 111 | + |
| 112 | +df = pd.read_csv("clean_loan.csv", nrows=5) |
| 113 | +print(utils.get_column_strings(df)) |
| 114 | +``` |
| 115 | + |
| 116 | + |
| 117 | +### Using a different user |
| 118 | + |
| 119 | +If you do not want to use your admin account, you can create one for the `loan` database limited to `SELECT`ing: |
| 120 | +```sql |
| 121 | +CREATE LOGIN developers WITH PASSWORD = '<your-password>'; |
| 122 | + |
| 123 | +USE [loan] |
| 124 | +GO; |
| 125 | + |
| 126 | +CREATE USER developers FROM LOGIN developers; |
| 127 | +GRANT SELECT TO developers; |
| 128 | +``` |
| 129 | + |
| 130 | +Set the username to `developers` and the password to your new password. |
| 131 | + |
| 132 | +Read more about [GRANT](https://docs.microsoft.com/en-us/azure/synapse-analytics/sql/sql-authentication?tabs=serverless) and [SQL authentication](https://docs.microsoft.com/en-us/azure/synapse-analytics/sql/sql-authentication?tabs=serverless). |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +## Setting up the app |
| 137 | + |
| 138 | +First clone this repo: |
| 139 | +``` |
| 140 | +git clone https://github.com/plotly/dash-sample-apps.git |
| 141 | +cd dash-sample-apps/apps/dash-interest-rate |
| 142 | +``` |
| 143 | + |
| 144 | +Create a conda env (or venv) and install the requirements: |
| 145 | +``` |
| 146 | +conda create -n dash-interest-rate python=3.6.7 |
| 147 | +conda activate dash-interest-rate |
| 148 | +pip install -r requirements.txt |
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +## Start the app |
| 153 | + |
| 154 | +Start a redis server: |
| 155 | +``` |
| 156 | +redis-server --port 7777 |
| 157 | +``` |
| 158 | + |
| 159 | +In a separate terminal window, you can now run the app: |
| 160 | +``` |
| 161 | +python app.py |
| 162 | +``` |
| 163 | + |
| 164 | +and visit http://127.0.0.1:8050/. |
0 commit comments