我有一个代码可以向矢量化数据集添加新的非文本功能(文档的长度):
from sklearn.feature_extraction.text import CountVectorizer
from scipy.sparse import csr_matrix, hstack
def add_feature(X, feature_to_add):
return hstack([X, csr_matrix(feature_to_add).T], 'csr')
vect = CountVectorizer().fit(X_train)
X_train_vectorized = vect.transform(X_train)
X_train1 = add_feature(X_train_vectorized, X_train.str.len())
我想将此新功能称为' length_of_doc'。怎么做? 谢谢。