阿里云从哪里建设网站,企业设计网站公司排名,行业电子网站建设,涿州网站制作博文转载请注明作者和出处#xff08;作者#xff1a;finallyliuyu #xff1a;出处博客园#xff09; 附#xff1a;《卡方特征词选择算法》 《DF特征词选择算法》 一.数学背景 将数学知识、数学理论以及数学思想迁移到实际工程问题中#xff0c;经常会促进工程问题的圆…博文转载请注明作者和出处作者finallyliuyu 出处博客园 附《卡方特征词选择算法》 《DF特征词选择算法》 一.数学背景 将数学知识、数学理论以及数学思想迁移到实际工程问题中经常会促进工程问题的圆满解决。 可是如何将数学知识引入工程问题中呢首先需要有“数学思维”例如理解数学公式所刻画的内涵其次需要有“建模”能力从不同的视角来看待同一个问题最后抽象出的数学模型也可能会差别很大。比如有的数学模型有现成的解决方案可用然而有的数学模型没有现成的解决方案可用或者有的模型比其他模型能更好地刻画和表示实际问题。如果把在头脑中搜索适合待解决实际问题的数学工具定义为“数学思维”把将实际问题抽象成数学模型的能力定义为”数学建模”的话,只有这两种能力配合“默契”才能更好地解决实际问题。或许我们从文本分类问题中的各种特征词选择方法能够看到些端倪。 首先先来看一些公式的含义 注意上面的互信息公式有些错误做如下更正 平均互信息或叫做信息增益 下面开开始介绍平均互信息与互信息在文本分类特征词选择算法中的应用 为了避免引起混淆互信息在文本分类特征词选择中被称为point-wise MI平均互信息被称作IG(Information Gain)。 参照 Manning《信息检索导论》p189页王斌译作的定义 IG 公式为 point-wise MI公式为 二.下面给出实现这两种算法的代码C代码 point-wise MI 声明代码 double CalPointWiseMI(double N11,double N10,double N01,double N00);//计算pointwise MI;vectorpairstring,double PointWiseMIFeatureSelectionForPerclass(DICTIONARY mymap,CONTINGENCY contingencyTable,string classLabel);void PointWiseMIFeatureSelection(vectorstring classLabels,DICTIONARY mymap,CONTINGENCY contingencyTable,int N,char * address); 计算pointwiseMI的值 /************************************************************************//* 计算pointwiseMI的值 *//************************************************************************/double Preprocess:: CalPointWiseMI(double N11,double N10,double N01,double N00){ double pointwiseMI0; if(N110) { pointwiseMIlog(N11N10N01N00)log(N11)-log(N11N10)-log(N11N01); } return pointwiseMI;} 计算每个词对每个类别的pointwiseMI /************************************************************************//* 计算每个词对每个类别的pointwiseMI *//************************************************************************/vectorpairstring,double Preprocess:: PointWiseMIFeatureSelectionForPerclass(DICTIONARY mymap,CONTINGENCY contingencyTable,string classLabel){ int NendIndex-beginIndex1;//总共的文章数目 vectorstringtempvector;//词袋子中的所有词 vectorpairstring,double MIInfo; for(mapstring,vectorpairint,int::iterator itmymap.begin();it!mymap.end();it) { tempvector.push_back(it-first); } //计算卡方值 for(vectorstring::iterator ittmptempvector.begin();ittmp!tempvector.end();ittmp) { int N1mymap[*ittmp].size(); pairstring,string compoundKeymake_pair(*ittmp,classLabel); double N11double(contingencyTable[compoundKey].first); double N01double(contingencyTable[compoundKey].second); double N10double(N1-N11); double N00double(N-N1-N01); double miValueCalPointWiseMI(N11,N10,N01,N00); MIInfo.push_back(make_pair(*ittmp,miValue)); } //按照卡方值从大到小将这些词排列起来 stable_sort(MIInfo.begin(), MIInfo.end(),isLarger); return MIInfo;} point-wise MI特征词选择法 /************************************************************************//* point-wise MI特征词选择法 *//************************************************************************/void Preprocess:: PointWiseMIFeatureSelection(vectorstring classLabels,DICTIONARY mymap,CONTINGENCY contingencyTable,int N,char * address){ clock_t start,finish; double totaltime; int totalTraingingCorpusendIndex-beginIndex1;//训练语料库总共的文章数目 setstringfinalKeywords;//存放最终遴选出的特征词 vectorpairstring,doubleMIInfo; startclock(); for(vectorstring::iterator itclassLabels.begin();it!classLabels.end();it) { //训练语料库中某个类别的文章数目 int N_subClassCntgetCategorizationNum(*it,TrainingCorpus); //threshold决定每个类别遴选多少个特征词 int thresholdN_subClassCnt*N/totalTraingingCorpus; MIInfoPointWiseMIFeatureSelectionForPerclass(mymap,contingencyTable,*it); for(vectorpairstring,double ::size_type j0;jthreshold;j) { finalKeywords.insert(MIInfo[j].first); } MIInfo.clear(); } ofstream outfile(address); int finalKeyWordsCountfinalKeywords.size(); for (setstring::iterator itfinalKeywords.begin();it!finalKeywords.end();it) { outfile*itendl; } outfile.close(); cout最后共选择特征词finalKeyWordsCountendl; finishclock(); totaltime(double)(finish-start)/CLOCKS_PER_SEC; cout遴选特征词共有了totaltimeendl;} 信息增益特征词选择算法 double CalInformationGain(double N11,double N10,double N01,double N00);//计算IG值vectorpairstring,double InformationGainFeatureSelectionForPerclass(DICTIONARY mymap,CONTINGENCY contingencyTable,string classLabel);void InformationGainFeatureSelection(vectorstring classLabels,DICTIONARY mymap,CONTINGENCY contingencyTable,int N,char * address); 计算IG值 /************************************************************************//* 计算IG值 *//************************************************************************/double Preprocess::CalInformationGain(double N11,double N10,double N01,double N00){ double IG0; double NtotalN11N10N01N00; double N1_N11N10; double N0_N01N00; double N_0N10N00; double N_1N11N01; if(N110) { IGN11/Ntotal*log(Ntotal*N11/(N1_*N_1)); } if(N100) { IGN10/Ntotal*log(Ntotal*N10/(N1_*N_0)); } if(N010) { IGN01/Ntotal*log(Ntotal*N01/(N0_*N_1)); } if(N000) { IGN00/Ntotal*log(Ntotal*N00/(N0_*N_0)); } return IG;} 计算每个单词对于某个类别的IG值并排序 ************************************************************************//* 计算每个单词对于某个类别的IG值并排序 *//************************************************************************/vectorpairstring,double Preprocess:: InformationGainFeatureSelectionForPerclass(DICTIONARY mymap,CONTINGENCY contingencyTable,string classLabel){ int NendIndex-beginIndex1;//总共的文章数目 vectorstringtempvector;//词袋子中的所有词 vectorpairstring,double IGInfo; for(mapstring,vectorpairint,int::iterator itmymap.begin();it!mymap.end();it) { tempvector.push_back(it-first); } //计算卡方值 for(vectorstring::iterator ittmptempvector.begin();ittmp!tempvector.end();ittmp) { int N1mymap[*ittmp].size(); pairstring,string compoundKeymake_pair(*ittmp,classLabel); double N11double(contingencyTable[compoundKey].first); double N01double(contingencyTable[compoundKey].second); double N10double(N1-N11); double N00double(N-N1-N01); //double chiValueCalChiSquareValue(N11,N10,N01,N00); double igValueCalInformationGain(N11,N10,N01,N00); IGInfo.push_back(make_pair(*ittmp,igValue)); } //按照卡方值从大到小将这些词排列起来 stable_sort(IGInfo.begin(),IGInfo.end(),isLarger); return IGInfo;} 信息增益特征词选择算法 /************************************************************************//* 信息增益特征词选择算法 *//************************************************************************/void Preprocess::InformationGainFeatureSelection(vectorstring classLabels,DICTIONARY mymap,CONTINGENCY contingencyTable,int N,char * address){ clock_t start,finish; double totaltime; int totalTraingingCorpusendIndex-beginIndex1;//训练语料库总共的文章数目 setstringfinalKeywords;//存放最终遴选出的特征词 vectorpairstring,doubleIGInfo; startclock(); for(vectorstring::iterator itclassLabels.begin();it!classLabels.end();it) { //训练语料库中某个类别的文章数目 int N_subClassCntgetCategorizationNum(*it,TrainingCorpus); //threshold决定每个类别遴选多少个特征词 int thresholdN_subClassCnt*N/totalTraingingCorpus; IGInfoInformationGainFeatureSelectionForPerclass(mymap,contingencyTable,*it); for(vectorpairstring,double ::size_type j0;jthreshold;j) { finalKeywords.insert(IGInfo[j].first); } IGInfo.clear(); } ofstream outfile(address); int finalKeyWordsCountfinalKeywords.size(); for (setstring::iterator itfinalKeywords.begin();it!finalKeywords.end();it) { outfile*itendl; } outfile.close(); cout最后共选择特征词finalKeyWordsCountendl; finishclock(); totaltime(double)(finish-start)/CLOCKS_PER_SEC; cout遴选特征词共有了totaltimeendl;} 附 DF特征词选择算法转载于:https://www.cnblogs.com/finallyliuyu/archive/2010/10/04/1841820.html