File size: 1,193 Bytes
cb68ede
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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()