Kaggle competition: COVID19 Global Forecasting (Week 1)
train_ds = ArrayDataset(train_90_data,train_90_labels)
 valid_ds = ArrayDataset(valid_10_data,valid_10_labels)
 other_ds = ArrayDataset(other_data, other_labels)
 test_ds = ArrayDataset(test_data, test_labels )<train_model>
import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt
import plotly.graph_objects as go
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import numpy as np
from sklearn.metrics import mean_squared_log_error
from sklearn.tree import DecisionTreeRegressor
bs = 256
databunch = DataBunch.create(train_ds, valid_ds, test_ds=test_ds, bs=bs )<concatenate>
train_df=pd.read_csv(".. /input/covid19-global-forecasting-week-1/train.csv")
 test_df=pd.read_csv(".. /input/covid19-global-forecasting-week-1/test.csv" )
def conv2(ni,nf,stride=2,ks=5):
return conv_layer(ni,nf,stride=stride,ks=ks )<choose_model_class>
train_df[train_df["Country/Region"]=="China"]
best_architecture = nn.Sequential(
 conv2(1,32,stride=1,ks=5),
conv2(32,32,stride=1,ks=5),
conv2(32,32,stride=2,ks=8),
nn.Dropout(0.4),
conv2(32,64,stride=1,ks=5),
conv2(64,64,stride=1,ks=5),
conv2(64,64,stride=2,ks=5),
nn.Dropout(0.4),
Flatten() ,
nn.Linear(3136, 256),
relu(inplace=True),
nn.BatchNorm1d(256),
nn.Dropout(0.4),
nn.Linear(256,10)
 )<choose_model_class>
temp_Confirmed = train_df.loc[train_df["Date"]==train_df["Date"][len(train_df)-1]].groupby(['Country/Region'])["ConfirmedCases"].sum().reset_index()
temp_Confirmed.head()
learn = Learner(databunch, best_architecture, loss_func = nn.CrossEntropyLoss() , metrics=[accuracy] )<train_model>
Country_Confirmed=pd.DataFrame()
Country_Confirmed["Name"]=temp_Confirmed["Country/Region"]
Country_Confirmed["Confirmed"]=temp_Confirmed["ConfirmedCases"]
Country_Confirmed.head()
learn.fit_one_cycle(42 )<predict_on_test>
temp_Fatalities = train_df.loc[train_df["Date"]==train_df["Date"][len(train_df)-1]].groupby(['Country/Region'])["Fatalities"].sum().reset_index()
temp_Fatalities.head()
preds, ids = learn.get_preds(DatasetType.Test)
 y = torch.argmax(preds, dim=1 )<save_to_csv>
Country_Fatalities=pd.DataFrame()
Country_Fatalities["Name"]=temp_Fatalities["Country/Region"]
Country_Fatalities["Fatalities"]=temp_Fatalities["Fatalities"]
Country_Fatalities.head()
submission = pd.DataFrame({'id': ids, 'label': y})
 submission.to_csv(path_or_buf="submission.csv", index=False )<import_modules>
China=train_df[train_df["Country/Region"]=="China"].groupby(["Date"])["ConfirmedCases"].sum().reset_index()
China['Date']=pd.to_datetime(China['Date'] )
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.models import load_model
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder<load_from_csv>
China_Fatalities=train_df[train_df["Country/Region"]=="China"].groupby(["Date"])["Fatalities"].sum().reset_index()
China_Fatalities['Date']=pd.to_datetime(China_Fatalities['Date'] )
train_set = ".. /input/Kannada-MNIST/train.csv"
valid_set = ".. /input/Kannada-MNIST/Dig-MNIST.csv"
test_set = ".. /input/Kannada-MNIST/test.csv"
train = pd.read_csv(train_set)
 valid = pd.read_csv(valid_set)
 test = pd.read_csv(test_set)
 xtrain = train.drop('label', axis=1)
 ytrain = train.label
xtest = test.drop('id', axis=1)
 xval = valid.drop('label', axis=1)
 yval = valid.label
xtrain = xtrain.values
ytrain = ytrain.values
xval = xval.values
yval = yval.values
xtest = xtest.values
xtrain = xtrain.reshape(-1, 28, 28, 1 ).astype('float32')
 xval = xval.reshape(-1, 28, 28, 1 ).astype('float32')
 xtest = xtest.reshape(-1, 28, 28, 1 ).astype('float32')
 xtrain =(xtrain - np.mean(xtrain)) /255
