import streamlit as st
import pandas as pd
import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score,mean_absolute_percentage_error
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
import torch
load_reg_data = False
load_class_data = False
def conformal_Predict(cal_err,alpha = 0.8):
assert alpha != None, " Provide a value of alpha "
idx = int(alpha*len(cal_err))
return cal_err[idx]
if __name__ == '__main__':
st.set_page_config(layout="wide")
if not(load_reg_data):
x_test = np.load("./Reg_Test_X.npy")
y_test = np.load("./Reg_Test_y.npy")
err_calib = np.load("./Reg_calib_err.npy")
y_pred = np.load("./Reg_y_pred.npy")
california_img=plt.imread("./california.png")
load_reg_data = True
if not(load_class_data):
img = np.load("./final_images.npy")
pred = np.load("./final_pred.npy")
cls_calib = np.load("./cerr.npy")
load_class_data = True
st.title("Conformal Prediction")
intro_tab , reg_tab , class_tab = st.tabs(["Introduction","Regression", "Classification"])
css = '''
'''
st.markdown(css, unsafe_allow_html=True)
with intro_tab:
f = open("Introduction.md",'r')
st.markdown(f.read())
with reg_tab:
with st.container():
left,right = st.columns([3,2])
with left:
st.write(" ")
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.")
st.write("---")
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")
budget = st.slider('Your Budget (in Millions)',min_value=0.3,max_value=5.0,value=2.0,step=0.05)
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")
alpha = st.slider(' Select a value of alpha',min_value=0.1,max_value=.99,value=0.5,step=0.05)
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")
with right:
sigma = conformal_Predict(err_calib,1-alpha)
in_range = (y_pred+sigma)sigma]
if len(out_labels)==0:
out_labels = ["None"]
out_labels = ",".join(out_labels)
st.write("Ouput Labels : "+out_labels)
st.write("True Label : Coat")
with col2:
fig1, ax1 = plt.figure(), plt.gca()
ax1.imshow(torch.tensor(img[1]).permute(1,2,0),cmap='gray')
ax1.spines['top'].set_visible(False)
ax1.spines['bottom'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['left'].set_visible(False)
ax1.set_xticks([])
ax1.set_yticks([])
st.pyplot(fig1)
fig1, ax1 = plt.figure(), plt.gca()
ax1.bar(range(10),pred[1])
ax1.axhline(y=sigma,linestyle='dashed',c='r')
ax1.set_xlabel("Classe Labels")
ax1.set_ylabel("SoftMax Probabilities")
ax1.set_title("Class Scores with Threshold")
ax1.set_xticks([i for i in range(10)])
st.pyplot(fig1)
out_labels = labels[pred[1]>sigma]
if len(out_labels)==0:
out_labels = ["None"]
out_labels = ",".join(out_labels)
st.write("Ouput Labels : "+out_labels)
st.write("True Label : Ankle Boot")
with col3:
fig1, ax1 = plt.figure(), plt.gca()
ax1.imshow(torch.tensor(img[2]).permute(1,2,0),cmap='gray')
ax1.spines['top'].set_visible(False)
ax1.spines['bottom'].set_visible(False)
ax1.spines['right'].set_visible(False)
ax1.spines['left'].set_visible(False)
ax1.set_xticks([])
ax1.set_yticks([])
st.pyplot(fig1)
fig1, ax1 = plt.figure(), plt.gca()
ax1.bar(range(10),pred[2])
ax1.axhline(y=sigma,linestyle='dashed',c='r')
ax1.set_xlabel("Classe Labels")
ax1.set_ylabel("SoftMax Probabilities")
ax1.set_title("Class Scores with Threshold")
ax1.set_xticks([i for i in range(10)])
st.pyplot(fig1)
out_labels = labels[pred[2]>sigma]
if len(out_labels)==0:
out_labels = ["None"]
out_labels = ",".join(out_labels)
st.write("Ouput Labels : "+out_labels)
st.write("True Label : Bag")