Spaces:
Sleeping
Sleeping
Create app1.py
Browse files
app1.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sklearn.linear_model import LinearRegression
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.datasets import fetch_california_housing
|
| 7 |
+
import pickle
|
| 8 |
+
|
| 9 |
+
# Load the data
|
| 10 |
+
california = fetch_california_housing()
|
| 11 |
+
df = pd.DataFrame(california.data, columns=california.feature_names)
|
| 12 |
+
df['MedHouseVal'] = california.target
|
| 13 |
+
|
| 14 |
+
# Prepare the data for the model
|
| 15 |
+
X = df[['MedInc']]
|
| 16 |
+
y = df['MedHouseVal']
|
| 17 |
+
|
| 18 |
+
# Split the data into training and testing sets
|
| 19 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 20 |
+
|
| 21 |
+
# Train the model
|
| 22 |
+
model = LinearRegression()
|
| 23 |
+
model.fit(X_train, y_train)
|
| 24 |
+
|
| 25 |
+
# Save the model
|
| 26 |
+
with open("linear_regression_model.pkl", "wb") as file:
|
| 27 |
+
pickle.dump(model, file)
|
| 28 |
+
|
| 29 |
+
# Load the model
|
| 30 |
+
with open("linear_regression_model.pkl", "rb") as file:
|
| 31 |
+
model = pickle.load(file)
|
| 32 |
+
|
| 33 |
+
# Define prediction function
|
| 34 |
+
def predict(med_inc):
|
| 35 |
+
X_new = np.array([[med_inc]])
|
| 36 |
+
prediction = model.predict(X_new)
|
| 37 |
+
return prediction[0]
|
| 38 |
+
|
| 39 |
+
# Create Gradio interface
|
| 40 |
+
iface = gr.Interface(fn=predict, inputs="number", outputs="number", title="California Housing Price Prediction")
|
| 41 |
+
iface.launch()
|