streamlit / app1.py
YoneSlapWind80085's picture
Create app1.py
cb68ede verified
import gradio as gr
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.datasets import fetch_california_housing
import pickle
# Load the data
california = fetch_california_housing()
df = pd.DataFrame(california.data, columns=california.feature_names)
df['MedHouseVal'] = california.target
# Prepare the data for the model
X = df[['MedInc']]
y = df['MedHouseVal']
# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train the model
model = LinearRegression()
model.fit(X_train, y_train)
# Save the model
with open("linear_regression_model.pkl", "wb") as file:
pickle.dump(model, file)
# Load the model
with open("linear_regression_model.pkl", "rb") as file:
model = pickle.load(file)
# Define prediction function
def predict(med_inc):
X_new = np.array([[med_inc]])
prediction = model.predict(X_new)
return prediction[0]
# Create Gradio interface
iface = gr.Interface(fn=predict, inputs="number", outputs="number", title="California Housing Price Prediction")
iface.launch()