Python, SelectPercentile.fit() returns 异常矩阵和错误,但是为什么呢?
In Python, SelectPercentile.fit() returns unusual matrix and error,but Why?
我是机器学习的新手,正在构建一个文本分类系统。我的文本是疾病的症状,标签是疾病的名称。我已经对所有文本进行了预处理并计算了 tf-idf values.As 我的训练集和测试集很大,所以我没有提到任何人需要的内容 here.If,我也可以包括 later.Here是labels_train,训练集和测试集的代码和输出tf-idf矩阵:
labels_train = ['Heart Attack', 'Heart Failure', 'Asthma', 'High Blood Pressure', 'Dengue Fever', 'Lung Cancer']
代码:
from sklearn import cross_validation
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_selection import SelectPercentile, f_classif
vectorizer=TfidfVectorizer(sublinear_tf=True,max_df=0.5,stop_words='english')
features_train_transformed = vectorizer.fit_transform(features_train).toarray()
print "features_train: ",features_train_transformed
features_test_transformed = vectorizer.transform(features_test)
print "features_test: ",features_test_transformed
输出:
features_train: [[ 0. 0.09420201 0. ..., 0. 0. 0.06521722]
[ 0. 0. 0. ..., 0. 0. 0. ]
[ 0. 0. 0. ..., 0. 0. 0. ]
[ 0. 0. 0. ..., 0.185467 0.08837602
0.06118382]
[ 0.11319809 0. 0.11319809 ..., 0. 0. 0.07836844]
[ 0. 0. 0. ..., 0. 0. 0. ]]
features_test: [[ 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.50792975 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.16756124 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.24203125 0. 0.
0. 0. 0. 0. 0. 0.19846908
0. 0. 0.40979452 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.19846908 0. 0. 0. 0. 0.
0.19846908 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.24203125
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.19846908 0. 0. 0.
0. 0. 0. 0. 0. 0.16756124
0. 0. 0. 0. 0. 0.
0.24203125 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.24203125 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.19846908 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0.16756124 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0.24203125 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. ]]
问题是,当我想要 select 特征时,我得到了一个零矩阵。
代码:
selector = SelectPercentile(f_classif, percentile=5)
selector.fit(features_train_transformed, labels_train)
输出:
0 [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0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]
SelectPercentile(percentile=5,
score_func=<function f_classif at 0x00000000053F6278>)
我不明白为什么我会收到这个。我怎样才能摆脱它或可能的错误在哪里?
我找到了解决方案
这里唯一的问题是只有几个样本,不足以计算出方差分析(方差分析),因为方差分析不能处理零方差特征。我应该只增加火车数据集的数量。我从这些链接中找到了详细信息:
https://www.quora.com/How-is-ANOVA-used-for-feature-selection
我是机器学习的新手,正在构建一个文本分类系统。我的文本是疾病的症状,标签是疾病的名称。我已经对所有文本进行了预处理并计算了 tf-idf values.As 我的训练集和测试集很大,所以我没有提到任何人需要的内容 here.If,我也可以包括 later.Here是labels_train,训练集和测试集的代码和输出tf-idf矩阵:
labels_train = ['Heart Attack', 'Heart Failure', 'Asthma', 'High Blood Pressure', 'Dengue Fever', 'Lung Cancer']
代码:
from sklearn import cross_validation
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_selection import SelectPercentile, f_classif
vectorizer=TfidfVectorizer(sublinear_tf=True,max_df=0.5,stop_words='english')
features_train_transformed = vectorizer.fit_transform(features_train).toarray()
print "features_train: ",features_train_transformed
features_test_transformed = vectorizer.transform(features_test)
print "features_test: ",features_test_transformed
输出:
features_train: [[ 0. 0.09420201 0. ..., 0. 0. 0.06521722]
[ 0. 0. 0. ..., 0. 0. 0. ]
[ 0. 0. 0. ..., 0. 0. 0. ]
[ 0. 0. 0. ..., 0.185467 0.08837602
0.06118382]
[ 0.11319809 0. 0.11319809 ..., 0. 0. 0.07836844]
[ 0. 0. 0. ..., 0. 0. 0. ]]
features_test: [[ 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.50792975 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.16756124 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.24203125 0. 0.
0. 0. 0. 0. 0. 0.19846908
0. 0. 0.40979452 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.
0.19846908 0. 0. 0. 0. 0.
0.19846908 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0.24203125
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0.19846908 0. 0. 0.
0. 0. 0. 0. 0. 0.16756124
0. 0. 0. 0. 0. 0.
0.24203125 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0.24203125 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0.19846908 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0.16756124 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0.24203125 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0.
0. ]]
问题是,当我想要 select 特征时,我得到了一个零矩阵。
代码:
selector = SelectPercentile(f_classif, percentile=5)
selector.fit(features_train_transformed, labels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0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0.]
SelectPercentile(percentile=5,
score_func=<function f_classif at 0x00000000053F6278>)
我不明白为什么我会收到这个。我怎样才能摆脱它或可能的错误在哪里?
我找到了解决方案
这里唯一的问题是只有几个样本,不足以计算出方差分析(方差分析),因为方差分析不能处理零方差特征。我应该只增加火车数据集的数量。我从这些链接中找到了详细信息: https://www.quora.com/How-is-ANOVA-used-for-feature-selection