Kaggle competition: Cloud Faculty Institute Workshop
<SOS> metric: meanfscore Kaggle data source: cloud-faculty-institute-workshop<split>
%matplotlib inline
rng = np.random.RandomState(31337)
def extract_recent_data(oldest_year):
print('extract_recent_data(oldest_year={})'.format(oldest_year))
global df_train, df_test
df_train = df_train[df_train['issue_d'] >= dt.datetime(oldest_year, 1, 1)]
df_test = df_test[df_test['issue_d'] >= dt.datetime(oldest_year, 1, 1)]
return<data_type_conversions>
dataset = read_csv('.. /input/train.csv')
 dataset=dataset[['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age','Outcome']]
dataset.head(10)
def ordinal2int(convert_target_col):
print('ordinal2int('{}')'.format(convert_target_col))
global df_train, df_test
convert_mae = sorted(df_train[convert_target_col].unique())
 convert_ato = list(range(len(convert_mae)) [::-1])
 df_train[convert_target_col] = df_train[convert_target_col].replace(convert_mae, convert_ato)
 df_train[convert_target_col] = df_train[convert_target_col].astype(int)
 df_test[convert_target_col] = df_test[convert_target_col].replace(convert_mae, convert_ato)
 df_test[convert_target_col] = df_test[convert_target_col].astype(int)
 return<categorify>
trainingData = read_csv('.. /input/train.csv')
 trainingData=trainingData[['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age','Outcome']]
testingData = read_csv('.. /input/test.csv')
 testingData=testingData[['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']]
trainingFeatures = trainingData.iloc[:, :-1]
trainingLabels = trainingData.iloc[:, -1]
imputer = SimpleImputer(missing_values=0,strategy='median')
 trainingFeatures = imputer.fit_transform(trainingFeatures)
 trainingFeatures = pd.DataFrame(trainingFeatures)
 trainingFeatures.columns=['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
testingData = imputer.transform(testingData)
 testingData = pd.DataFrame(testingData)
 testingData.columns=['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
print("
print("
Column Name
")
 print(( trainingFeatures[:] == 0 ).sum() )
def emplength2int() :
print('emplength2int() ')
 global df_train, df_test
convert_target_col = 'emp_length'
df_train[convert_target_col] = df_train[convert_target_col].fillna('
df_test[convert_target_col] = df_test[convert_target_col].fillna('
convert_mae = ['
convert_ato = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
df_train[convert_target_col] = df_train[convert_target_col].replace(convert_mae, convert_ato)
 df_train[convert_target_col] = df_train[convert_target_col].astype(int)
 df_test[convert_target_col] = df_test[convert_target_col].replace(convert_mae, convert_ato)
 df_test[convert_target_col] = df_test[convert_target_col].astype(int)
 return<data_type_conversions>
trainingFeatures2 = trainingFeatures.drop(['Id'], axis=1 )
def object2str(convert_target_col):
print('object2str('{}')'.format(convert_target_col))
global df_train, df_test
df_train[convert_target_col] = df_train[convert_target_col].fillna('
df_train[convert_target_col] = df_train[convert_target_col].astype(str)
 df_test[convert_target_col] = df_test[convert_target_col].fillna('
df_test[convert_target_col] = df_test[convert_target_col].astype(str)
 return<categorify>
kf = KFold(n_splits=2, shuffle=True, random_state=rng)
 X_train, X_test, y_train, y_test = train_test_split(trainingFeatures2, trainingLabels, test_size=0.2, random_state=rng)
 first_model = XGBClassifier().fit(X_train, y_train)
 model = GridSearchCV(first_model, {'max_depth': [2,4,6], 'n_estimators': [50,100,200]}, verbose=1)
 model.fit(X_train,y_train)
 predictions = model.predict(X_test)
 actuals = y_test
print(confusion_matrix(actuals, predictions))
def preprocess_text(preprocess_target_col):
print('preprocess_text('{}')'.format(preprocess_target_col))
global df_train, df_test, sr_title_train, sr_title_test, sr_emp_title_train, sr_emp_title_test
def replace_text(x):
x = x.replace('\t', '' ).replace('!', '' ).replace('?', '' ).replace(':)', '' ).replace('&', '')\ 
.replace('/', '' ).replace('-', '' ).replace('.', '' ).replace('"', '' ).replace (<categorify>
Feature Importance:
',fullList,'
' )
def preprocess_words_of_text(preprocess_target_col):
print('preprocess_words_of_text('{}')'.format(preprocess_target_col))
global df_train, df_test
def replace_text(x):
x = x.replace('my ', '' ).replace('bye ', '' ).replace('the ', '')
 x = x.replace('cards', 'card' ).replace('credit consolidation', 'credit card consolidation')\ 
.replace('payoff', 'pay off' ).replace('cc', 'credit card' ).replace('improvements', 'improvement')\ 
.replace('refi ', 'refinancing ' ).replace('refinance', 'refinancing' ).replace('consoildation', 'consolidation')\ 
.replace('considation', 'consolidation' ).replace('consolodation', 'consolidation')\ 
.replace('refy ', 'refinancing ' ).replace('bills', 'bill')
 x = re.sub('refi$', 'refinancing', x)
 x = re.sub('refy$', 'refinancing', x)
 return x
df_train[preprocess_target_col] = df_train[preprocess_target_col].map(replace_text)
 df_test[preprocess_target_col] = df_test[preprocess_target_col].map(replace_text)
 return<data_type_conversions>
kfold = KFold(n_splits=100, shuffle=True, random_state=rng)
 results = cross_val_score(model, trainingFeatures2, trainingLabels, cv=kfold)
 print("DecisionTreeClassifier:
Cross_Val_Score: %.2f%%(%.2f%%)" %(results.mean() *100, results.std() *100))
prediction = model.predict(X_test)
 cnf_matrix = confusion_matrix(y_test, prediction)
 dict_characters = {0: 'Healthy', 1: 'Diabetes'}
plot_confusion_matrix(cnf_matrix, classes=dict_characters,title='Confusion matrix' )
<categorify><EOS> 
test = testingData
test = pd.DataFrame(test)
 test.columns=['Id','Pregnancies','Glucose','BloodPressure','SkinThickness','Insulin','BMI','DiabetesPedigreeFunction','Age']
test2 = test.drop(['Id'], axis=1)
 my_predictions = model.predict(test2)
 Identifier = test.Id.astype(int)
 my_submission = pd.DataFrame({'Id': Identifier, 'Outcome': my_predictions})
 my_submission.to_csv('submission_mendoza.csv', index=False)
 my_submission.head()