Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding pandas columns to a sparse matrix

I have additional derived values for X variables that I want to use in my model.

XAll = pd_data[['title','wordcount','sumscores','length']]
y = pd_data['sentiment']
X_train, X_test, y_train, y_test = train_test_split(XAll, y, random_state=1)

As I am working with text data in title, I first convert it to a dtm separately:

vect = CountVectorizer(max_df=0.5)
vect.fit(X_train['title'])
X_train_dtm = vect.transform(X_train['title'])
column_index = X_train_dtm.indices

print(type(X_train_dtm))    # This is <class 'scipy.sparse.csr.csr_matrix'>
print("X_train_dtm shape",X_train_dtm.get_shape())  # This is (856, 2016)
print("column index:",column_index)     # This is column index: [ 533  754  859 ...,  633  950 1339]

Now that I have the text as a document term matrix, I would like to add the other features like 'wordcount','sumscores','length' to X_train_dtm which are numeric. This I shall create the model using the new dtm and thus would be more accurate as I would have inserted additinal features.

How do I add additional numeric columns of the pandas dataframe to a sparse csr matrix?

like image 528
Bonson Avatar asked Jan 30 '17 01:01

Bonson


1 Answers

Found the solution. We can do this using sparse.hstack:

from scipy.sparse import hstack
X_train_dtm = hstack((X_train_dtm,np.array(X_train['wordcount'])[:,None]))
like image 129
Bonson Avatar answered Oct 06 '22 00:10

Bonson