<jupyter_start><jupyter_text>Import modules to run everything<jupyter_code>import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pickle
import ipywidgets as widgets
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import train_test_split
from sklearn import metrics
from re import search
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Import our dataset<jupyter_code># https://stackoverflow.com/questions/18039057/python-pandas-error-tokenizing-data <NAME> answer for error
# that occurred here.

# Deprecation error can be ignored.
car_sales = pd.read_csv("resources/data/car_prices.csv", on_bad_lines = 'skip')
# Display the first five rows of our data to check it is imported properly.
car_sales.head()<jupyter_output><empty_output><jupyter_text>Descriptive Method Here we describe the data to get some standard statistics such as mean, standard deviation, minimum, maximum, etc.<jupyter_code>car_sales.describe()<jupyter_output><empty_output><jupyter_text>Here we plot a histogram of all the selling prices in our data<jupyter_code>car_sales["sellingprice"].hist(figsize= (10, 10), bins = 200);<jupyter_output><empty_output><jupyter_text>Here we plot a heat map of how each column in the dataset correlates to eachother. - Positive numbers signify a positive correlation with negative numbers meaning less correlation.<jupyter_code>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")<jupyter_output><empty_output><jupyter_text>Here we plot each numerical value against eachother to display how they are related<jupyter_code># Run this before importing seaborn if not already done !conda install -c anaconda seaborn -y
sns.pairplot(car_sales, dropna = True);<jupyter_output><empty_output><jupyter_text>Clean the data<jupyter_code># 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.
car_sales = car_sales.dropna()
# Convert our categorical data to 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>Load model from .pk1 file using pickle and make predictions/score<jupyter_code># 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)

# 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)<jupyter_output><empty_output><jupyter_text>Plot the y_test and y_preds on a regression plot to see how close the model was in predicting prices.<jupyter_code>sns.regplot(x = y_test, y = y_preds);<jupyter_output><empty_output><jupyter_text>Create functions and widgets used for predicting a used car price<jupyter_code># 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 average mmr per car make
    mean = car_sales.groupby("make").mean()
    mean.mmr[make.value]

    # Year, Condition, MMR (mean from car_sales.describe()), Make, Body, Color
    # Set array to values we want to predict based on the values passed from the widget
    arr = [year.value, quality.value, mean.mmr[make.value], make.value, color.value] 
    
    # Get the index of where the value we want to find is equal to the column names
    make_index = cols.index("make_" + arr[3])
    color_index = cols.index("color_" + arr[4])

    # Year, Condition, MMR filling blank data into a format the prediction model will accept
    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 to represent make
    array_to_predict[color_index] = 1 # Set color to 1 to represent correct color

    # Create a dataframe of the converted columns and then combine with the array we created for prediction
    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
# Initialize variables 
columns = list(dummies.columns) # Columns holds the list of all the column headers in our car_sales.csv
makes = []
colors = []

# Populate our widget make/color dropdown boxes
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:")

# Create box
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
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.09700649534029936, 'fraction_numerical': 0.010166619598983339, 'mean_word_length': 4.887780548628429, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '586666', 'license': 'MIT', 'n_tokens_mistral': 2288, 'n_tokens_neox': 2169, 'n_words': 704, 'path': 'carsales-ml-model-final.ipynb', 'repo_name': 'hewittaj/c964_wgu_capstone', 'size': '7087'}