# How to train a Text Based data for a Machine Learning problem?

1. I am working on a project for displaying products to customer by context, based on a search query. For example, I don't want customers to have to enter a specific product name, instead searching based on functionality (e.g., "walls do not heat much" would return product names such as "Whirlpool NEO IC355 ROY 3S 340 L Double Door Refrigerator")
2. I have a training set comprised of the functionality associated products. I am planning to use Logistic Regression to train a model on these data. How do I process this data in Python or extract features to feed into logistic regression? I have heard of "Bag of words model", but not sure how to use this, or is it even applicable here?

I know there are plenty of NLTK libraries available. But, I want to implement it from the scratch or using minimum external libraries possible.

For your particular problem I'm not sure that using a supervised Logistic Regression approach is ideal, but I suppose that is a different and larger topic. To answer your question, yes you can use a "bag of words" representation of your text. Python's sci-kit learn library offers this functionality via both CountVectorizer and TfidfVectorizer. This will result in a sparse matrix representation of n-grams and occurrences of those n-grams in your corpus. From here you basically have 2 options, (1) training your supervised model directly on the sparse matrix or (2) reducing the dimension of your sparse matrix so it be represented as a dense matrix. Luckily sklearn offers functionality for both these, their LogisticRegression class supports sparse matrices and their TruncatedSVD implementation of PCA/LSA supports sparse matrices as well.