Kaggle competition: Digit Recognizer
for dataset in combine:
dataset['Age*Class'] = dataset.Age * dataset.Pclass
train_data.loc[:, ['Age*Class', 'Age', 'Pclass']].head(10 )<save_to_csv>
train = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 train_image = np.array(train.drop(['label'], axis=1), dtype="float32")/ 255
train_image = train_image.reshape(-1, 28, 28, 1)
 train_label = tf.keras.utils.to_categorical(train['label'])
 test = np.array(test, dtype="float32")/ 255
test = test.reshape(-1, 28, 28, 1)
 show_images(train_image[:25], train_label[:25], shape=(5,5))
y = train_data["Survived"]
features = ["Pclass", "Sex", "Age", "Embarked", "Title", "IsAlone", "Age*Class"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )<save_to_csv>
( image_train_mnist, label_train_mnist),(image_test_mnist, label_test_mnist)= mnist.load_data()
image_mnist = np.concatenate(( image_train_mnist, image_test_mnist))
label_mnist = np.concatenate(( label_train_mnist, label_test_mnist))
image_mnist = image_mnist.reshape(-1,28,28,1)
 image_mnist = image_mnist.astype(np.float32)/ 255
label_mnist = tf.keras.utils.to_categorical(label_mnist,num_classes=10)
 images = np.concatenate(( train_image, image_mnist))
labels = np.concatenate(( train_label, label_mnist))
print("training image dataset shape:", images.shape)
 print("training label dataset shape:", labels.shape)
 show_images(images[:25], labels[:25], shape=(5,5))
y = train_data["Survived"]
features = ["Pclass", "Sex", "Age", "Embarked", "Title", "IsAlone"]
X = pd.get_dummies(train_data[features])
 X_test = pd.get_dummies(test_data[features])
 model = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=1)
 model.fit(X, y)
 predictions = model.predict(X_test)
 output = pd.DataFrame({'PassengerId': test_data.PassengerId, 'Survived': predictions})
 output.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )<import_modules>
datagen = tf.keras.preprocessing.image.ImageDataGenerator(
 rotation_range=20,
width_shift_range=0.20,
shear_range=15,
zoom_range=0.10,
validation_split=0.25,
horizontal_flip=False 
)
 train_generator = datagen.flow(
 images,
labels,
batch_size=256,
subset='training', 
)
 validation_generator = datagen.flow(
 images,
labels,
batch_size=64,
subset='validation',
)
import pandas as pd
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score<load_from_csv>
def create_model() :
model = tf.keras.Sequential([
tf.keras.layers.Reshape(( 28, 28, 1)) ,
tf.keras.layers.Conv2D(filters=32, kernel_size=(5,5), activation="relu", padding="same", input_shape=(28,28,1)) ,
tf.keras.layers.MaxPool2D(( 2,2)) ,
tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation="relu", padding="same"),
tf.keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation="relu", padding="same"),
tf.keras.layers.MaxPool2D(( 2,2)) ,
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation="relu", padding="same"),
tf.keras.layers.Conv2D(filters=128, kernel_size=(3,3), activation="relu", padding="same"),
tf.keras.layers.MaxPool2D(( 2,2)) ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation="sigmoid"),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(512, activation="sigmoid"),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Dense(256, activation="sigmoid"),
tf.keras.layers.Dropout(0.1),
tf.keras.layers.Dense(10, activation="sigmoid")
 ])
 model.compile(
 optimizer="adam", loss = 'categorical_crossentropy', metrics = ['accuracy'] 
)
 return model
model = create_model()
train=pd.read_csv('.. /input/titanic/train.csv' )<count_missing_values>
history = model.fit_generator(train_generator, epochs=60, validation_data=validation_generator, callbacks=[reduce_lr,checkpoint], verbose=1 )
<count_values><EOS> 
df = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" ).astype("float32")/ 255.0
res = tf.keras.backend.argmax(model.predict(df))
csv = pd.DataFrame({'ImageId': range(1, len(res)+ 1), "Label": res})
 csv.to_csv('submission.csv', index=False )