Last active
September 25, 2021 17:06
-
-
Save danemacaulay/c8e3194b63570de1cf88f431ade32107 to your computer and use it in GitHub Desktop.
adding additional features to a bag of words classifier from https://stackoverflow.com/questions/30653642/combining-bag-of-words-and-other-features-in-one-model-using-sklearn-and-pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.feature_extraction.text import CountVectorizer | |
import numpy as np | |
import pandas as pd | |
import scipy as sp | |
posts = pd.read_csv('posts.csv') | |
# Create vectorizer for function to use | |
vectorizer = CountVectorizer(binary=False) | |
y = posts["score"].values.astype(np.float32) | |
X = sp.sparse.hstack((vectorizer.fit_transform(posts.message), | |
posts[['feature_1', 'feature_2']].values), | |
format='csr') | |
X_columns = vectorizer.get_feature_names() + | |
posts[['feature_1', 'feature_2']].columns.tolist() | |
print(posts) | |
print(X_columns) | |
print(X.toarray()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ID | message | feature_1 | feature_2 | score | |
---|---|---|---|---|---|
1 | 'This is the text' | 4 | 7 | 10 | |
2 | 'This is more text' | 3 | 2 | 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do I need to apply this function for test set as well?
or just pass the X_test to predict function?