Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<import_modules>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import pandas as pd
import numpy as np
from sklearn.metrics import f1_score<load_from_csv>
train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 test = pd.read_csv(".. /input/digit-recognizer/test.csv")
 train.shape
train=pd.read_csv('.. /input/nlp-getting-started/train.csv')
 test=pd.read_csv('.. /input/nlp-getting-started/test.csv' )<count_values>
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1 )
train.target.value_counts()<load_pretrained>
X_train = X_train / 255.0
test = test / 255.0
nltk.download('punkt')
 nltk.download('stopwords')
 !pip install contractions
nltk.download('wordnet')
 !pip install pyspellchecker 
<string_transform>
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1 )
stop_words=nltk.corpus.stopwords.words('english')
 i=0
wnl=WordNetLemmatizer()
stemmer=SnowballStemmer('english')
 for doc in train.text:
doc=re.sub(r'https?://\S+|www\.\S+','',doc)
 doc=re.sub(r'<.*?>','',doc)
 doc=re.sub(r'[^a-zA-Z\s]','',doc,re.I|re.A)
 doc=' '.join([wnl.lemmatize(i)for i in doc.lower().split() ])
 doc=contractions.fix(doc)
 tokens=nltk.word_tokenize(doc)
 filtered=[token for token in tokens if token not in stop_words]
doc=' '.join(filtered)
 train.text[i]=doc
i+=1
i=0
for doc in test.text:
doc=re.sub(r'https?://\S+|www\.\S+','',doc)
 doc=re.sub(r'<.*?>','',doc)
 doc=re.sub(r'[^a-zA-Z\s]','',doc,re.I|re.A)
 doc=' '.join([wnl.lemmatize(i)for i in doc.lower().split() ])
 doc=contractions.fix(doc)
 tokens=nltk.word_tokenize(doc)
 filtered=[token for token in tokens if token not in stop_words]
doc=' '.join(filtered)
 test.text[i]=doc
i+=1<categorify>
from keras.utils.np_utils import to_categorical
tfidf=TfidfVectorizer(ngram_range=(1,1),use_idf=True)
 mat=tfidf.fit_transform(train.text ).toarray()
train_df=pd.DataFrame(mat,columns=tfidf.get_feature_names())
 test_df=pd.DataFrame(tfidf.transform(test.text ).toarray() ,columns=tfidf.get_feature_names())
 train_df.head()<predict_on_test>
Y_train = to_categorical(Y_train, num_classes = 10 )
model=LogisticRegression()
model.fit(train_df,train.target)
 print(f1_score(model.predict(train_df),train.target))
pred=model.predict(test_df )<save_to_csv>
from sklearn.model_selection import train_test_split
pd.DataFrame({
'id':test.id,
'target':pred
} ).to_csv('submission.csv',index=False )<import_modules>
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from nltk.corpus import stopwords
from nltk.util import ngrams
from nltk.stem import WordNetLemmatizer
import re
from textblob import TextBlob
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_score, recall_score, confusion_matrix
from sklearn.tree import DecisionTreeClassifier
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
import tensorflow as tf
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
from gensim.models import Word2Vec
from gensim.models.keyedvectors import KeyedVectors
import time
from keras.layers import Dense, Input, Flatten, Dropout
from keras.layers import Conv1D, MaxPooling1D, Embedding
from keras.models import Sequential
from keras import losses
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model<load_from_csv>
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=10 )
train_data = pd.read_csv('/kaggle/input/nlp-getting-started/train.csv')
 submit_data = pd.read_csv('/kaggle/input/nlp-getting-started/test.csv' )<count_missing_values>
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D
from keras.optimizers import RMSprop
import tensorflow as tf
train_data[train_data['text'].isna() ]<groupby>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'),
tf.keras.layers.Conv2D(64,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'),
tf.keras.layers.Conv2D(128,(3, 3), activation='relu', input_shape=(28, 28, 1),padding='same'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(128,(3, 3), activation='relu',padding='same'),
tf.keras.layers.Conv2D(192,(3, 3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(192,(3, 3), activation='relu',padding='same'),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout (.5),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
 ] )
train_data.groupby('target' ).count()<set_options>
model.compile(optimizer = keras.optimizers.RMSprop(learning_rate=0.001, rho=0.9), loss = "categorical_crossentropy", metrics=["accuracy"] )
%matplotlib inline<concatenate>
model.fit(X_train, Y_train, batch_size = 86, epochs =50, validation_data =(X_val, Y_val), verbose = 1 )
data = pd.concat([train_data, submit_data])
 data.shape<feature_engineering>
results = model.predict(test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_fourth.csv",index=False )