<jupyter_start><jupyter_text>KNN (K Nearest Neighbor - regression)<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

import statsmodels.formula.api as sm
import scipy.stats as stats

%matplotlib inline
plt.rcParams['figure.figsize'] = 10, 7.5
plt.rcParams['axes.grid'] = True
plt.gray()

from matplotlib.backends.backend_pdf import PdfPages

from sklearn.model_selection import train_test_split
from sklearn import metrics

from statsmodels.stats.outliers_influence import variance_inflation_factor
from patsy import dmatrices
from sklearn.metrics import mean_absolute_error, mean_squared_error

import sklearn.neighbors as kn
from sklearn.neighbors import KNeighborsRegressor

from sklearn import metrics

from sklearn.model_selection import GridSearchCV


url = 'https://raw.githubusercontent.com/mkmritunjay/machineLearning/master/Car_sales.csv'
car_sales = pd.read_csv(url)
car_sales.head()
car_sales.info()
car_sales.describe()
car_sales.dtypes<jupyter_output><empty_output><jupyter_text>Separate numerical and categorical data<jupyter_code># numerical
car_sales_num=car_sales._get_numeric_data()
car_sales_num.head()
# categorical
car_sales_cat=car_sales.select_dtypes(include=['object'])<jupyter_output><empty_output><jupyter_text>Creating Data audit Report<jupyter_code># Use a general function that returns multiple values
def var_summary(x):
    return pd.Series([x.count(), x.isnull().sum(), x.sum(), x.mean(), x.median(),  x.std(), x.var(), x.min(), x.dropna().quantile(0.01), x.dropna().quantile(0.05),x.dropna().quantile(0.10),x.dropna().quantile(0.25),x.dropna().quantile(0.50),x.dropna().quantile(0.75), x.dropna().quantile(0.90),x.dropna().quantile(0.95), x.dropna().quantile(0.99),x.max()], 
                  index=['N', 'NMISS', 'SUM', 'MEAN','MEDIAN', 'STD', 'VAR', 'MIN', 'P1' , 'P5' ,'P10' ,'P25' ,'P50' ,'P75' ,'P90' ,'P95' ,'P99' ,'MAX'])

num_summary=car_sales_num.apply(lambda x: var_summary(x)).T
num_summary
def cat_summary(x):
    return pd.Series([x.count(), x.isnull().sum(), x.value_counts()], 
                  index=['N', 'NMISS', 'ColumnsNames'])

cat_summary=car_sales_cat.apply(lambda x: cat_summary(x))
cat_summary<jupyter_output><empty_output><jupyter_text>Handling Outliers<jupyter_code>#Handling Outliers - Method
def outlier_capping(x):
    x = x.clip(upper=x.quantile(0.99))
    x = x.clip(lower=x.quantile(0.01))
    return x

car_sales_num=car_sales_num.apply(lambda x: outlier_capping(x))
num_summary = car_sales_num.apply(lambda x: var_summary(x)).T
num_summary<jupyter_output><empty_output><jupyter_text>Handling missing values<jupyter_code>#Handling missings - Method
def Missing_imputation(x):
    x = x.fillna(x.median())
    return x

car_sales_num=car_sales_num.apply(lambda x: Missing_imputation(x))
num_summary = car_sales_num.apply(lambda x: var_summary(x)).T
num_summary<jupyter_output><empty_output><jupyter_text>Handling Categorical Variablesscikit-learn expects all features to be numeric. So how do we include a categorical feature in our model?- **Ordered categories:** transform them to sensible numeric values (example: small=1, medium=2, large=3)- **Unordered categories:** use dummy encoding (0/1)What are the categorical features in our dataset?- **Ordered categories:** weather (already encoded with sensible numeric values)- **Unordered categories:** season (needs dummy encoding), holiday (already dummy encoded), workingday (already dummy encoded)For season, we can't simply leave the encoding as 1 = spring, 2 = summer, 3 = fall, and 4 = winter, because that would imply an **ordered relationship**. Instead, we create **multiple dummy variables:**<jupyter_code># A utility function to create dummy variable
def create_dummies( df, colname ):
    col_dummies = pd.get_dummies(df[colname], prefix=colname)
    col_dummies.drop(col_dummies.columns[0], axis=1, inplace=True)
    df = pd.concat([df, col_dummies], axis=1)
    df.drop( colname, axis = 1, inplace = True )
    return df
#for c_feature in categorical_features
car_sales_cat=car_sales[['Manufacturer', 'Vehicle_type']]

for c_feature in ['Manufacturer', 'Vehicle_type']:
    car_sales_cat[c_feature] = car_sales_cat[c_feature].astype('category')
    car_sales_cat = create_dummies(car_sales_cat , c_feature )
car_sales_cat.head()
# concat both numerical and categorical variables
X = pd.concat([car_sales_num, car_sales_cat], axis=1)
features = X.columns.difference(['Sales_in_thousands'])
features<jupyter_output><empty_output><jupyter_text>Train Test Split<jupyter_code>train_X, test_X, train_y, test_y = train_test_split( X[features],
                                                  X['Sales_in_thousands'],
                                                  test_size = 0.3,
                                                  random_state = 142 )<jupyter_output><empty_output><jupyter_text>Building the model<jupyter_code>tuned_parameters = [{'n_neighbors': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]}]

knn_clf = GridSearchCV(KNeighborsRegressor(),
                   tuned_parameters,
                   cv=5)
knn_clf.fit(train_X, train_y)
knn_clf.best_score_
knn_clf.best_params_<jupyter_output><empty_output><jupyter_text>Building final model with k=16<jupyter_code>knn_clf = KNeighborsRegressor( n_neighbors =16)
knn_clf.fit( train_X, train_y)
train_X['pred'] = knn_clf.predict(train_X)
test_X['pred'] = knn_clf.predict(test_X)
print('MAE:', metrics.mean_absolute_error(train_y, train_X['pred']))
print('MSE:', metrics.mean_squared_error(train_y, train_X['pred']))
print('RMSE:', np.sqrt(metrics.mean_squared_error(train_y, train_X['pred'])))
print('R-Squre:', metrics.r2_score(train_y, train_X['pred']))
print('MAE:', metrics.mean_absolute_error(test_y, test_X['pred']))
print('MSE:', metrics.mean_squared_error(test_y, test_X['pred']))
print('RMSE:', np.sqrt(metrics.mean_squared_error(test_y, test_X['pred'])))
print('R-Squre:', metrics.r2_score(test_y, test_X['pred']))<jupyter_output>MAE: 32.84538114583333
MSE: 2535.67998640824
RMSE: 50.35553580698193
R-Squre: 0.1772439116657094<jupyter_text>Decile Analysis<jupyter_code>train_X['Y'] = train_y

train_X['Deciles']=pd.qcut(train_X['pred'],10, labels=False)
test_X['Deciles']=pd.qcut(test_X['pred'],10, labels=False)
train_X[['Deciles','Y', 'pred']].groupby(train_X.Deciles).mean()
test_X['Y'] = test_y
test_X[['Deciles','Y', 'pred']].groupby(test_X.Deciles).mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14073385973060845, 'fraction_numerical': 0.027558445579811117, 'mean_word_length': 6.029379760609358, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 40, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '97326', 'license': 'MIT', 'n_tokens_mistral': 2507, 'n_tokens_neox': 2312, 'n_words': 429, 'path': 'KNNRegressor.ipynb', 'repo_name': 'mkmritunjay/machinelearning', 'size': '6459'}