xval =(xval - np.mean(xval)) /255
xtest =(xtest - np.mean(xtest)) /255<choose_model_class>
Italy=train_df[train_df["Country/Region"]=="Italy"].groupby(["Date"])["ConfirmedCases"].sum().reset_index()
Italy['Date']=pd.to_datetime(Italy['Date'] )
def build_model() :
inputs = tf.keras.Input(shape=(28,28,1))
x1 = tf.keras.layers.Conv2D(64,(3, 3), padding='same' )(inputs)
 x1 = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform" )(x1)
 x1 = tf.keras.layers.LeakyReLU(alpha=0.1 )(x1)
 x2 = tf.keras.layers.Conv2D(64,(3, 3), padding='same' )(x1)
 x2 = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform" )(x2)
 x2 = tf.keras.layers.LeakyReLU(alpha=0.1 )(x2)
 x3 = tf.keras.layers.Conv2D(64,(3, 3), padding='same' )(x2)
 x3 = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform" )(x3)
 x3 = tf.keras.layers.LeakyReLU(alpha=0.1 )(x3)
 x4 = tf.keras.layers.Average()([x1, x2, x3])
 x4 = tf.keras.layers.MaxPooling2D(( 2, 2))(x4)
 x4 = tf.keras.layers.Dropout(0.25 )(x4)
 x5 = tf.keras.layers.Conv2D(128,(3, 3), padding='same' )(x4)
 x5 = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform" )(x5)
 x5 = tf.keras.layers.LeakyReLU(alpha=0.1 )(x5)
 x6 = tf.keras.layers.Conv2D(128,(3, 3), padding='same' )(x5)
 x6 = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform" )(x6)
 x6 = tf.keras.layers.LeakyReLU(alpha=0.1 )(x6)
 x7 = tf.keras.layers.Conv2D(128,(3, 3), padding='same' )(x6)
 x7 = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform" )(x7)
 x7 = tf.keras.layers.LeakyReLU(alpha=0.1 )(x7)
 x8 = tf.keras.layers.Average()([x5, x6, x7])
 x8 = tf.keras.layers.MaxPooling2D(( 2, 2))(x8)
 x8 = tf.keras.layers.Dropout(0.25 )(x8)
 x9 = tf.keras.layers.Conv2D(256,(3, 3), padding='same' )(x8)
 x9 = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform" )(x9)
 x9 = tf.keras.layers.LeakyReLU(alpha=0.1 )(x9)
 x10 = tf.keras.layers.Conv2D(256,(3, 3), padding='same' )(x9)
 x10 = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform" )(x10)
 x10 = tf.keras.layers.LeakyReLU(alpha=0.1 )(x10)
 x11 = tf.keras.layers.Conv2D(256,(3, 3), padding='same' )(x10)
 x11 = tf.keras.layers.BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform" )(x11)
 x11 = tf.keras.layers.LeakyReLU(alpha=0.1 )(x11)
 x12 = tf.keras.layers.Average()([x9, x10, x11])
 x12 = tf.keras.layers.MaxPooling2D(( 2, 2))(x12)
 x12 = tf.keras.layers.Dropout(0.25 )(x12)
 x = tf.keras.layers.Flatten()(x12)
 x = tf.keras.layers.Dense(256, activation=tf.nn.relu )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dense(128, activation=tf.nn.relu )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 x = tf.keras.layers.Dense(64, activation=tf.nn.relu )(x)
 x = tf.keras.layers.BatchNormalization()(x)
 output = tf.keras.layers.Dense(10, activation=tf.nn.softmax )(x)
 model = tf.keras.Model(inputs=inputs, outputs=output)
 model.summary()
return model<choose_model_class>
Italy_Fatalities=train_df[train_df["Country/Region"]=="Italy"].groupby(["Date"])["Fatalities"].sum().reset_index()
Italy_Fatalities['Date']=pd.to_datetime(Italy_Fatalities['Date'] )
opt = tf.keras.optimizers.RMSprop(learning_rate=0.002, rho=0.9)
 model = build_model()
model.compile(optimizer=opt,loss='sparse_categorical_crossentropy',metrics=['accuracy'] )<train_model>
Other_countries=train_df[~train_df["Country/Region"].isin(['China','Italy'])]
Other_countries['Country/Region']= 'Others'
Other_countries=Other_countries.groupby(["Date"])["ConfirmedCases"].sum().reset_index()
Other_countries['Date']=pd.to_datetime(Other_countries['Date'] )
data_generator = tf.keras.preprocessing.image.ImageDataGenerator(
 rotation_range=12,
zoom_range=0.35,
width_shift_range=0.3,
height_shift_range=0.3)
 data_generator.fit(xtrain)
 learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(
 monitor='loss',
factor=0.2,
patience=2,
verbose=2,
mode="min",
min_delta=0.0001,
cooldown=0,
min_lr=0.0000001)
 es = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=300, restore_best_weights=True )<train_model>
