Q:

NLP - Bag of words classification

The issue:
I am confused as to why we transform our test data using the CountVectorizer fitted on our train data for bag of words classification.
Why would we not create a new CountVectorizer and fit the test data to this and have the classifier predict on the test CountVectorizer?
Looking here: How to standardize the bag of words for train and test?
Ripped from the answer:
LabeledWords=pd.DataFrame(columns=['word','label'])

LabeledWords.append({'word':'Church','label':'Religion'} )

vectorizer = CountVectorizer()

Xtrain,yTrain=vectorizer.fit_transform(LabeledWords['word']).toarray(),vectorizer.fit_transform(LabeledWords['label']).toarray()
forest = RandomForestClassifier(n_estimators = 100) 
clf=forest.fit(Xtrain,yTrain)

for each_word,label in Preprocessed_list:
    test_featuresX.append(vectorizer.transform(each_word),toarray())
    test_featuresY.append(label.toarray())

clf.score(test_featuresX,test_featuresY) 

We can see the user created a CountVectorizer and fit it to the training data. Then fit the classifier to this CountVectorizer. Afterwards the user just transformed the test data using the CountVectorizer which was fit to the train data, and fed this into the classifier. Why is that?
What I am trying to accomplish:
I am trying to implement bag of visual words. It uses the same concept, but I am unsure how should create my train and test sets for classification.

A:

You want your test data to go through the same pipeline as your train data to make the difference between test and train as similar as possible to the difference between the real world and your model. The whole point to having test and train splits in your data is to help validate that your model generalizes, and allowing extra data from the test set to leak into the model trained on your train set prevents you from getting an accurate picture of that generalization ability.
Also, as @Juanpa.arrivillaga said, text processing opens cans of worms on top of the standard rules of thumb for predictive analytics. By using two different count vectorizers, you would have a model (in this case a random forest) trained to expect the first coordinate to correspond to some word like "apple" and then feed it some word like "grape." Any success you might have in such a scenario would be purely accidental.

