<jupyter_start><jupyter_text><jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn
from sklearn.model_selection import train_test_split as tts
from sklearn import preprocessing
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder
import tensorflow as tf
import pandas.util.testing as tm
train_data = pd.read_csv('train.csv')
train_data.shape
StandardScaler = StandardScaler()
train_data.head()
plt.figure()
sns.heatmap(train_data.isnull(),yticklabels=False,cbar=False,cmap='viridis')
le = LabelEncoder()

X_train = train_data.drop(['Id',	'EmployeeNumber', 'YearsSinceLastPromotion', 'EducationField', 'StockOptionLevel', 'BusinessTravel'], axis=1)


from sklearn.preprocessing import LabelEncoder
X=X_train
categorical_feature_mask = X.dtypes==object

categorical_cols = X.columns[categorical_feature_mask].tolist()

le = LabelEncoder()
X[categorical_cols] = X[categorical_cols].apply(lambda col: le.fit_transform(col))+1
X_train = X

X_train.head()
plt.figure(figsize=(15,12))
sns.heatmap(train_data.corr())
Edu_Age_TotalWorking = np.array(X_train['Education'] / (X_train['Age'] + X_train['TotalWorkingYears']))
CurrMgr_Coy = np.array(X_train['YearsWithCurrManager'] / (X_train['YearsAtCompany'] + 1))
CurrRole_CurrMgr = np.array(X_train['YearsInCurrentRole'] / (X_train['YearsWithCurrManager'] + 1))


print(Edu_Age_TotalWorking)
X_train.insert(1,'EAT_working',  Edu_Age_TotalWorking)
X_train.insert(2, 'CM_Coy', CurrMgr_Coy)
X_train.insert(3, 'CR_CM', CurrRole_CurrMgr)

X_train.drop(columns=['Education', 'TotalWorkingYears', 'YearsWithCurrManager', 'YearsAtCompany', 'YearsInCurrentRole'], inplace=True)
X_train.head()
X_train.shape
y_train=X_train['Attrition']

X_train.drop(columns=['Attrition'], inplace=True, axis=1)
X_train.head()
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2

X=X_train
y=y_train

print(X.shape)
X_norm = MinMaxScaler().fit_transform(X)
chi_selector = SelectKBest(chi2, k=15)
chi_selector.fit(X_norm, y)
chi_support = chi_selector.get_support()
chi_feature = X.loc[:,chi_support].columns.tolist()
print(chi_feature)

X_train = X_train.loc[:, chi_feature]
X_train = pd.DataFrame(data = (X_train+0.1).apply(np.log), columns=X_train.columns)
X_train.head()
from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
X_train = pd.DataFrame(data=scaler.fit_transform(X_train), columns=X_train.columns)

X_train.shape
import imblearn
from imblearn.over_sampling import SMOTE

sm = SMOTE(random_state = 1)
X_train, y_train = sm.fit_sample(X_train, y_train.ravel())

pd.Series(y_train).value_counts().plot.bar()
X_train.shape
from sklearn.model_selection import train_test_split as tts

X_train, X_validation, y_train, y_validation = tts(X_train, y_train, test_size=0.2, train_size=0.8)
print(X_train.shape)
print(y_train.shape)
print(X_validation.shape)
print(y_validation.shape)
from sklearn.ensemble import RandomForestClassifier

model = RandomForestClassifier(random_state = 1, max_depth = 9, n_estimators = 1500, min_samples_split = 2, min_samples_leaf = 1)
model = model.fit(X_train, y_train)
from sklearn.metrics import roc_auc_score
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score

Y_predict_training = model.predict(X_train)
print(roc_auc_score(y_train, Y_predict_training))
Y_predict_validation = model.predict(X_validation)
from sklearn.metrics import accuracy_score
print(roc_auc_score(y_validation, Y_predict_validation))
import seaborn as sns
test_data = pd.read_csv('test.csv')
ID_test = test_data.Id

test_data.drop(['Id',	'EmployeeNumber', 'YearsSinceLastPromotion', 'EducationField', 'StockOptionLevel', 'BusinessTravel'], axis=1, inplace=True)

X_test = test_data

plt.figure()
sns.heatmap(X_test.isnull(),yticklabels=False,cbar=False,cmap='viridis')
X=X_test
categorical_feature_mask = X.dtypes==object
categorical_cols = X.columns[categorical_feature_mask].tolist()
le = LabelEncoder()
X[categorical_cols] = X[categorical_cols].apply(lambda col: le.fit_transform(col))+1
X_test = X
Edu_Age_TotalWorking = np.array(X_test['Education'] / (X_test['Age'] + X_test['TotalWorkingYears']))
CurrMgr_Coy = np.array(X_test['YearsWithCurrManager'] / (X_test['YearsAtCompany'] + 1))
CurrRole_CurrMgr = np.array(X_test['YearsInCurrentRole'] / (X_test['YearsWithCurrManager'] + 1))
X_test.insert(1,'EAT_working',  Edu_Age_TotalWorking)
X_test.insert(2, 'CM_Coy', CurrMgr_Coy)
X_test.insert(3, 'CR_CM', CurrRole_CurrMgr)

print(chi_feature)

X_test = X_test.loc[:, chi_feature]
X_test.head()
X_test = pd.DataFrame(data=(X_test+0.1).apply(np.log), columns=X_test.columns)

min_max_scaler.fit(X_test)
X_test = pd.DataFrame(scaler.transform(X_test), columns=X_test.columns)
Y_predict = model.predict_proba(X_test)
Y_predict = Y_predict[:, 1]
Y_predict = pd.DataFrame({'Id': ID_test, 'Attrition': Y_predict})

Y_predict.head(40)

Y_predict.to_csv('Submission_3.csv', index=False, header=True)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12610315748185918, 'fraction_numerical': 0.008825259854873504, 'mean_word_length': 8.659090909090908, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '651258', 'license': 'Apache-2.0', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1894, 'n_words': 315, 'path': 'IITG_Summer_Analytics_Hackathon_20.ipynb', 'repo_name': 'Susheel06/IITG_SA20_Hackathon', 'size': '5099'}