Kaggle competition: Bag of Words Meets Bags of Popcorn
!rm *.whl
!rm *.py
!rm *.ckpt
!ls<import_modules>
train = pd.read_csv(".. /input/labeledTrainData.tsv", header=0, delimiter="\t", quoting=3 )
import numpy as np
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Activation, Dropout, Flatten, Dense, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from keras.optimizers import Adam<load_from_csv>
sw = stopwords.words('english')
 for w in ['film','see','get','make','fill']:
sw.append(w)
 sw = set(sw )
df = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 print(df.shape)
 print(test.shape)
 print(df.head() )<count_values>
def stage_one(text):
text = BeautifulSoup(text ).get_text()
reg = re.sub("[^a-zA-Z]", " ", text)
 low = reg.lower().split()
meaningful_words = [w for w in low if not w in sw]
return(" ".join(meaningful_words))
labels = df["label"]
X = df.drop('label', axis = 1)
 print(labels.value_counts())
 print("Baseline Accuracy: " + str(round(labels.value_counts().max() /labels.value_counts().sum() ,3)) )<categorify>
clean_train = []
for i in range(0, train["review"].size):
clean_train.append(stage_one(train["review"][i]))
def normalizeANDreshape(df, minimum, maximum):
diff = maximum - minimum
zero_min = df - minimum
adjusted = zero_min/diff
shaped = adjusted.values.reshape(-1,28,28,1)
 return shaped
print(np.max(normalizeANDreshape(X, 0, 255)))
 print(np.min(normalizeANDreshape(X, 0, 255)))
 print(type(normalizeANDreshape(X, 0, 255)))
 print(normalizeANDreshape(X, 0, 255 ).shape )<categorify>
vectorizer = CountVectorizer(analyzer = "word", \
tokenizer = None, \
preprocessor = None, \
stop_words = None, \
max_features = 12000, \
ngram_range=(1, 3))
features = vectorizer.fit_transform(clean_train ).toarray()
dist = np.sum(features, axis=0)
 vocab = vectorizer.get_feature_names()
forest = ExtraTreesClassifier(n_estimators = 150, n_jobs = -1)
 forest = forest.fit(features, train["sentiment"])
 test = pd.read_csv(".. /input/testData.tsv", header=0, delimiter="\t", quoting=3)
 clean_test = []
for i in range(0,len(test["review"])) :
clean_review = stage_one(test["review"][i])
 clean_test.append(clean_review)
 test_features = vectorizer.transform(clean_test)
 test_features = test_features.toarray()
result = forest.predict(test_features)
 output = pd.DataFrame(data={"id":test["id"], "sentiment":result})
 output.to_csv("n_vectorize_1_3 submit.csv", index=False, quoting=3 )