<jupyter_start><jupyter_text>Import our data<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# https://stackoverflow.com/questions/18039057/python-pandas-error-tokenizing-data <NAME> answer for error
# that occurred here.
%matplotlib inline
# col_names = ["year", "make", "model", "trim", "body", "transmission", "vin", "state", "condition",
#             "odometer", "color", "interior", "seller", "mmr", "sellingprice", "saledate"]
car_sales = pd.read_csv("resources/data/car_prices.csv", error_bad_lines=False, warn_bad_lines=False)
car_sales.head()
car_sales.describe()<jupyter_output><empty_output><jupyter_text>**Clean the data**<jupyter_code># Get datatypes of columns
car_sales.dtypes
correlation = car_sales.corr()
top_corr_features = correlation.index
plt.figure(figsize=(20,20))

# plot heat map
graph = sns.heatmap(car_sales[top_corr_features].corr(), annot=True, cmap="RdYlGn")
# Drop data columns as to make modelling take less time, model may be less accurate though because of this
# Also drop the columns that don't have a great correlation.
car_sales = car_sales.drop(['vin', 'saledate', 'seller', 'trim', 'state',
                           'model', 'transmission', 'interior', 'odometer', 'body'], axis = 1)
# Drop Null rows, since this is a large data set it won't effect it too much.
print(car_sales.isna().sum())
car_sales.count()
car_sales = car_sales.dropna()
# Should all equal the same value
car_sales.count()
# Let's try and convert our data to numbers
# Turn the categories into numbers
dummies = pd.get_dummies(car_sales, drop_first = True)
dummies
# Create X (features matrix)
x = dummies.drop("sellingprice", axis = 1)

# Create y (labels)
y = dummies["sellingprice"]<jupyter_output><empty_output><jupyter_text>If running without a loaded model run the following code<jupyter_code># Modelling
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn import metrics

# Create model
reg_model = RandomForestRegressor()

# Split data (increase test_size to decrease model .pk1 file)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.99)

# Fit model
reg_model.fit(x_train, y_train);
#print(reg_model.feature_importances_)

# Make predictions
y_preds = reg_model.predict(x_test)
y_preds

# Score model
reg_model_score = reg_model.score(x_test, y_test)
reg_model_score

# Save a model
import pickle

pickle.dump(reg_model, open("random_forest_model.pk1", "wb"))
reg_model.score(x_test, y_test)<jupyter_output><empty_output><jupyter_text>If running from a loaded model run the following code<jupyter_code># Modelling
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn import metrics
import pickle

# Open model
reg_model = RandomForestRegressor()
reg_model = pickle.load(open("random_forest_model.pk1", "rb"))

# Split data
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.99)

# # Fit model
# reg_model.fit(x_train, y_train);

# Make predictions
y_preds = reg_model.predict(x_test)
y_preds

# Score model and store to variables
reg_model_score = reg_model.score(x_test, y_test)
mean_abs_err = metrics.mean_absolute_error(y_test, y_preds)
# Run this before importing seaborn if not already done !conda install -c anaconda seaborn -y
import seaborn as sns
sns.pairplot(car_sales)
sns.regplot(x = y_test, y = y_preds);<jupyter_output><empty_output><jupyter_text>Prediction method<jupyter_code># from sklearn import metrics
# # Declaring variables and initializing
# cols = list(x.columns)
# # Year, Condition, MMR (mean from car_sales.describe()), Make, Body, Color
# arr = [1990, 5., 13769.324646, '<NAME>', 'pink']
# make_index = cols.index("make_" + arr[3])
# color_index = cols.index("color_" + arr[4])

# # Year, Condition, MMR filling blank data
# array_to_predict = [1, 0.0, 5555]
# for i in range(111):
#     array_to_predict.append(0)

