CoolFace
Apppublic

ecgray2001/CSCI4750

sourceHugging Facelppl-1.3cupdated 4y agoView on Hugging Face
0likes
app.py64 linesDownload Raw Back to root
1# -*- coding: utf-8 -*-2"""Lab04.ipynb3 4Automatically generated by Colaboratory.5 6Original file is located at7    https://colab.research.google.com/drive/1nWqDykiddq7cSNwgvgH7ktJzcwJhbs8M8"""9from sklearn.model_selection import train_test_split10from sklearn.preprocessing import MinMaxScaler11from sklearn.tree import DecisionTreeRegressor12import gradio as gr13import pandas as pd14import numpy as np15 16housing = pd.read_csv("housing.csv")17 18 19def testPrice(input1,input2,input3,input4,input5,input6,input7,input8):20 21  ## 1. split data to get train and test set22  train_set, test_set = train_test_split(housing, test_size=0.2, random_state=10)23 24  ## 2. clean the missing values25  train_set_clean = train_set.dropna(subset=["total_bedrooms"])26  train_set_clean27 28  ## 2. derive training features and training labels 29  train_labels = train_set_clean["median_house_value"].copy() # get labels for output label Y30  train_features = train_set_clean.drop("median_house_value", axis=1) # drop labels to get features X for training set31 32  ## 4. scale the numeric features in training set33  scaler = MinMaxScaler() ## define the transformer34  scaler.fit(train_features) ## call .fit() method to calculate the min and max value for each column in dataset35 36  train_features_normalized = scaler.transform(train_features)37  train_features_normalized38 39  ## Step 1: training the data using decision tree algorithm40  tree_reg = DecisionTreeRegressor(random_state=42) ## Initialize the class41  tree_reg.fit(train_features_normalized, train_labels) # feed the training data X, and label Y for supervised learningg42 43  test_feature = np.array([[input1,input2,input3,input4,input5,input6,input7,input8]])44 45  test_features = scaler.transform(test_feature)46  training_predictions_trees = tree_reg.predict(test_features)47  return training_predictions_trees48 49input_module1 = gr.inputs.Slider(-124.35,-114.31,step=0.01,label="Longitude")50input_module2 = gr.inputs.Slider(32.54,41.95,step=0.01,label="Latitude")51input_module3 = gr.inputs.Slider(1,52,step=1,label="Median Age")52input_module4 = gr.inputs.Slider(2,39320,step=1,label="Total Rooms")53input_module5 = gr.inputs.Slider(1,6445,step=1,label="Total Bedrooms")54input_module6 = gr.inputs.Slider(3,35682,step=1,label="Populations")55input_module7 = gr.inputs.Slider(1,6082,step=1,label="Households")56input_module8 = gr.inputs.Slider(0.4999,15.0001,step=0.0001,label="Median Income")57 58output_module1 = gr.outputs.Textbox(label="Predicted Price")59 60gr.Interface(fn=testPrice,61             inputs=[input_module1,input_module2,input_module3,62                     input_module4,input_module5,input_module6,63                     input_module7,input_module8],64             outputs=[output_module1]).launch()