# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import pandas as pd
import numpy as np

from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.preprocessing import LabelEncoder

# +
train = pd.read_csv('train.csv',parse_dates=['issue_date','listing_date'])

train = train.sample(frac=1).reset_index(drop=True)

#filling the missing the value of condition
train.fillna(3,inplace=True)

# +
train['area_occupied'] = train['height(cm)'] * train['length(m)'] * 100
train['no_of_days'] =abs((train['listing_date'] - train['issue_date']).dt.days)
train['listing_day'] = train['listing_date'].dt.day
train['listing_month'] = train['listing_date'].dt.month
train['listing_dayofweek'] = train['listing_date'].dt.dayofweek
train['listing_weekday'] = train['listing_date'].dt.weekday
train['listing_hour'] = train['listing_date'].dt.hour
train['listing_time'] = train['listing_date'].dt.time

train['issue_day'] = train['issue_date'].dt.day
train['issue_month'] = train['listing_date'].dt.month
train['issue_dayofweek'] = train['listing_date'].dt.dayofweek
train['issue_weekday'] = train['listing_date'].dt.weekday
train['issue_hour'] = train['listing_date'].dt.hour
train['issue_time'] = train['listing_date'].dt.time

train['pet_id'] = train['pet_id'].apply(lambda x: int(x.split('_')[1]))

#get some with pet_id
def id_bins(s):
    if s['pet_id'] <= 63355:
        return 'OLD'
    if s['pet_id'] <= 70150:
        return 'MID'
    else:
        return 'NEW'

train['id_bins'] = train.apply(id_bins,axis=1)

def height_bins(s):
    
    if s['height(cm)'] <=27.36:
        return 'SHORT'
    else:
        return 'TALL'
    
train['height_bin'] = train.apply(height_bins,axis=1)

cat = list(train.select_dtypes('object'))
print('categorical values',cat)
for i in cat:
    l = LabelEncoder()
    l.fit(train[i])
    train[i]= l.transform(train[i])
    
#train = pd.get_dummies(train,
#                          columns=['color_type','id_bins', 'height_bin'],
#                          drop_first=True)

#train.drop(['pet_id'])
print(train.shape)
# -

train.head()

X = train.drop(['breed_category','pet_category',
                'issue_date','listing_date'],axis=1)
y = train[['breed_category']]
X.shape , y.shape

bestfeatures = SelectKBest(score_func=chi2,k=20)
fit=bestfeatures.fit(X,y)

# +
df1 = pd.DataFrame(fit.scores_)
df2 = pd.DataFrame(X.columns)

df = pd.concat([df1,df2],axis=1)
df.columns = ['specs','score']
df.sort_values(by=['specs'],ascending=False).head(10)
# -

print(df.nlargest(10,'specs'))

# +
from sklearn.ensemble import ExtraTreesClassifier
import matplotlib.pyplot as plt
model = ExtraTreesClassifier(random_state=10)
model.fit(X,y)

d= {'columns':X.columns,'importance':model.feature_importances_}
df = pd.DataFrame(data=d)
df.sort_values(by='importance',ascending=False).head(10)
# -

fe = pd.Series(model.feature_importances_,index=X.columns)
fe.nlargest(10).plot(kind='barh')
plt.show()

import seaborn as sns
corrmat = train.corr()
top_corr_features = corrmat.index
plt.figure(figsize=(30,30))
g = sns.heatmap(train[top_corr_features].corr(),annot=True,cmap='RdYlGn')







StarcoderdataJupyterScriptsDedupFiltered