<jupyter_start><jupyter_text>1. Data Exploration and Visualization:<jupyter_code>df = pd.read_csv("car data.csv")
df.head()
df["Selling_Price"] = df["Selling_Price"] * 11286
df["Present_Price"] = df["Present_Price"] * 11286
df.head()
df.info()
df["Car_Name"].nunique() # we have 98 different car models, we need to drop this feature
df.describe()
df.corr()["Selling_Price"]
plt.figure(figsize=(12,10))
sns.set_style("darkgrid")
sns.histplot(data=df, x="Selling_Price", bins=50,cbar=True, color="red")
plt.figure(figsize=(12,10))
sns.boxplot(data=df, x="Selling_Price",color="green")
df["Selling_Price"].describe()
df.corr()["Selling_Price"].sort_values(ascending=False)
plt.figure(figsize=(15,12))
sns.set_style("darkgrid")
sns.regplot(x=df["Year"], y=df["Selling_Price"])
plt.figure(figsize=(15,12))
sns.set_style("darkgrid")
sns.regplot(x=df["Present_Price"], y=df["Selling_Price"],color="red")
#Visualising numerical predictor variables with Target Variables
train_num = df.select_dtypes(include=['int64','float64'])
fig,axs= plt.subplots(12,3,figsize=(20,80))
#adjust horizontal space between plots 
fig.subplots_adjust(hspace=0.6)
for i,ax in zip(train_num.columns,axs.flatten()):
    sns.scatterplot(x=i, y='Selling_Price', hue='Selling_Price',data=train_num,ax=ax,palette='coolwarm')
    plt.xlabel(i,fontsize=12)
    plt.ylabel('Selling_Price',fontsize=12)
    #ax.set_yticks(np.arange(0,900001,100000))
    ax.set_title('Selling_Price'+' - '+str(i),fontweight='bold',size=20)
##Visualising Categorical predictor variables with Target Variables
categorical = df.select_dtypes(exclude=['int64','float64'])
def facetgrid_boxplot(x, y, **kwargs):
    sns.boxplot(x=x, y=y)
    x=plt.xticks(rotation=90)
    

f = pd.melt(df, id_vars=['Selling_Price'], value_vars=sorted(df[categorical.columns]))
g = sns.FacetGrid(f, col="variable", col_wrap=3, sharex=False, sharey=False, size=5)
g = g.map(facetgrid_boxplot, "value", "Selling_Price")
sns.pairplot(df)
plt.figure(figsize=(15,12))
sns.heatmap(df.corr(),cmap="jet",annot=True,linewidths=1,robust=True)
df.isnull().sum() #There is no missing data in the columns<jupyter_output><empty_output><jupyter_text>2. Feature Engineering<jupyter_code>df["Current_Year"] = 2021
df["Car's Age"] = df["Current_Year"] -df["Year"]
df.head()
#we will drop unnecessary features:
df.drop(["Car_Name","Year","Current_Year"],axis=1,inplace=True)
df.head()
df = pd.get_dummies(data= df,columns=["Fuel_Type","Seller_Type","Transmission"],drop_first=True)
df.head()
X = df.drop("Selling_Price",axis=1)
y = df["Selling_Price"]
X.head()
y.head()
#Lets find out the importance of features:
from sklearn.ensemble import ExtraTreesRegressor
model = ExtraTreesRegressor()
model.fit(X,y)
model.feature_importances_
plt.figure(figsize=(12,8))
pd.Series(model.feature_importances_,index=X.columns).plot(kind="barh")<jupyter_output><empty_output><jupyter_text>3. Model Training<jupyter_code>#First lets siplit our data into train and test sets:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.1)
print(X_train.shape)
print(y_train.shape)
print(X_test.shape)
print(y_test.shape)
from sklearn.ensemble import RandomForestRegressor
forest = RandomForestRegressor()
#Choosing best hyperparameters:
# Number of trees in random forest
n_estimators = [int(x) for x in np.linspace(start = 100, stop = 1200, num = 12)]
# Number of features to consider at every split
max_features = ['auto', 'sqrt']
# Maximum number of levels in tree
max_depth = [int(x) for x in np.linspace(5, 30, num = 6)]
# max_depth.append(None)
# Minimum number of samples required to split a node
min_samples_split = [2, 5, 10, 15, 100]
# Minimum number of samples required at each leaf node
min_samples_leaf = [1, 2, 5, 10]
# Create the random grid for these hyperparameters:
random_grid = {'n_estimators': n_estimators,
               'max_features': max_features,
               'max_depth': max_depth,
               'min_samples_split': min_samples_split,
               'min_samples_leaf': min_samples_leaf}

print(random_grid)
from sklearn.model_selection import RandomizedSearchCV
random = RandomizedSearchCV(estimator = forest, param_distributions = random_grid,scoring='neg_mean_squared_error', n_iter = 10, cv = 5, verbose=2, random_state=42, n_jobs = 1)
random.fit(X_train,y_train)
random.best_params_
predictions = random.predict(X_test)
plt.figure(figsize=(14,9))
sns.distplot(y_test-predictions)

plt.figure(figsize=(14,9))
sns.scatterplot(x=predictions,y=y_test,color="red")
from sklearn import metrics
print('MAE:', metrics.mean_absolute_error(y_test, predictions))
print('MSE:', metrics.mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))
import pickle
# open a file, where you ant to store the data
file = open('latest_model3.pkl', 'wb')

# dump information to that file
pickle.dump(random, file)
file.close()
predictions
X_test.iloc[:1,:]
y_test.iloc[:1,:]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15161161957819339, 'fraction_numerical': 0.02626343016315161, 'mean_word_length': 7.4487394957983195, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '183462', 'license': 'Apache-2.0', 'n_tokens_mistral': 1949, 'n_tokens_neox': 1826, 'n_words': 354, 'path': 'car_prices.ipynb', 'repo_name': 'abrahamanderson19972020/Car-Price-Prediction', 'size': '5026'}