-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_app.py
91 lines (82 loc) · 4.63 KB
/
main_app.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Code for 'main_app.py' file.
# Importing the necessary Python modules.
import streamlit as st
import numpy as np
import pandas as pd
# Import the individual Python files
import home
import data
import plots
import predict
# Configure your home page by setting its title and icon that will be displayed in a browser tab.
st.set_page_config(page_title = 'Car Price Prediction',
page_icon = ':car:',
layout = 'centered',
initial_sidebar_state = 'auto'
)
# Dictionary containing positive integers in the form of words as keys and corresponding former as values.
words_dict = {"two": 2, "three": 3, "four": 4, "five": 5, "six": 6, "eight": 8, "twelve": 12}
def num_map(series):
return series.map(words_dict)
# Loading the dataset.
@st.cache()
def load_data():
# Reading the dataset
cars_df = pd.read_csv("car-prices.csv")
# Extract the name of the manufactures from the car names and display the first 25 cars to verify whether names are extracted successfully.
car_companies = pd.Series([car.split(" ")[0] for car in cars_df['CarName']], index = cars_df.index)
# Create a new column named 'car_company'. It should store the company names of a the cars.
cars_df['car_company'] = car_companies
# Replace the misspelled 'car_company' names with their correct names.
cars_df.loc[(cars_df['car_company'] == "vw") | (cars_df['car_company'] == "vokswagen"), 'car_company'] = 'volkswagen'
cars_df.loc[cars_df['car_company'] == "porcshce", 'car_company'] = 'porsche'
cars_df.loc[cars_df['car_company'] == "toyouta", 'car_company'] = 'toyota'
cars_df.loc[cars_df['car_company'] == "Nissan", 'car_company'] = 'nissan'
cars_df.loc[cars_df['car_company'] == "maxda", 'car_company'] = 'mazda'
cars_df.drop(columns= ['CarName'], axis = 1, inplace = True)
cars_numeric_df = cars_df.select_dtypes(include = ['int64', 'float64'])
cars_numeric_df.drop(columns = ['car_ID'], axis = 1, inplace = True)
# Map the values of the 'doornumber' and 'cylindernumber' columns to their corresponding numeric values.
cars_df[['cylindernumber', 'doornumber']] = cars_df[['cylindernumber', 'doornumber']].apply(num_map, axis = 1)
# Create dummy variables for the 'carbody' columns.
car_body_dummies = pd.get_dummies(cars_df['carbody'], dtype = int)
# Create dummy variables for the 'carbody' columns with 1 column less.
car_body_new_dummies = pd.get_dummies(cars_df['carbody'], drop_first = True, dtype = int)
# Create a DataFrame containing all the non-numeric type features.
cars_categorical_df = cars_df.select_dtypes(include = ['object'])
#Get dummy variables for all the categorical type columns using the dummy coding process.
cars_dummies_df = pd.get_dummies(cars_categorical_df, drop_first = True, dtype = int)
# Drop the categorical type columns from the 'cars_df' DataFrame.
cars_df.drop(list(cars_categorical_df.columns), axis = 1, inplace = True)
# Concatenate the 'cars_df' and 'cars_dummies_df' DataFrames.
cars_df = pd.concat([cars_df, cars_dummies_df], axis = 1)
# Drop the 'car_ID' column
cars_df.drop('car_ID', axis = 1, inplace = True)
final_columns = ['carwidth', 'enginesize', 'horsepower', 'drivewheel_fwd', 'car_company_buick', 'price']
return cars_df[final_columns]
final_cars_df = load_data()
# Adding a navigation in the sidebar using radio buttons.
# Create a dictionary.
pages_dict = {
"Home": home,
"View Data": data,
"Visualise Data": plots,
"Predict": predict
}
# Add radio buttons in the sidebar for navigation and call the respective pages based on user selection.
st.sidebar.title('Navigation')
user_choice = st.sidebar.radio("Go to", tuple(pages_dict.keys()))
if user_choice == "Home":
home.app()
else:
selected_page = pages_dict[user_choice]
selected_page.app(final_cars_df)
# This 'app()' function is defined in all the 'home.py', data.py', 'plots.py' and 'predict.py' files.
# Whichever option out of "View Data", "Visualise Data" and "Predict" a user selects, that option gets stored in the
# 'selection' variable and the correspoding value to that key gets stored in the 'page' variable and then the 'app()'
# function gets called from that Python file
# which could be either of 'home.py', 'data.py', 'plots.py' and 'predict.py' files in this case.
# Add a short description of video and embed a YouTube video inside the expander.
# with st.beta_expander("Watch Video"):
# st.write("You can learn how to host a Streamlit app on Heroku by watching this video:")
# st.video("https://youtu.be/oBA5I__AfmY")