Spaces:
Build error
Build error
import streamlit as st | |
import numpy as np | |
# Model | |
import seaborn as sns | |
import pandas as pd | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import LinearRegression | |
from sklearn.metrics import mean_squared_error, r2_score | |
# Load dataset | |
df = sns.load_dataset('mpg') | |
df.dropna(inplace=True) # Dropping missing values | |
# Selecting relevant features for simplicity | |
features = df[['cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'model_year']] | |
target = df['mpg'] | |
# Splitting the dataset into training and testing sets | |
X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42) | |
# Create and train the model | |
model = LinearRegression() | |
model.fit(X_train, y_train) | |
# Write a function to take user inputs | |
def user_input_features(): | |
cylinders = st.sidebar.slider('Cylinders', 3, 8, 4) | |
displacement = st.sidebar.number_input('Displacement') | |
horsepower = st.sidebar.number_input('Horsepower') | |
weight = st.sidebar.number_input('Weight') | |
acceleration = st.sidebar.number_input('Acceleration') | |
model_year = st.sidebar.slider('Model Year', 70, 82, 76) | |
data = {'cylinders': cylinders, | |
'displacement': displacement, | |
'horsepower': horsepower, | |
'weight': weight, | |
'acceleration': acceleration, | |
'model_year': model_year} | |
features = pd.DataFrame(data, index=[0]) | |
return features | |
# Main | |
st.write(""" | |
# Simple MPG Prediction App | |
This app predicts the **Miles Per Gallon (MPG)** of your car! | |
""") | |
# User input features | |
input_df = user_input_features() | |
# Display the user input features | |
st.subheader('User Input features') | |
st.write(input_df) | |
# Predict and display the output | |
st.subheader('Prediction') | |
prediction = model.predict(input_df) | |
st.write(prediction) | |