Spaces:
Build error
Build error
Limit to 6 features
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ from sklearn.linear_model import LinearRegression
|
|
| 6 |
from sklearn.ensemble import RandomForestRegressor
|
| 7 |
from sklearn.datasets import fetch_california_housing
|
| 8 |
|
| 9 |
-
def trainme(MedInc,
|
| 10 |
|
| 11 |
#Call base dataset
|
| 12 |
housing = fetch_california_housing()
|
|
@@ -14,36 +14,29 @@ def trainme(MedInc, HouseAge, AveRooms, AveBedrms, Population, AveOccup, Latitud
|
|
| 14 |
#As a dataframe, with tragets
|
| 15 |
housing_df = pd.DataFrame(data=housing.data, columns=housing.feature_names)
|
| 16 |
housing_df["Price"] = housing.target
|
|
|
|
| 17 |
|
| 18 |
#Put the new line at index 0
|
| 19 |
-
housing_df.iloc[0] = [MedInc,
|
| 20 |
|
| 21 |
#Split
|
| 22 |
y = pd.DataFrame(data=housing_df['Price']).to_numpy()
|
| 23 |
-
X = housing_df
|
| 24 |
|
| 25 |
#create a machine learning model and train it
|
| 26 |
regressor = RandomForestRegressor(n_estimators = 10, random_state = 0)
|
| 27 |
-
regressor.fit(X,y)
|
| 28 |
|
| 29 |
#Create a redable/clean feature list
|
| 30 |
-
clean_features = ['Median income',
|
| 31 |
-
'Median house age',
|
| 32 |
-
'Average number of rooms per household',
|
| 33 |
-
'Average number of bedrooms per household',
|
| 34 |
-
'Population of the block',
|
| 35 |
-
'Average number of household members',
|
| 36 |
-
'Latitude of the block',
|
| 37 |
-
'Longitude of the block']
|
| 38 |
|
| 39 |
#Create a lime object, regression mode
|
| 40 |
from lime import lime_tabular
|
| 41 |
explainer = lime_tabular.LimeTabularExplainer(X, mode="regression", feature_names=clean_features)
|
| 42 |
|
| 43 |
#Create the expl
|
| 44 |
-
explanation = explainer.explain_instance(X[0], regressor.predict, num_features=
|
| 45 |
listing = explanation.as_list()
|
| 46 |
-
#explanation.show_in_notebook(show_table=True)
|
| 47 |
|
| 48 |
#Get pred and actual scores
|
| 49 |
Pred_value = regressor.predict(X[0].reshape(1,-1))*100000
|
|
@@ -159,17 +152,14 @@ def trainme(MedInc, HouseAge, AveRooms, AveBedrms, Population, AveOccup, Latitud
|
|
| 159 |
|
| 160 |
return final_list
|
| 161 |
|
| 162 |
-
#Define gradio UI
|
| 163 |
|
| 164 |
input = [gr.inputs.Slider(0.5, 15, default=4.406300, label='Median income'),
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
gr.inputs.Slider(32, 42, default=38.260000, label='Latitude of the block'),
|
| 171 |
-
gr.inputs.Slider(-124, -111, default=-120.330000, label='Longitude of the block'),
|
| 172 |
-
gr.inputs.Slider(0.14, 5, default=1.558000, label='Price'),
|
| 173 |
]
|
| 174 |
output = [gr.outputs.Textbox(label='Prediction'),
|
| 175 |
gr.outputs.Textbox(label='Why?'),
|
|
@@ -182,4 +172,4 @@ iface = gr.Interface(fn=trainme,
|
|
| 182 |
outputs=output,
|
| 183 |
title = '🔮 Explain me like I\'m 5',
|
| 184 |
description="Get explanations from a model prediction")
|
| 185 |
-
iface.launch(
|
|
|
|
| 6 |
from sklearn.ensemble import RandomForestRegressor
|
| 7 |
from sklearn.datasets import fetch_california_housing
|
| 8 |
|
| 9 |
+
def trainme(MedInc, AveOccup, HouseAge, Latitude, Longitude, Price):
|
| 10 |
|
| 11 |
#Call base dataset
|
| 12 |
housing = fetch_california_housing()
|
|
|
|
| 14 |
#As a dataframe, with tragets
|
| 15 |
housing_df = pd.DataFrame(data=housing.data, columns=housing.feature_names)
|
| 16 |
housing_df["Price"] = housing.target
|
| 17 |
+
housing_df = housing_df.drop(['AveBedrms', 'Population', 'AveRooms'], axis=1)
|
| 18 |
|
| 19 |
#Put the new line at index 0
|
| 20 |
+
housing_df.iloc[0] = [MedInc, AveOccup, HouseAge, Latitude, Longitude, Price]
|
| 21 |
|
| 22 |
#Split
|
| 23 |
y = pd.DataFrame(data=housing_df['Price']).to_numpy()
|
| 24 |
+
X = housing_df[['MedInc','AveOccup','HouseAge', 'Latitude', 'Longitude']].to_numpy()
|
| 25 |
|
| 26 |
#create a machine learning model and train it
|
| 27 |
regressor = RandomForestRegressor(n_estimators = 10, random_state = 0)
|
| 28 |
+
regressor.fit(X,np.ravel(y))
|
| 29 |
|
| 30 |
#Create a redable/clean feature list
|
| 31 |
+
clean_features = ['Median income','Average number of household members','Median house age','Latitude', 'Longitude']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
#Create a lime object, regression mode
|
| 34 |
from lime import lime_tabular
|
| 35 |
explainer = lime_tabular.LimeTabularExplainer(X, mode="regression", feature_names=clean_features)
|
| 36 |
|
| 37 |
#Create the expl
|
| 38 |
+
explanation = explainer.explain_instance(X[0], regressor.predict, num_features=5)
|
| 39 |
listing = explanation.as_list()
|
|
|
|
| 40 |
|
| 41 |
#Get pred and actual scores
|
| 42 |
Pred_value = regressor.predict(X[0].reshape(1,-1))*100000
|
|
|
|
| 152 |
|
| 153 |
return final_list
|
| 154 |
|
| 155 |
+
#Define gradio UI MedInc, AveOccup, HouseAge, Latitude, Longitude, Price):
|
| 156 |
|
| 157 |
input = [gr.inputs.Slider(0.5, 15, default=4.406300, label='Median income'),
|
| 158 |
+
gr.inputs.Slider(1, 30, default=2.446602, label='Average number of household members'),
|
| 159 |
+
gr.inputs.Slider(1, 52, default=13, label='Median house age'),
|
| 160 |
+
gr.inputs.Slider(32, 42, default=38.260000, label='Latitude of the block'),
|
| 161 |
+
gr.inputs.Slider(-124, -111, default=-120.330000, label='Longitude of the block'),
|
| 162 |
+
gr.inputs.Slider(0.14, 5, default=1.558000, label='Price'),
|
|
|
|
|
|
|
|
|
|
| 163 |
]
|
| 164 |
output = [gr.outputs.Textbox(label='Prediction'),
|
| 165 |
gr.outputs.Textbox(label='Why?'),
|
|
|
|
| 172 |
outputs=output,
|
| 173 |
title = '🔮 Explain me like I\'m 5',
|
| 174 |
description="Get explanations from a model prediction")
|
| 175 |
+
iface.launch()
|