Other_Fatalities=train_df[~train_df["Country/Region"].isin(["China","Italy"])]
Other_Fatalities['Country/Region']= 'Others'
Other_Fatalities=Other_Fatalities.groupby(["Date"])["Fatalities"].sum().reset_index()
Other_Fatalities['Date']=pd.to_datetime(Other_Fatalities['Date'] )
model.fit_generator(data_generator.flow(xtrain, ytrain, batch_size=1024),
steps_per_epoch=len(xtrain)//1024,
epochs=500,
validation_data=(np.array(xval), np.array(yval)) ,
validation_steps=50,
callbacks=[learning_rate_reduction, es] )<save_to_csv>
train_df.isnull().sum()
preds = model.predict(xtest)
 preds = preds.argmax(axis=1)
 print(preds)
 preds = preds.astype(int ).flatten()
preds =(LabelEncoder().fit_transform(( preds)))
 preds = pd.DataFrame({'label': preds})
 sub = pd.DataFrame(data=test.id)
 sub = sub.join(preds)
 sub.to_csv('submission.csv', index=False )<load_from_csv>
X=train_df[["Date","Lat","Long"]]
X["Date"]=X["Date"].apply(lambda x:x.replace("-",""))
X["Date"] = X["Date"].astype(int )
train = pd.read_csv('/kaggle/input/Kannada-MNIST/train.csv')
 test = pd.read_csv('/kaggle/input/Kannada-MNIST/test.csv')
 X = train.drop('label',axis=1)
 y = train.label
X = X.values.astype('float32')/ 255
X = X.reshape(-1, 28, 28,1)
 y = to_categorical(y)
 X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.15, random_state=1)
 def group_layers(model, filters, kernel, count):
for i in range(count):
model.add(Conv2D(filters, kernel, padding='same'))
model.add(BatchNormalization(momentum=0.9, epsilon=1e-5))
model.add(ReLU())
 custom_model = Sequential()
custom_model.add(Conv2D(32,(3, 3), padding='same', input_shape=(28, 28, 1)))
 custom_model.add(BatchNormalization(momentum=0.9, epsilon=1e-5, gamma_initializer="uniform"))
custom_model.add(ReLU())
 group_layers(custom_model, 32,(3,3), 2)
 custom_model.add(AveragePooling2D(strides=2))
custom_model.add(Dropout(0.25))
group_layers(custom_model, 64,(3,3), 2)
 custom_model.add(AveragePooling2D(strides=2))
custom_model.add(Dropout(0.25))
custom_model.add(Flatten())
 custom_model.add(Dense(64))
custom_model.add(ReLU())
 custom_model.add(BatchNormalization())
 custom_model.add(Dense(10, activation='softmax'))
custom_model.compile(loss='categorical_crossentropy', optimizer='Adam', metrics=['accuracy'])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience=3,
verbose=1,
factor=0.2,
min_lr=1e-6)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=300, restore_best_weights=True)
 custom_model.fit(X_train, y_train, batch_size=64,
epochs=60,
validation_data=(X_valid, y_valid),
callbacks=[learning_rate_reduction, es],
verbose=2)
 test = test.drop('id',axis=1)
 X_test = test.values.astype('float32')/ 255
X_test = X_test.reshape(-1, 28, 28,1)
 sample_sub=pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 sub = custom_model.predict(X_test)
 sub = np.argmax(sub, axis=1)
 sample_sub['label']= sub
sample_sub.to_csv('submission.csv',index=False )<import_modules>
y1=train_df["ConfirmedCases"]
y2=train_df["Fatalities"]
import matplotlib.pyplot as plt,seaborn as sns,pandas as pd,numpy as np
from keras.models import Sequential, load_model
from keras.layers.core import Dense, Dropout, Activation
from keras.layers import Conv2D, MaxPooling2D,MaxPool2D,Flatten,BatchNormalization
from keras.utils import np_utils
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras.optimizers import Adam
import tensorflow as tf
from sklearn.model_selection import train_test_split<load_from_csv>
X_train_confirmed, X_test_confirmed, y_train_confirmed, y_test_confirmed = train_test_split(X, y1, test_size =.20, random_state = 42 )
train_data = pd.read_csv('.. /input/Kannada-MNIST/train.csv' )<prepare_x_and_y>
dt1=DecisionTreeRegressor()
dt1.fit(X_train_confirmed, y_train_confirmed)
 y_pred_dt_confirmed=dt1.predict(X_test_confirmed )
x_train = train_data.drop(labels = ["label"],axis = 1)
 y_train = train_data["label"]<data_type_conversions>
np.sqrt(mean_squared_log_error(y_test_confirmed, y_pred_dt_confirmed))
x_train = x_train.values.astype('float32')/ 255<split>
rfc1=RandomForestRegressor(n_estimators=5, min_samples_split=3, random_state=42)
 rfc1.fit(X_train_confirmed, y_train_confirmed )
