Jurk06's picture
Create app.py
2d6366a verified
raw
history blame contribute delete
No virus
1.61 kB
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
import gradio as gr
# ... (Rest of your code remains the same)
# # Select features and target
# Load the dataset
df = pd.read_csv('california_housing_train.csv')
col=['population', 'households', 'median_income']
features = df[col] # replace with actual feature names
target = df['median_house_value']
# Split the data
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)
# Standardize the data
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Train the model
model = LinearRegression()
model.fit(X_train_scaled, y_train)
# Evaluate the model
predictions = model.predict(X_test_scaled)
mse = mean_squared_error(y_test, predictions)
print(f'Mean Squared Error: {mse}')
# Function to make predictions
def predict_house_price(feature1, feature2, feature3):
input_data = scaler.transform([[feature1, feature2, feature3]])
prediction = model.predict(input_data)
return prediction[0]
# Create Gradio interface
iface = gr.Interface(
fn=predict_house_price,
inputs=[gr.Number(label="population"), gr.Number(label="households"), gr.Number(label="median_income")], # Use gr.Number directly
outputs="text",
title="House Price Prediction",
description="Enter the features to get the predicted house price."
)
# Launch the app
iface.launch()