Spaces:
Sleeping
Sleeping
Initial Commit : Regression Conformal
Browse files- Introduction.md +13 -0
- Reg_Test_X.npy +3 -0
- Reg_Test_y.npy +3 -0
- Reg_calib_err.npy +3 -0
- Reg_y_pred.npy +3 -0
- app.py +125 -0
- california.png +0 -0
Introduction.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
In Decision-Making, Machine Learning models need to not only make predictions but also quantify their predictions' uncertainty. A point prediction from the model might be dramatically different from the real value because of the high stochasticity of the real world. But, on the other hand, if the model could estimate the range which guarantees to cover the true value with high probability, model could compute the best and worst rewards and make more sensible decisions.
|
3 |
+
|
4 |
+
For example
|
5 |
+
* While buying a house, the predictions' upper bound can be useful for a buyer to be certain whether it will be able to buy a house or not.
|
6 |
+
* While Identifying an object, Applying threshold on softmax predictions can help us identify what that object could be.
|
7 |
+
|
8 |
+
Conformal prediction is a technique for quantifying such uncertainties for AI systems. In particular, given an input, conformal prediction estimates a prediction interval in regression problems and a set of classes in classification problems. Both the prediction interval and sets are guaranteed to cover the true value with high probability.
|
9 |
+
|
10 |
+
---
|
11 |
+
|
12 |
+
#### Theory
|
13 |
+
|
Reg_Test_X.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bd7195e256886dc393eacfc58e6ec6d1d13654d64474be213b7f276a1f028006
|
3 |
+
size 132224
|
Reg_Test_y.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:07cb30807ddf4a3eeb9afa3ccf7e9d04d979ae9aa50c90886384b38283b0fe7b
|
3 |
+
size 16640
|
Reg_calib_err.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:319fdf0049f99f432f170a2c2edefa21c2072394fe3e8de6562ced8e93ee6520
|
3 |
+
size 16480
|
Reg_y_pred.npy
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f1b50ba7749702e7738f9e0ec31c19d10e5d44ee5bc8d499d9d76b5abee64b4c
|
3 |
+
size 16640
|
app.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
from sklearn.datasets import fetch_california_housing
|
5 |
+
from sklearn.model_selection import train_test_split
|
6 |
+
from sklearn.preprocessing import StandardScaler
|
7 |
+
from sklearn.linear_model import LinearRegression
|
8 |
+
from sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score,mean_absolute_percentage_error
|
9 |
+
from matplotlib import pyplot as plt
|
10 |
+
import matplotlib.image as mpimg
|
11 |
+
import pickle
|
12 |
+
|
13 |
+
load_reg_data = False
|
14 |
+
load_class_data = False
|
15 |
+
california_img=plt.imread("./california.png")
|
16 |
+
|
17 |
+
|
18 |
+
# plt.rcParams['axes.spines.right'] = False
|
19 |
+
# plt.rcParams['axes.spines.top'] = False
|
20 |
+
# plt.rcParams['axes.spines.left'] = False
|
21 |
+
# plt.rcParams['axes.spines.bottom'] = False
|
22 |
+
|
23 |
+
|
24 |
+
|
25 |
+
def conformal_Predict(cal_err,alpha = 0.8):
|
26 |
+
|
27 |
+
assert alpha != None, " Provide a value of alpha "
|
28 |
+
# assert cal_err!= [] or None, "Provide the caliberation errors"
|
29 |
+
idx = int(alpha*len(cal_err))
|
30 |
+
return cal_err[idx]
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
if __name__ == '__main__':
|
35 |
+
|
36 |
+
st.set_page_config(layout="wide")
|
37 |
+
|
38 |
+
if not(load_reg_data):
|
39 |
+
x_test = np.load("./Reg_Test_X.npy")
|
40 |
+
y_test = np.load("./Reg_Test_y.npy")
|
41 |
+
err_calib = np.load("./Reg_calib_err.npy")
|
42 |
+
y_pred = np.load("./Reg_y_pred.npy")
|
43 |
+
load_reg_data = True
|
44 |
+
|
45 |
+
|
46 |
+
if not(load_class_data):
|
47 |
+
pass
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
st.title("Conformal Prediction")
|
52 |
+
intro_tab , reg_tab , class_tab = st.tabs(["Introduction","Regression", "Classification"])
|
53 |
+
css = '''
|
54 |
+
<style>
|
55 |
+
.stTabs [data-baseweb="tab-list"] button [data-testid="stMarkdownContainer"] p {
|
56 |
+
font-size:2rem;
|
57 |
+
}
|
58 |
+
</style>
|
59 |
+
'''
|
60 |
+
|
61 |
+
st.markdown(css, unsafe_allow_html=True)
|
62 |
+
|
63 |
+
with intro_tab:
|
64 |
+
|
65 |
+
f = open("Introduction.md",'r')
|
66 |
+
st.markdown(f.read())
|
67 |
+
|
68 |
+
|
69 |
+
with reg_tab:
|
70 |
+
with st.container():
|
71 |
+
|
72 |
+
left,right = st.columns([3,2])
|
73 |
+
|
74 |
+
with left:
|
75 |
+
|
76 |
+
st.write(" ")
|
77 |
+
|
78 |
+
st.markdown("For Regression, we are using California Housing Dataset. It serves as an excellent introduction to implementing machine learning algorithms because it has an easily understandable list of variables and sits at an optimal size between being too toyish and too cumbersome. The dataset pertains to the houses found in a given California district and some summary stats about them based on the 1990 census data.")
|
79 |
+
|
80 |
+
st.write("---")
|
81 |
+
|
82 |
+
st.markdown("Lets assume you are a buyer in California who is intrested in buying a house. You will most likely have a budget in mind. We have trained a Random forest regressor on the california dataset that predicts the price of a property. This model will help you determine where you will be able to buy a house in california given your budget estimates")
|
83 |
+
|
84 |
+
budget = st.slider('Your Budget (in Millions)',min_value=0.3,max_value=5.0,value=2.0,step=0.05)
|
85 |
+
|
86 |
+
st.markdown("Now, Please select how certain you want the model to be. More the value of alpha, more certain the model will be and hence more accurate will the reading be for the price estimate")
|
87 |
+
|
88 |
+
alpha = st.slider(' Select a value of alpha',min_value=0.1,max_value=.99,value=0.5,step=0.05)
|
89 |
+
|
90 |
+
st.markdown("The green points indicate that your budget it greater than the upper bound of model's prediction and hence these properties could be bought. Red points however, are the are the areas where you wont be able to buy a house")
|
91 |
+
|
92 |
+
with right:
|
93 |
+
|
94 |
+
sigma = conformal_Predict(err_calib,1-alpha)
|
95 |
+
in_range = (y_pred+sigma)<budget
|
96 |
+
|
97 |
+
fig1, ax1 = plt.figure(figsize=(10,7),dpi=150), plt.gca()
|
98 |
+
ax1.imshow(california_img, alpha=0.3,cmap=plt.get_cmap("jet"),extent=[-124.55, -113.80, 32.45, 42.05],zorder=1)
|
99 |
+
ax1.scatter(x_test[in_range,7],x_test[in_range,6],s=10,alpha=0.5,label='Can be Bought',c='C2',zorder=3)
|
100 |
+
ax1.scatter(x_test[~in_range,7],x_test[~in_range,6],s=10,alpha=0.5,label='Cannot Buy',c='r',zorder=3)
|
101 |
+
|
102 |
+
|
103 |
+
|
104 |
+
ax1.set_title("California Housing Locations (Test-set)")
|
105 |
+
ax1.set_xlabel("Latitude")
|
106 |
+
ax1.set_ylabel("Longitude")
|
107 |
+
ax1.spines['top'].set_visible(False)
|
108 |
+
ax1.spines['bottom'].set_visible(False)
|
109 |
+
ax1.spines['right'].set_visible(False)
|
110 |
+
ax1.spines['left'].set_visible(False)
|
111 |
+
ax1.set_xticks([])
|
112 |
+
ax1.set_yticks([])
|
113 |
+
ax1.legend()
|
114 |
+
ax1.patch.set_alpha(0.0)
|
115 |
+
st.pyplot(fig1)
|
116 |
+
|
117 |
+
|
118 |
+
|
119 |
+
with class_tab:
|
120 |
+
st.subheader("Classification")
|
121 |
+
st.write("This is the classification tab")
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
california.png
ADDED