x_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size = 0.2, random_state=42 )<train_model>
y_pred_rfc_confirmed=rfc1.predict(X_test_confirmed )
X_train = x_train.reshape(-1, 28, 28,1)
 X_test = x_test.reshape(-1, 28, 28,1)
 print("Train matrix shape", X_train.shape)
 print("Test matrix shape", X_test.shape )<categorify>
np.sqrt(mean_squared_log_error(y_test_confirmed, y_pred_rfc_confirmed))
n_classes = 10
print("Shape before one-hot encoding: ", y_train.shape)
 Y_train = np_utils.to_categorical(y_train, n_classes)
 Y_test = np_utils.to_categorical(y_test, n_classes)
 print("Shape after one-hot encoding: ", Y_train.shape )<choose_model_class>
X_train_fatal, X_test_fatal, y_train_fatal, y_test_fatal = train_test_split(X, y2, test_size =.20, random_state = 42 )
model = Sequential()
model.add(Conv2D(32, kernel_size = 3, activation='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 3, activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size = 5, strides=2, padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.4))
model.add(Conv2D(128, kernel_size = 4, activation='relu'))
model.add(BatchNormalization())
 model.add(Flatten())
 model.add(Dropout(0.4))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')
 adam = Adam(lr=5e-4)
 model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer=adam )<choose_model_class>
dt2=DecisionTreeRegressor()
reduce_lr = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.2,
min_lr=1e-6 )<train_model>
dt2.fit(X_train_fatal, y_train_fatal )
datagen = ImageDataGenerator(
 rotation_range=10,
width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1)
 history = datagen.fit(X_train )<train_model>
y_pred_dt_fatal=dt2.predict(X_test_fatal )
history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=100), steps_per_epoch=len(X_train)/100,
epochs=20, validation_data=(X_test, Y_test), callbacks=[reduce_lr] )<compute_test_metric>
np.sqrt(mean_squared_log_error(y_test_fatal, y_pred_dt_fatal))
score = model.evaluate(X_test, Y_test, verbose=0)
 print('Test loss:', score[0])
 print('Test accuracy:', score[1] )<load_from_csv>
rfc2=RandomForestRegressor(n_estimators=5, min_samples_split=3, random_state=42)
 rfc2.fit(X_train_fatal, y_train_fatal )
test_data = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 id_ = test_data.id
test_data = test_data.drop("id",axis="columns")
 test_data = test_data.values.reshape(-1, 28, 28,1)
 test_data = test_data.astype('float32')
 test_data /= 255
print("Test data matrix shape", test_data.shape )<predict_on_test>
y_pred_rfc_fatal=rfc2.predict(X_test_fatal )
y_pred = model.predict_classes(test_data, verbose=0)
 print(y_pred )<predict_on_test>
np.sqrt(mean_squared_log_error(y_test_fatal, y_pred_rfc_fatal))
i = 7
predicted_value = np.argmax(model.predict(X_test[i].reshape(1,28, 28,1)))
 print('predicted value:',predicted_value)
 plt.imshow(X_test[i].reshape([28, 28]), cmap='Greys_r' )<save_to_csv>
test_df.isnull().sum()
sample_sub=pd.read_csv('.. /input/Kannada-MNIST/sample_submission.csv')
 sample_sub['label']=y_pred
sample_sub.to_csv('submission.csv',index=False )<load_from_csv>
test_data=test_df[["Date","Lat","Long"]]
train = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 train.columns<categorify>
test_data["Date"]=test_data["Date"].apply(lambda x:x.replace("-",""))
test_data["Date"] = test_data["Date"].astype(int )
sample_size = 8000
np.random.seed(2018)
 idx = np.random.choice(60000, size=sample_size, replace=False)
 train_sample = train.loc[idx,'pixel0':]
label_sample = train.loc[idx,'label']
train_sample
embeddings = TSNE(verbose=2 ).fit_transform(train_sample )<prepare_x_and_y>
y_confirmed=rfc1.predict(test_data )
y_train = train['label']
y_train<count_values>
y_fatal=rfc2.predict(test_data )
train['label'].value_counts()<count_values>
submission=pd.DataFrame({'ForecastId': test_df["ForecastId"], 'ConfirmedCases': y_confirmed, 'Fatalities': y_fatal} )
g = sns.countplot(y_train)
 y_train.value_counts()<set_options>
submission["ConfirmedCases"]=submission["ConfirmedCases"].astype(int)
 submission["Fatalities"]=submission["Fatalities"].astype(int )
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )<prepare_x_and_y>
submission.to_csv('submission.csv', index=False )
X = train_data.values / 255.0
X_test = test.loc[:,'pixel0':].values / 255.0
y = train_label<split>
submission.to_csv('submission.csv', index=False )