Lab04 / app.py
Zackary Hopkins
Fixed requirements and app paths
c9caa5d
import pandas as pd
import gradio as gr
#Get data
housing = pd.read_csv("housing.csv")
#Clean the data and sperate it into lables and features
data_set_clean = housing.dropna(subset=["total_bedrooms"])
data_labels = data_set_clean["median_house_value"].copy()
data_features = data_set_clean.drop("median_house_value", axis=1)
#Create scaller
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(data_features)
#scale data
data_features_normalized = scaler.transform(data_features)
#Create decisiontree model
from sklearn.tree import DecisionTreeRegressor
tree_reg = DecisionTreeRegressor()
tree_reg.fit(data_features_normalized, data_labels)
#Task 1 Create inputs for gr
input_module_list = []
for col_name in data_features:
input_module_list.append(gr.inputs.Slider(minimum= data_features[col_name].min(),
maximum= data_features[col_name].max(),
label = col_name))
#Task 1.1 outputs
output_module1 = gr.outputs.Textbox(label = "Prediction")
output_module2 = gr.outputs.Image(label = "Output Image")
import matplotlib.pyplot as plt
import matplotlib.image as mping
#Task 2 the function
def run_model(input1, intput2, input3, input4, input5, input6, input7, input8):
#Takes the data point and makes the prediction
data_point = [[input1, intput2, input3, input4, input5, input6, input7, input8]]
scaled_data_point = scaler.transform(data_point)
output1 = tree_reg.predict(scaled_data_point)[0]
housing.plot(kind='scatter', x='longitude', y='latitude', alpha=.4,
s=housing['population']/100, label='population', figsize=(10,7),
c='median_house_value', cmap=plt.get_cmap('jet'), colorbar=True,
sharex=False)
plt.legend()
plt.scatter(x=input1, y=intput2, label="Your Point", c=output1, cmap=plt.get_cmap('jet'))
plt.annotate('Your Point', xy=(input1, intput2), xytext=(-116,40), arrowprops={'width':5,
'headwidth':10,
'headlength':10,
'shrink':0})
plt.savefig('newprediction.png')
output2 = mping.imread(f'newprediction.png')
return output1, output2
gr.Interface(fn=run_model,
inputs=input_module_list,
outputs=[output_module1,output_module2],
).launch()