解决TF-IDE中Reshape your data either using array.reshape(-1, 1) if your data has a single feature

解决TF-IDE中Reshape your data either using array.reshape(-1, 1) if your data has a single feature

看出错的代码:

# ——创建时间:2019.3.15——
# 文本特征表示
from sklearn.feature_extraction.text import CountVectorizer

sample = [
    'feature engineering',
    'feature selection',
    'feature extraction'
]

# 使用词频—逆文档进行文本特征表示
from sklearn.feature_extraction.text import TfidfTransformer
import numpy as np

vec1 = TfidfTransformer()
x = vec1.fit_transform(array)
X.toarray()  # 转换成稀疏矩阵
print(x)

看来IF-IDE无法直接转换文档??那么先用CountVectorizer进行文档-数组的转黄,成功解决问题!

看代码:

# ——创建时间:2019.3.15——
# 文本特征表示
from sklearn.feature_extraction.text import CountVectorizer

sample = [
    'feature engineering',
    'feature selection',
    'feature extraction'
]
vec = CountVectorizer()
x = vec.fit_transform(sample)
array = x.toarray()  # 转换成稀疏矩阵

# 使用词频—逆文档进行文本特征表示
from sklearn.feature_extraction.text import TfidfTransformer
import numpy as np

vec1 = TfidfTransformer()
# sample(type=np.float32)
x = vec1.fit_transform(array)
# X.toarray()  # 转换成稀疏矩阵
print(x)

 

你可能感兴趣的:(机器学习)