# # Get indeces for where values lie and change to proper ones
# array_to_predict[0] = arr[0] # Set year to predict
# array_to_predict[1] = arr[1] # Set condition to predict
# array_to_predict[2] = arr[2] # Set MMR
# array_to_predict[make_index] = 1 # Set Make to 1
# array_to_predict[color_index] = 1 # Set color to 1 to represent correct color

# # Create an array of 
# cols_conv = pd.DataFrame(cols)

# df = pd.DataFrame(data = [array_to_predict], columns = cols_conv,  )

# reg_model.predict(df)
# calculate_car_price takes inputs from the prediction widget for regression analysis
# Output is a predicted used car price based on values provided


def calculate_car_price(year, quality, make, color):
    
    # Declaring variables and initializing
    cols = list(x.columns)
    
    # Get mmr for car make
    mean = car_sales.groupby("make").mean()
    mean.mmr[make.value]

    # Year, Condition, MMR (mean from car_sales.describe()), Make, Body, Color
    arr = [year.value, quality.value, mean.mmr[make.value], make.value, color.value] # temp values
    make_index = cols.index("make_" + arr[3])
    color_index = cols.index("color_" + arr[4])

    # Year, Condition, MMR filling blank data
    array_to_predict = [1, 0.0, 5555]
    for i in range(111):
        array_to_predict.append(0)

    # Get indeces for where values lie and change to proper ones
    array_to_predict[0] = arr[0] # Set year to predict
    array_to_predict[1] = arr[1] # Set condition to predict
    array_to_predict[2] = arr[2] # Set MMR
    array_to_predict[make_index] = 1 # Set Make to 1
    array_to_predict[color_index] = 1 # Set color to 1 to represent correct color

    # Create an array of 
    cols_conv = pd.DataFrame(cols)

    df = pd.DataFrame(data = [array_to_predict], columns = cols_conv)

    predicted_price = reg_model.predict(df.values).astype(float)
    
    return predicted_price
import ipywidgets as widgets
from re import search

# List that holds all the columns of our DataFrame
columns = list(dummies.columns)
makes = []
colors = []

# Get make of vehicles and add to make list and remove item from columns
i = 0
for i in range(len(columns)):
    if search('make', columns[i]):
        makes.append(columns[i][5:])

    elif search('color', columns[i]):
        colors.append(columns[i][6:])

# Create actual dropdown
make = widgets.Dropdown(options = makes, description = "Make of car:")
year = widgets.Dropdown(options = range(1980,2022), description = "Year of car:")
color = widgets.Dropdown(options = colors, description = "Color of car:")
quality = widgets.Dropdown(options = np.arange(0, 5.5, .5).round(1), description = "Quality of car:")

widgets_box = widgets.VBox([make, year, color, quality])

# List of widget box children for tab
children = [widgets_box]

# Initialize tab and set children
tab = widgets.Tab()
tab.children = children

# Box title
tab.set_title(0, 'Car Price Prediction')
tab

from sklearn import metrics
def button_clicked(_):
    # Link function with output
    predicted_price = calculate_car_price(year, quality, make, color)
    
    # On Button press do the following
    with out:
        out.clear_output()
        print("--Prediction--")
        print("Regression model predicts approximately $", f"{predicted_price[0]:.2f}", "dollars for your car.")
        print("--Error Analysis--")
        print('Mean Absolute Error:', mean_abs_err)
        print('Accuracy Score:', f"{reg_model_score * 100:.2f}%" )
    return

# calculate_prediction execution button harvest input from sliders
calc_button = widgets.Button(description='Calculate')
out = widgets.Output()

# Link button and function together using button method
calc_button.on_click(button_clicked)
# Display button and output together
widgets.VBox([calc_button,out])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11207890354809787, 'fraction_numerical': 0.013833738952222365, 'mean_word_length': 4.844311377245509, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '256086', 'license': 'MIT', 'n_tokens_mistral': 2698, 'n_tokens_neox': 2558, 'n_words': 746, 'path': 'carsales-ml-model.ipynb', 'repo_name': 'hewittaj/c964_wgu_capstone', 'size': '7818'}