Kaggle competition: Digit Recognizer
distinct_triples_counts = dict()
for arr in train_df.loc[train_df["NumberOfTimes90DaysLate"] > 17][late_pay_cols].values:
triple = ",".join(list(map(str, arr)))
 if triple not in distinct_triples_counts:
distinct_triples_counts[triple] = 0
else:
distinct_triples_counts[triple] += 1
distinct_triples_counts<count_values>
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.utils import np_utils
train_df.loc[train_df["DebtRatio"] > train_df["DebtRatio"].quantile(0.975)]['MonthlyIncome'].value_counts()<filter>
( X_train, y_train),(X_test, y_test)= mnist.load_data()
len(train_df[(train_df["DebtRatio"] > train_df["DebtRatio"].quantile(0.9)) &(train_df['SeriousDlqin2yrs'] == train_df['MonthlyIncome'])] )<count_values>
num_pixels = X_train.shape[1] * X_train.shape[2]
X_train = X_train.reshape(( X_train.shape[0], num_pixels)).astype('float32')
 X_test = X_test.reshape(( X_test.shape[0], num_pixels)).astype('float32' )
train_df["NumberRealEstateLoansOrLines"].value_counts()<count_values>
X_train = X_train / 255
X_test = X_test / 255
train_df["NumberOfDependents"].value_counts()<rename_columns>
y_train = np_utils.to_categorical(y_train)
 y_test = np_utils.to_categorical(y_test)
 num_classes = y_test.shape[1]
for df in combine:
df.rename(columns={'Unnamed: 0':'ID'}, inplace=True )<data_type_conversions>
def baseline_model() :
	
	model = Sequential()
	model.add(Dense(num_pixels, input_dim=num_pixels, kernel_initializer='normal', activation='relu'))
	model.add(Dense(num_classes, kernel_initializer='normal', activation='softmax'))
	
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 	return model
for df in combine:
df['NumberOfDependents'].fillna(0, inplace=True )<prepare_x_and_y>
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200, verbose=2)
 scores = model.evaluate(X_test, y_test, verbose=0)
 print("Baseline Error: %.2f%%" %(100-scores[1]*100))
def randomforest_filled_func(df):
train = df[df.MonthlyIncome.notnull() ]
test = df[df.MonthlyIncome.isnull() ]
train_x = train.iloc[:,2:].drop('MonthlyIncome',axis=1)
 train_y = train['MonthlyIncome']
test_x = test.iloc[:,2:].drop('MonthlyIncome',axis=1)
 rfr = RandomForestRegressor(random_state=2021, n_estimators=200,max_depth=3,n_jobs=-1)
 rfr.fit(train_x, train_y)
 predicted = rfr.predict(test_x ).round(0)
 print(predicted)
 df.loc[(df.MonthlyIncome.isnull()), 'MonthlyIncome'] = predicted
return df<statistical_test>
( X_train, y_train),(X_test, y_test)= mnist.load_data()
X_train = X_train.reshape(( X_train.shape[0], 28, 28, 1)).astype('float32')
 X_test = X_test.reshape(( X_test.shape[0], 28, 28, 1)).astype('float32')
 X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
 y_test = np_utils.to_categorical(y_test)
 num_classes = y_test.shape[1]
def baseline_model() :
	
	model = Sequential()
	model.add(Conv2D(32,(5, 5), input_shape=(28, 28, 1), activation='relu'))
	model.add(MaxPooling2D())
 	model.add(Dropout(0.2))
	model.add(Flatten())
 	model.add(Dense(128, activation='relu'))
	model.add(Dense(num_classes, activation='softmax'))
	
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 	return model
model_simple = baseline_model()
model_simple.summary()
for df in combine:
df = randomforest_filled_func(df )<filter>
model_simple.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)
 scores = model_simple.evaluate(X_test, y_test, verbose=1)
 print("CNN Error: %.2f%%" %(100-scores[1]*100))
train_df = train_df[train_df['age'] > 0]<filter>
( X_train, y_train),(X_test, y_test)= mnist.load_data()
X_train = X_train.reshape(( X_train.shape[0], 28, 28, 1)).astype('float32')
 X_test = X_test.reshape(( X_test.shape[0], 28, 28, 1)).astype('float32')
 X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
 y_test = np_utils.to_categorical(y_test)
 num_classes = y_test.shape[1]
def large_model() :
	
	model = Sequential()
	model.add(Conv2D(30,(5, 5), input_shape=(28, 28, 1), activation='relu'))
	model.add(MaxPooling2D())
 	model.add(Conv2D(15,(3, 3), activation='relu'))
	model.add(MaxPooling2D())
 	model.add(Dropout(0.2))
	model.add(Flatten())
 	model.add(Dense(128, activation='relu'))
	model.add(Dense(50, activation='relu'))
	model.add(Dense(num_classes, activation='softmax'))
	
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 	return model
model_large = large_model()
model_large.summary()
train_df = train_df[train_df['RevolvingUtilizationOfUnsecuredLines'] < 13]<filter>
model_large.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)
 scores = model_large.evaluate(X_test, y_test, verbose=1)
 print("Large CNN Error: %.2f%%" %(100-scores[1]*100))
