# -*- coding: utf-8 -*- """Untitled0.ipynb Automatically generated by Colaboratory. Original file is located at https://colab.research.google.com/drive/1fyFBMy_3a9yZumgON5rfSKvfibePbsRy """ # First, install Gradio #!pip install --quiet gradio import pandas as pd housing = pd.read_csv("housing.csv") import numpy as np # split data to get train and test set from sklearn.model_selection import train_test_split train_set, test_set = train_test_split(housing, test_size=0.2, random_state=42) #clean the missing values 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) #scale the numeric features in training set 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 housing_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(columns=['longitude', 'latitude', 'housing_median_age', 'total_rooms', 'total_bedrooms', 'population', 'households', 'median_income']) test_features = test_features.append(data,ignore_index=True) test_features = test_features.dropna(subset=["total_bedrooms"]) test_features_normalized = scaler.transform(test_features) test_features_normalized prediction = lin_reg.predict(test_features_normalized)[0] return prediction import gradio as gr input_module1 = gr.inputs.Slider(-124, -114, step=1, label = "Longitude") input_module2 = gr.inputs.Slider(32, 41, step=1, label = "Latitude") input_module3 = gr.inputs.Slider(1, 52, step=1, label = "Housing median age") input_module4 = gr.inputs.Slider(2, 39329, step=10, label = "Total rooms") input_module5 = gr.inputs.Slider(1, 6445, step=10, label = "Total bedrooms") input_module6 = gr.inputs.Slider(3, 35682, step=10, label = "Population") input_module7 = gr.inputs.Slider(1, 6082, step=10, label = "Households") input_module8 = gr.inputs.Slider(0.5, 15, step=0.5, label = "Median Income") output_module1 = gr.outputs.Textbox(label = "Predicted House Prices") gr.Interface(fn = housing_price , inputs =[input_module1, input_module2, input_module3, input_module4, input_module5, input_module6, input_module7, input_module8], outputs=[output_module1] ).launch()