Spaces:
Sleeping
Sleeping
Commit
•
a831d9a
1
Parent(s):
d76811f
Upload 3 files
Browse files- app.py +102 -0
- model.py +131 -0
- mpg_model.pkl +3 -0
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "728431f5",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": []
|
10 |
+
},
|
11 |
+
{
|
12 |
+
"cell_type": "code",
|
13 |
+
"execution_count": 1,
|
14 |
+
"id": "fd56baf1",
|
15 |
+
"metadata": {},
|
16 |
+
"outputs": [
|
17 |
+
{
|
18 |
+
"name": "stderr",
|
19 |
+
"output_type": "stream",
|
20 |
+
"text": [
|
21 |
+
"2023-12-25 15:31:55.354 \n",
|
22 |
+
" \u001b[33m\u001b[1mWarning:\u001b[0m to view this Streamlit app on a browser, run it with the following\n",
|
23 |
+
" command:\n",
|
24 |
+
"\n",
|
25 |
+
" streamlit run C:\\Users\\user\\anaconda3\\Lib\\site-packages\\ipykernel_launcher.py [ARGUMENTS]\n"
|
26 |
+
]
|
27 |
+
}
|
28 |
+
],
|
29 |
+
"source": [
|
30 |
+
"import streamlit as st\n",
|
31 |
+
"import pandas as pd\n",
|
32 |
+
"import joblib\n",
|
33 |
+
"\n",
|
34 |
+
"# Load trained model\n",
|
35 |
+
"model = joblib.load('mpg_model.pkl') # Ensure this path is correct\n",
|
36 |
+
"\n",
|
37 |
+
"def user_input_features():\n",
|
38 |
+
" cylinders = st.sidebar.slider('Cylinders', 3, 8, 4)\n",
|
39 |
+
" displacement = st.sidebar.number_input('Displacement')\n",
|
40 |
+
" horsepower = st.sidebar.number_input('Horsepower')\n",
|
41 |
+
" weight = st.sidebar.number_input('Weight')\n",
|
42 |
+
" acceleration = st.sidebar.number_input('Acceleration')\n",
|
43 |
+
" model_year = st.sidebar.slider('Model Year', 70, 82, 76)\n",
|
44 |
+
" data = {'cylinders': cylinders,\n",
|
45 |
+
" 'displacement': displacement,\n",
|
46 |
+
" 'horsepower': horsepower,\n",
|
47 |
+
" 'weight': weight,\n",
|
48 |
+
" 'acceleration': acceleration,\n",
|
49 |
+
" 'model_year': model_year}\n",
|
50 |
+
" features = pd.DataFrame(data, index=[0])\n",
|
51 |
+
" return features\n",
|
52 |
+
"\n",
|
53 |
+
"# Main Streamlit app interface\n",
|
54 |
+
"st.write(\"\"\"\n",
|
55 |
+
"# Simple MPG Prediction App\n",
|
56 |
+
"This app predicts the **Miles Per Gallon (MPG)** of your car!\n",
|
57 |
+
"\"\"\")\n",
|
58 |
+
"\n",
|
59 |
+
"# User input features\n",
|
60 |
+
"input_df = user_input_features()\n",
|
61 |
+
"\n",
|
62 |
+
"# Display the user input features\n",
|
63 |
+
"st.subheader('User Input features')\n",
|
64 |
+
"st.write(input_df)\n",
|
65 |
+
"\n",
|
66 |
+
"# Predict and display the output\n",
|
67 |
+
"st.subheader('Prediction')\n",
|
68 |
+
"prediction = model.predict(input_df)\n",
|
69 |
+
"st.write(f'Predicted MPG: {prediction[0]:.2f}')"
|
70 |
+
]
|
71 |
+
},
|
72 |
+
{
|
73 |
+
"cell_type": "code",
|
74 |
+
"execution_count": null,
|
75 |
+
"id": "f8836f1f",
|
76 |
+
"metadata": {},
|
77 |
+
"outputs": [],
|
78 |
+
"source": []
|
79 |
+
}
|
80 |
+
],
|
81 |
+
"metadata": {
|
82 |
+
"kernelspec": {
|
83 |
+
"display_name": "Python 3 (ipykernel)",
|
84 |
+
"language": "python",
|
85 |
+
"name": "python3"
|
86 |
+
},
|
87 |
+
"language_info": {
|
88 |
+
"codemirror_mode": {
|
89 |
+
"name": "ipython",
|
90 |
+
"version": 3
|
91 |
+
},
|
92 |
+
"file_extension": ".py",
|
93 |
+
"mimetype": "text/x-python",
|
94 |
+
"name": "python",
|
95 |
+
"nbconvert_exporter": "python",
|
96 |
+
"pygments_lexer": "ipython3",
|
97 |
+
"version": "3.11.5"
|
98 |
+
}
|
99 |
+
},
|
100 |
+
"nbformat": 4,
|
101 |
+
"nbformat_minor": 5
|
102 |
+
}
|
model.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"id": "e2d9e6fa",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import seaborn as sns\n",
|
11 |
+
"import pandas as pd\n",
|
12 |
+
"from sklearn.model_selection import train_test_split\n",
|
13 |
+
"from sklearn.linear_model import LinearRegression\n",
|
14 |
+
"from sklearn.metrics import mean_squared_error, r2_score\n",
|
15 |
+
"\n",
|
16 |
+
"# Load dataset\n",
|
17 |
+
"df = sns.load_dataset('mpg')\n",
|
18 |
+
"df.dropna(inplace=True) # Dropping missing values"
|
19 |
+
]
|
20 |
+
},
|
21 |
+
{
|
22 |
+
"cell_type": "code",
|
23 |
+
"execution_count": 2,
|
24 |
+
"id": "6eb9757f",
|
25 |
+
"metadata": {},
|
26 |
+
"outputs": [],
|
27 |
+
"source": [
|
28 |
+
"# Selecting relevant features for simplicity\n",
|
29 |
+
"features = df[['cylinders', 'displacement', 'horsepower', 'weight', 'acceleration', 'model_year']]\n",
|
30 |
+
"target = df['mpg']\n",
|
31 |
+
"\n",
|
32 |
+
"# Splitting the dataset into training and testing sets\n",
|
33 |
+
"X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)"
|
34 |
+
]
|
35 |
+
},
|
36 |
+
{
|
37 |
+
"cell_type": "code",
|
38 |
+
"execution_count": 4,
|
39 |
+
"id": "72821417",
|
40 |
+
"metadata": {},
|
41 |
+
"outputs": [],
|
42 |
+
"source": [
|
43 |
+
"# Create and train the model\n",
|
44 |
+
"model = LinearRegression()\n",
|
45 |
+
"model.fit(X_train, y_train)\n",
|
46 |
+
"\n",
|
47 |
+
"# Predictions and Evaluation\n",
|
48 |
+
"y_pred = model.predict(X_test)"
|
49 |
+
]
|
50 |
+
},
|
51 |
+
{
|
52 |
+
"cell_type": "code",
|
53 |
+
"execution_count": 6,
|
54 |
+
"id": "5dc111db",
|
55 |
+
"metadata": {},
|
56 |
+
"outputs": [
|
57 |
+
{
|
58 |
+
"name": "stdout",
|
59 |
+
"output_type": "stream",
|
60 |
+
"text": [
|
61 |
+
"Requirement already satisfied: joblib in c:\\users\\user\\anaconda3\\lib\\site-packages (1.2.0)\n"
|
62 |
+
]
|
63 |
+
}
|
64 |
+
],
|
65 |
+
"source": [
|
66 |
+
"#!pip install joblib"
|
67 |
+
]
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"cell_type": "code",
|
71 |
+
"execution_count": 7,
|
72 |
+
"id": "c41776ae",
|
73 |
+
"metadata": {},
|
74 |
+
"outputs": [],
|
75 |
+
"source": [
|
76 |
+
"import joblib"
|
77 |
+
]
|
78 |
+
},
|
79 |
+
{
|
80 |
+
"cell_type": "code",
|
81 |
+
"execution_count": 8,
|
82 |
+
"id": "318d866d",
|
83 |
+
"metadata": {},
|
84 |
+
"outputs": [
|
85 |
+
{
|
86 |
+
"data": {
|
87 |
+
"text/plain": [
|
88 |
+
"['mpg_model.pkl']"
|
89 |
+
]
|
90 |
+
},
|
91 |
+
"execution_count": 8,
|
92 |
+
"metadata": {},
|
93 |
+
"output_type": "execute_result"
|
94 |
+
}
|
95 |
+
],
|
96 |
+
"source": [
|
97 |
+
"# Save the model\n",
|
98 |
+
"joblib.dump(model, 'mpg_model.pkl')"
|
99 |
+
]
|
100 |
+
},
|
101 |
+
{
|
102 |
+
"cell_type": "code",
|
103 |
+
"execution_count": null,
|
104 |
+
"id": "7636f0d3",
|
105 |
+
"metadata": {},
|
106 |
+
"outputs": [],
|
107 |
+
"source": []
|
108 |
+
}
|
109 |
+
],
|
110 |
+
"metadata": {
|
111 |
+
"kernelspec": {
|
112 |
+
"display_name": "Python 3 (ipykernel)",
|
113 |
+
"language": "python",
|
114 |
+
"name": "python3"
|
115 |
+
},
|
116 |
+
"language_info": {
|
117 |
+
"codemirror_mode": {
|
118 |
+
"name": "ipython",
|
119 |
+
"version": 3
|
120 |
+
},
|
121 |
+
"file_extension": ".py",
|
122 |
+
"mimetype": "text/x-python",
|
123 |
+
"name": "python",
|
124 |
+
"nbconvert_exporter": "python",
|
125 |
+
"pygments_lexer": "ipython3",
|
126 |
+
"version": "3.11.5"
|
127 |
+
}
|
128 |
+
},
|
129 |
+
"nbformat": 4,
|
130 |
+
"nbformat_minor": 5
|
131 |
+
}
|
mpg_model.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3eda59e103b0644df6fd16650bd9da8556b44235c569eef8dd0dcc6dac8213f0
|
3 |
+
size 1032
|