train_df = train_df[train_df['NumberOfTimes90DaysLate'] <= 17]<filter>
( X_train, y_train),(X_test, y_test)= mnist.load_data()
X_train = X_train.reshape(( X_train.shape[0], 28, 28, 1)).astype('float32')
 X_test = X_test.reshape(( X_test.shape[0], 28, 28, 1)).astype('float32')
 X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
 y_test = np_utils.to_categorical(y_test)
 num_classes = y_test.shape[1]
def larger_model() :
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28,28,1)))
 model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.1))
model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(Conv2D(filters=192, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.1))
model.add(Conv2D(filters=192, kernel_size=5, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2, padding='same'))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model
model_larger = larger_model()
model_larger.summary()
train_df = train_df.loc[train_df["DebtRatio"] <= train_df["DebtRatio"].quantile(0.975)]<count_duplicates>
model_larger.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=100)
 scores = model_larger.evaluate(X_test, y_test, verbose=1)
 print("Larger CNN Error: %.2f%%" %(100-scores[1]*100))
train_df.duplicated().value_counts()<remove_duplicates>
X_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' ).values.astype('float32')
 X_test = X_test.reshape(-1, 28, 28, 1)
 X_test = X_test.astype('float32')/255
testY = model_larger.predict_classes(X_test, verbose=1 )
train_df = train_df.drop_duplicates()<split>
sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 sub['Label'] = testY
sub.to_csv('submission.csv',index=False )
x_train = train_df.iloc[:,2:]
y_train = train_df['SeriousDlqin2yrs'].astype('uint8')
 train_X, test_X, train_y, test_y = train_test_split(x_train,y_train,test_size=.1,random_state=2021, stratify = y_train )<find_best_model_class>
YouTubeVideo('3JQ3hYko51Y', width=800, height=450 )
num_estimators = range(100,401,10)
 train_scores = []
test_scores = []
for num_estimator in num_estimators:
lgbm = LGBMClassifier(is_unbalance = True,num_leaves=60,learning_rate=0.02,n_estimators=num_estimator)
 lgbm.fit(train_X,train_y)
 pre_y1 = lgbm.predict_proba(train_X)[:,1]
pre_y2 = lgbm.predict_proba(test_X)[:,1]
train_scores.append(roc_auc_score(train_y, pre_y1))
test_scores.append(roc_auc_score(test_y, pre_y2))
fig = plt.figure()
ax = fig.add_subplot(111)
 ax.plot(num_estimators,test_scores,label='Testing Score')
 ax.set_xlabel(r'num')
 ax.set_ylabel(r'auc')
 ax.set_title('num_estimators')
 ax.legend(loc='best')
 plt.show()<find_best_params>
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.utils import np_utils
num_estimators[test_scores.index(max(test_scores)) ]<filter>
( X_train, y_train),(X_test, y_test)= mnist.load_data()
learning_rates [test_scores.index(max(test_scores)) ]<filter>
num_pixels = X_train.shape[1] * X_train.shape[2]
X_train = X_train.reshape(( X_train.shape[0], num_pixels)).astype('float32')
 X_test = X_test.reshape(( X_test.shape[0], num_pixels)).astype('float32' )
max_bins[test_scores.index(max(test_scores)) ]<filter>
X_train = X_train / 255
X_test = X_test / 255
num_leaves[test_scores.index(max(test_scores)) ]<filter>
y_train = np_utils.to_categorical(y_train)
 y_test = np_utils.to_categorical(y_test)
 num_classes = y_test.shape[1]
max_depths[test_scores.index(max(test_scores)) ]<filter>
def baseline_model() :
	
	model = Sequential()
	model.add(Dense(num_pixels, input_dim=num_pixels, kernel_initializer='normal', activation='relu'))
	model.add(Dense(num_classes, kernel_initializer='normal', activation='softmax'))
	
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 	return model
feature_fractions[test_scores.index(max(test_scores)) ]<train_model>
model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200, verbose=2)
 scores = model.evaluate(X_test, y_test, verbose=0)
 print("Baseline Error: %.2f%%" %(100-scores[1]*100))
lgbm = LGBMClassifier(is_unbalance = True,max_depth=14,num_leaves=30,max_bin=40,learning_rate=0.025,n_estimators=180,feature_fraction=0.6)
 lgbm.fit(train_X,train_y)
 pre_y = lgbm.predict_proba(test_X)[:,1]<predict_on_test>
