|
import os |
|
import numpy as np |
|
import matplotlib.pyplot as plt |
|
import gradio as gr |
|
import pandas as pd |
|
import tarfile |
|
import urllib.request |
|
|
|
|
|
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/master/" |
|
HOUSING_PATH = os.path.join("datasets", "housing") |
|
HOUSING_URL = DOWNLOAD_ROOT + "datasets/housing/housing.tgz" |
|
|
|
def fetch_housing_data(housing_url=HOUSING_URL, housing_path=HOUSING_PATH): |
|
if not os.path.isdir(housing_path): |
|
os.makedirs(housing_path) |
|
tgz_path = os.path.join(housing_path, "housing.tgz") |
|
urllib.request.urlretrieve(housing_url, tgz_path) |
|
housing_tgz = tarfile.open(tgz_path) |
|
housing_tgz.extractall(path=housing_path) |
|
housing_tgz.close() |
|
|
|
|
|
|
|
def load_housing_data(housing_path=HOUSING_PATH): |
|
csv_path = os.path.join(housing_path, "housing.csv") |
|
return pd.read_csv(csv_path) |
|
|
|
|
|
|
|
|
|
fetch_housing_data() |
|
|
|
housing_pd = load_housing_data() |
|
housing_pd.head() |
|
|
|
|
|
housing = housing_pd.drop('ocean_proximity', axis=1) |
|
housing |
|
|
|
|
|
|
|
|
|
|
|
from sklearn.model_selection import train_test_split |
|
train_set, test_set = train_test_split(housing, test_size=0.2, random_state=10) |
|
|
|
|
|
train_set_clean = train_set.dropna(subset=["total_bedrooms"]) |
|
train_set_clean |
|
|
|
|
|
train_labels = train_set_clean["median_house_value"].copy() |
|
train_features = train_set_clean.drop("median_house_value", axis=1) |
|
|
|
|
|
|
|
from sklearn.preprocessing import MinMaxScaler |
|
scaler = MinMaxScaler() |
|
scaler.fit(train_features) |
|
|
|
train_features_normalized = scaler.transform(train_features) |
|
train_features_normalized |
|
|
|
|
|
|
|
from sklearn.linear_model import LinearRegression |
|
lin_reg = LinearRegression() |
|
lin_reg.fit(train_features_normalized, train_labels) |
|
|
|
|
|
|
|
|
|
def save_fig(fig_id, tight_layout=True, fig_extension="png", resolution=300): |
|
path = os.path.join(IMAGES_PATH, fig_id + "." + fig_extension) |
|
print("Saving figure", fig_id, ' to ',path) |
|
if tight_layout: |
|
plt.tight_layout() |
|
plt.savefig(path, format=fig_extension, dpi=resolution) |
|
|
|
PROJECT_ROOT_DIR='./' |
|
IMAGES_PATH = os.path.join(PROJECT_ROOT_DIR, "images") |
|
os.makedirs(IMAGES_PATH, exist_ok=True) |
|
|
|
images_path = os.path.join(PROJECT_ROOT_DIR, "images", "end_to_end_project") |
|
os.makedirs(images_path, exist_ok=True) |
|
DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml2/master/" |
|
filename = "california.png" |
|
print("Downloading", filename) |
|
url = DOWNLOAD_ROOT + "images/end_to_end_project/" + filename |
|
urllib.request.urlretrieve(url, os.path.join(images_path, filename)) |
|
|
|
|
|
|
|
def draw_map_customize(longitude,latitude, fig_id='test',fig_extension='png' ): |
|
import matplotlib.image as mpimg |
|
california_img=mpimg.imread(os.path.join(images_path, filename)) |
|
ax = housing.plot(kind="scatter", x="longitude", y="latitude", figsize=(10,7), |
|
s=housing['population']/100, label="Population", |
|
c="median_house_value", cmap="jet", |
|
colorbar=False, alpha=0.4) |
|
plt.imshow(california_img, extent=[-124.55, -113.80, 32.45, 42.05], alpha=0.5, |
|
cmap=plt.get_cmap("jet")) |
|
plt.ylabel("Latitude", fontsize=18) |
|
plt.xlabel("Longitude", fontsize=18) |
|
|
|
plt.xticks(fontsize=18, rotation=0) |
|
plt.yticks(fontsize=18, rotation=0) |
|
|
|
|
|
plt.plot(longitude,latitude, "ro", alpha=0.7, marker=r'$\clubsuit$', markersize=30) |
|
|
|
plt.annotate("Your location is here", xy=(longitude,latitude), xytext=(longitude+1,latitude+1), fontsize=20, |
|
arrowprops=dict(arrowstyle="->")) |
|
|
|
|
|
prices = housing["median_house_value"] |
|
tick_values = np.linspace(prices.min(), prices.max(), 11) |
|
cbar = plt.colorbar(ticks=tick_values/prices.max()) |
|
cbar.ax.set_yticklabels(["$%dk"%(round(v/1000)) for v in tick_values], fontsize=14) |
|
cbar.set_label('Median House Value', fontsize=16) |
|
|
|
plt.legend(fontsize=16) |
|
save_fig(fig_id) |
|
|
|
|
|
path = os.path.join(IMAGES_PATH, fig_id + "." + fig_extension) |
|
return path |
|
|
|
|
|
|
|
def get_sample_data(num_data): |
|
sample_data = [] |
|
for i in range(num_data): |
|
samp = housing.sample(1) |
|
longitude = float(samp['longitude'].values[0]) |
|
latitude = float(samp['latitude'].values[0]) |
|
housing_median_age = float(samp['housing_median_age'].values[0]) |
|
total_rooms = float(samp['total_rooms'].values[0]) |
|
total_bedrooms = float(samp['total_bedrooms'].values[0]) |
|
population = float(samp['population'].values[0]) |
|
households = float(samp['households'].values[0]) |
|
median_income = float(samp['median_income'].values[0]) |
|
|
|
sample_data.append([longitude,latitude,housing_median_age,total_rooms,total_bedrooms,population,households,median_income]) |
|
return sample_data |
|
|
|
|
|
def predict_price(longitude,latitude,housing_median_age,total_rooms,total_bedrooms,population,households,median_income): |
|
|
|
|
|
|
|
|
|
|
|
data = {'longitude':[float(longitude)], |
|
'latitude':[float(latitude)], |
|
'housing_median_age':[float(housing_median_age)], |
|
'total_rooms':[float(total_rooms)], |
|
'total_bedrooms':[float(total_bedrooms)], |
|
'population':[float(population)], |
|
'households':[float(households)], |
|
'median_income':[float(median_income)], |
|
} |
|
|
|
|
|
|
|
test_features = pd.DataFrame(data) |
|
|
|
test_features = test_features.dropna(subset=["total_bedrooms"]) |
|
|
|
|
|
|
|
test_features_normalized = scaler.transform(test_features) |
|
test_features_normalized |
|
|
|
pred = lin_reg.predict(test_features_normalized)[0] |
|
|
|
map_file = draw_map_customize(longitude,latitude, fig_id='test',fig_extension='png' ) |
|
|
|
return pred,map_file |
|
|
|
|
|
|
|
|
|
|
|
set_longitude = gr.inputs.Slider(-124.350000, -114.310000, step=0.5, default=-120, label = 'Longitude') |
|
set_latitude = gr.inputs.Slider(32, 41, step=0.5, default=33, label = 'Latitude') |
|
set_housing_median_age = gr.inputs.Slider(1, 52, step=1, default=10, label = 'Housing_median_age (Year)') |
|
set_total_rooms = gr.inputs.Slider(1, 40000, step=5, default=10000, label = 'Total_rooms') |
|
set_total_bedrooms = gr.inputs.Slider(1, 6445, step=5, default=5000, label = 'Total_bedrooms') |
|
set_population = gr.inputs.Slider(3, 35682, step=5, default=10, label = 'Population') |
|
set_households = gr.inputs.Slider(1, 6082, step=5, default=10, label = 'Households') |
|
set_median_income = gr.inputs.Slider(0, 15, step=0.5, default=10, label = 'Median_income') |
|
|
|
|
|
|
|
set_label = gr.outputs.Textbox(label="Predicted Housing Prices") |
|
|
|
|
|
set_out_images = gr.outputs.Image(label="Visualize your location") |
|
|
|
|
|
|
|
interface = gr.Interface(fn=predict_price, |
|
inputs=[set_longitude, set_latitude,set_housing_median_age,set_total_rooms,set_total_bedrooms,set_population,set_households,set_median_income], |
|
outputs=[set_label,set_out_images], |
|
examples_per_page = 2, |
|
examples = get_sample_data(10), |
|
title="CSCI4750/5750 Demo 3: Web Application for Housing Price Prediction", |
|
description= "Click examples below for a quick demo", |
|
theme = 'huggingface', |
|
layout = 'vertical' |
|
) |
|
interface.launch(debug=True) |