File size: 1,833 Bytes
207a615
 
 
2d64403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207a615
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)