( X_train, y_train),(X_test, y_test)= mnist.load_data()
X_train = X_train.reshape(( X_train.shape[0], 28, 28, 1)).astype('float32')
 X_test = X_test.reshape(( X_test.shape[0], 28, 28, 1)).astype('float32')
 X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
 y_test = np_utils.to_categorical(y_test)
 num_classes = y_test.shape[1]
def baseline_model() :
	
	model = Sequential()
	model.add(Conv2D(32,(5, 5), input_shape=(28, 28, 1), activation='relu'))
	model.add(MaxPooling2D())
 	model.add(Dropout(0.2))
	model.add(Flatten())
 	model.add(Dense(128, activation='relu'))
	model.add(Dense(num_classes, activation='softmax'))
	
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 	return model
model_simple = baseline_model()
model_simple.summary()
test_df_x = test_df.iloc[:,2:]
pre_y2 = lgbm.predict_proba(test_df_x)[:,1]<load_from_csv>
model_simple.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)
 scores = model_simple.evaluate(X_test, y_test, verbose=1)
 print("CNN Error: %.2f%%" %(100-scores[1]*100))
result = pd.read_csv('/kaggle/input/GiveMeSomeCredit/sampleEntry.csv')
 result['Probability'] = pre_y2
result.to_csv('./submit.csv',index=False)
 reload = pd.read_csv('./submit.csv')
 reload<load_from_csv>
( X_train, y_train),(X_test, y_test)= mnist.load_data()
X_train = X_train.reshape(( X_train.shape[0], 28, 28, 1)).astype('float32')
 X_test = X_test.reshape(( X_test.shape[0], 28, 28, 1)).astype('float32')
 X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
 y_test = np_utils.to_categorical(y_test)
 num_classes = y_test.shape[1]
def large_model() :
	
	model = Sequential()
	model.add(Conv2D(30,(5, 5), input_shape=(28, 28, 1), activation='relu'))
	model.add(MaxPooling2D())
 	model.add(Conv2D(15,(3, 3), activation='relu'))
	model.add(MaxPooling2D())
 	model.add(Dropout(0.2))
	model.add(Flatten())
 	model.add(Dense(128, activation='relu'))
	model.add(Dense(50, activation='relu'))
	model.add(Dense(num_classes, activation='softmax'))
	
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 	return model
model_large = large_model()
model_large.summary()
warnings.filterwarnings("ignore")
 x_train = pd.read_csv('/kaggle/input/GiveMeSomeCredit/cs-training.csv')
 x_test = pd.read_csv('/kaggle/input/GiveMeSomeCredit/cs-test.csv')
 combine = [x_train, x_test]<count_missing_values>
model_large.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200)
 scores = model_large.evaluate(X_test, y_test, verbose=1)
 print("Large CNN Error: %.2f%%" %(100-scores[1]*100))
x_train.isnull().mean()<count_missing_values>
( X_train, y_train),(X_test, y_test)= mnist.load_data()
X_train = X_train.reshape(( X_train.shape[0], 28, 28, 1)).astype('float32')
 X_test = X_test.reshape(( X_test.shape[0], 28, 28, 1)).astype('float32')
 X_train = X_train / 255
X_test = X_test / 255
y_train = np_utils.to_categorical(y_train)
 y_test = np_utils.to_categorical(y_test)
 num_classes = y_test.shape[1]
def larger_model() :
model = Sequential()
model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu', input_shape=(28,28,1)))
 model.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu'))
model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.1))
model.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))
model.add(Conv2D(filters=192, kernel_size=3, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Dropout(0.1))
model.add(Conv2D(filters=192, kernel_size=5, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2, padding='same'))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model
model_larger = larger_model()
model_larger.summary()
x_test.isnull().mean()<count_missing_values>
model_larger.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=100)
 scores = model_larger.evaluate(X_test, y_test, verbose=1)
 print("Larger CNN Error: %.2f%%" %(100-scores[1]*100))
[x_train[x_train['NumberOfDependents'].isnull() ][x_train['MonthlyIncome'].notnull() ]['DebtRatio'].count() ,
x_test[x_test['NumberOfDependents'].isnull() ][x_test['MonthlyIncome'].notnull() ]['DebtRatio'].count() ]<define_variables>
X_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' ).values.astype('float32')
 X_test = X_test.reshape(-1, 28, 28, 1)
 X_test = X_test.astype('float32')/255
testY = model_larger.predict_classes(X_test, verbose=1 )
x_train[x_train['NumberOfDependents'].notnull() ][x_train['MonthlyIncome'].isnull() ][['NumberOfDependents']].median()<import_modules>
sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
 sub['Label'] = testY
sub.to_csv('submission.csv',index=False )
<feature_engineering><EOS> 
YouTubeVideo('3JQ3hYko51Y', width=800, height=450 )