function [Tree RulesMatrix]=DecisionTree(DataSet,AttributName)
創(chuàng)新互聯(lián)建站長(zhǎng)期為上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為隴南企業(yè)提供專業(yè)的成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè),隴南網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
%輸入為訓(xùn)練集,為離散后的數(shù)字,如記錄1:1 1 3 2 1;
%前面為屬性列,最后一列為類標(biāo)
if nargin1
error('請(qǐng)輸入數(shù)據(jù)集');
else
if isstr(DataSet)
[DataSet AttributValue]=readdata2(DataSet);
else
AttributValue=[];
end
end
if nargin2
AttributName=[];
end
Attributs=[1:size(DataSet,2)-1];
Tree=CreatTree(DataSet,Attributs);
disp([char(13) 'The Decision Tree:']);
showTree(Tree,0,0,1,AttributValue,AttributName);
Rules=getRule(Tree);
RulesMatrix=zeros(size(Rules,1),size(DataSet,2));
for i=1:size(Rules,1)
rule=cell2struct(Rules(i,1),{'str'});
rule=str2num([rule.str([1:(find(rule.str=='C')-1)]) rule.str((find(rule.str=='C')+1):length(rule.str))]);
for j=1:(length(rule)-1)/2
RulesMatrix(i,rule((j-1)*2+1))=rule(j*2);
end
RulesMatrix(i,size(DataSet,2))=rule(length(rule));
end
end
function Tree=CreatTree(DataSet,Attributs) %決策樹程序 輸入為:數(shù)據(jù)集,屬性名列表
%disp(Attributs);
[S ValRecords]=ComputEntropy(DataSet,0);
if(S==0) %當(dāng)樣例全為一類時(shí)退出,返回葉子節(jié)點(diǎn)類標(biāo)
for i=1:length(ValRecords)
if(length(ValRecords(i).matrix)==size(DataSet,1))
break;
end
end
Tree.Attribut=i;
Tree.Child=[];
return;
end
if(length(Attributs)==0) %當(dāng)條件屬性個(gè)數(shù)為0時(shí)返回占多數(shù)的類標(biāo)
mostlabelnum=0;
mostlabel=0;
for i=1:length(ValRecords)
if(length(ValRecords(i).matrix)mostlabelnum)
mostlabelnum=length(ValRecords(i).matrix);
mostlabel=i;
end
end
Tree.Attribut=mostlabel;
Tree.Child=[];
return;
end
for i=1:length(Attributs)
[Sa(i) ValRecord]=ComputEntropy(DataSet,i);
Gains(i)=S-Sa(i);
AtrributMatric(i).val=ValRecord;
end
[maxval maxindex]=max(Gains);
Tree.Attribut=Attributs(maxindex);
Attributs2=[Attributs(1:maxindex-1) Attributs(maxindex+1:length(Attributs))];
for j=1:length(AtrributMatric(maxindex).val)
DataSet2=[DataSet(AtrributMatric(maxindex).val(j).matrix',1:maxindex-1) DataSet(AtrributMatric(maxindex).val(j).matrix',maxindex+1:size(DataSet,2))];
if(size(DataSet2,1)==0)
mostlabelnum=0;
mostlabel=0;
for i=1:length(ValRecords)
if(length(ValRecords(i).matrix)mostlabelnum)
mostlabelnum=length(ValRecords(i).matrix);
mostlabel=i;
end
end
Tree.Child(j).root.Attribut=mostlabel;
Tree.Child(j).root.Child=[];
else
Tree.Child(j).root=CreatTree(DataSet2,Attributs2);
end
end
end
function [Entropy RecordVal]=ComputEntropy(DataSet,attribut) %計(jì)算信息熵
if(attribut==0)
clnum=0;
for i=1:size(DataSet,1)
if(DataSet(i,size(DataSet,2))clnum) %防止下標(biāo)越界
classnum(DataSet(i,size(DataSet,2)))=0;
clnum=DataSet(i,size(DataSet,2));
RecordVal(DataSet(i,size(DataSet,2))).matrix=[];
end
classnum(DataSet(i,size(DataSet,2)))=classnum(DataSet(i,size(DataSet,2)))+1;
RecordVal(DataSet(i,size(DataSet,2))).matrix=[RecordVal(DataSet(i,size(DataSet,2))).matrix i];
end
Entropy=0;
for j=1:length(classnum)
P=classnum(j)/size(DataSet,1);
if(P~=0)
Entropy=Entropy+(-P)*log2(P);
end
end
else
valnum=0;
for i=1:size(DataSet,1)
if(DataSet(i,attribut)valnum) %防止參數(shù)下標(biāo)越界
clnum(DataSet(i,attribut))=0;
valnum=DataSet(i,attribut);
Valueexamnum(DataSet(i,attribut))=0;
RecordVal(DataSet(i,attribut)).matrix=[]; %將編號(hào)保留下來(lái),以方便后面按值分割數(shù)據(jù)集
end
if(DataSet(i,size(DataSet,2))clnum(DataSet(i,attribut))) %防止下標(biāo)越界
Value(DataSet(i,attribut)).classnum(DataSet(i,size(DataSet,2)))=0;
clnum(DataSet(i,attribut))=DataSet(i,size(DataSet,2));
end
Value(DataSet(i,attribut)).classnum(DataSet(i,size(DataSet,2)))= Value(DataSet(i,attribut)).classnum(DataSet(i,size(DataSet,2)))+1;
Valueexamnum(DataSet(i,attribut))= Valueexamnum(DataSet(i,attribut))+1;
RecordVal(DataSet(i,attribut)).matrix=[RecordVal(DataSet(i,attribut)).matrix i];
end
Entropy=0;
for j=1:valnum
Entropys=0;
for k=1:length(Value(j).classnum)
P=Value(j).classnum(k)/Valueexamnum(j);
if(P~=0)
Entropys=Entropys+(-P)*log2(P);
end
end
Entropy=Entropy+(Valueexamnum(j)/size(DataSet,1))*Entropys;
end
end
end
function showTree(Tree,level,value,branch,AttributValue,AttributName)
blank=[];
for i=1:level-1
if(branch(i)==1)
blank=[blank ' |'];
else
blank=[blank ' '];
end
end
blank=[blank ' '];
if(level==0)
blank=[' (The Root):'];
else
if isempty(AttributValue)
blank=[blank '|_____' int2str(value) '______'];
else
blank=[blank '|_____' value '______'];
end
end
if(length(Tree.Child)~=0) %非葉子節(jié)點(diǎn)
if isempty(AttributName)
disp([blank 'Attribut ' int2str(Tree.Attribut)]);
else
disp([blank 'Attribut ' AttributName{Tree.Attribut}]);
end
if isempty(AttributValue)
for j=1:length(Tree.Child)-1
showTree(Tree.Child(j).root,level+1,j,[branch 1],AttributValue,AttributName);
end
showTree(Tree.Child(length(Tree.Child)).root,level+1,length(Tree.Child),[branch(1:length(branch)-1) 0 1],AttributValue,AttributName);
else
for j=1:length(Tree.Child)-1
showTree(Tree.Child(j).root,level+1,AttributValue{Tree.Attribut}{j},[branch 1],AttributValue,AttributName);
end
showTree(Tree.Child(length(Tree.Child)).root,level+1,AttributValue{Tree.Attribut}{length(Tree.Child)},[branch(1:length(branch)-1) 0 1],AttributValue,AttributName);
end
else
if isempty(AttributValue)
disp([blank 'leaf ' int2str(Tree.Attribut)]);
else
disp([blank 'leaf ' AttributValue{length(AttributValue)}{Tree.Attribut}]);
end
end
end
function Rules=getRule(Tree)
if(length(Tree.Child)~=0)
Rules={};
for i=1:length(Tree.Child)
content=getRule(Tree.Child(i).root);
%disp(content);
%disp([num2str(Tree.Attribut) ',' num2str(i) ',']);
for j=1:size(content,1)
rule=cell2struct(content(j,1),{'str'});
content(j,1)={[num2str(Tree.Attribut) ',' num2str(i) ',' rule.str]};
end
Rules=[Rules;content];
end
else
Rules={['C' num2str(Tree.Attribut)]};
end
end
1、Jasmine
Jasmine是一個(gè)行為驅(qū)動(dòng)的測(cè)試開發(fā)框架,用于對(duì)JavaScript代碼進(jìn)行測(cè)試。它不依賴其它任何JavaScript框架,也不需要DOM。它的語(yǔ)法簡(jiǎn)潔、明確,寫測(cè)試非常容易。
2、Mocha
Mocha是一個(gè)功能豐富的JavaScript測(cè)試框架,既運(yùn)行于Node.js環(huán)境中,也可以運(yùn)行于瀏覽器環(huán)境中。Mocha以串行方式運(yùn)行測(cè)試,能做出靈活而準(zhǔn)確的報(bào)告,也能將測(cè)試中未捕捉的異常映射到正確的測(cè)試用例。
3、Chai
Chai是個(gè)支持BDD/TDD的庫(kù),可用于node和瀏覽器,可配合任何JavaScript測(cè)試框架使用。
4、QUnit
QUnit是個(gè)功能強(qiáng)大又易于使用的JavaScript單元測(cè)試框架。jQuery、jQueryUI和jQueyMobile項(xiàng)目都使用這個(gè)框架,它能測(cè)試普通的JavaScript代碼。
5、Sinon
Sinon.JS為JavaScript提供了獨(dú)立的spies、stubs和mocks[譯者注:Spy、Stub和Mock都是測(cè)試專用名詞,Stub常被翻譯為樁,spies是Spy的復(fù)數(shù)形式,是一種可以監(jiān)視方法、調(diào)用和參數(shù)的技術(shù)]。它不依賴任何東西,可以配合任何單元測(cè)試框架工作。
6、Karma
Karma是針對(duì)連通瀏覽器的一個(gè)框架無(wú)關(guān)測(cè)試運(yùn)行器。每一個(gè)測(cè)試結(jié)果對(duì)應(yīng)每個(gè)瀏覽器,它的測(cè)試和顯示都是通過(guò)命令行暴露給開發(fā)者的,這樣他們就可以看到瀏覽器測(cè)試的通過(guò)或失敗。
7、Selenium
Selenium有一個(gè)簡(jiǎn)單的目標(biāo):就是自動(dòng)化瀏覽器。它主要用于自動(dòng)化測(cè)試web應(yīng)用程序,但是只是很簡(jiǎn)單地考慮到了基于網(wǎng)絡(luò)的管理任務(wù)。
8、WebdriverIO
WebdriverIO允許用戶僅添加幾行代碼就可以控制瀏覽器或移動(dòng)應(yīng)用程序,使測(cè)試代碼更簡(jiǎn)單、簡(jiǎn)潔、易讀。集成的TestRunner同樣允許你以同步的方式調(diào)用異步命令,這樣你不需要關(guān)心如何處理Promise以避免競(jìng)態(tài)條件。此外,它取消了所有的繁瑣的設(shè)置工作,并且會(huì)為您管理的Selenium會(huì)話。
9、Nightwatch
Nightwatch.js是一個(gè)易于使用的Node.js,它是為基于瀏覽器的app和網(wǎng)站設(shè)計(jì)的終端到終端(E2E)的測(cè)試方法。它使用強(qiáng)大的W3CWebDriverAPI,用于在DOM元素上執(zhí)行命令和斷言。
10、PhantomCSS
PhantomCSS獲得CasperJS捕獲的屏幕截圖,并使用Resemble.js將其與基準(zhǔn)圖進(jìn)行對(duì)比,以測(cè)試RGB像素差異。java課程發(fā)現(xiàn)PhantomCSS然后生成圖像差異對(duì)比,用于幫助您找到原因。
11、PhantomFlow
PhantomFlow使用決策樹提供UI測(cè)試方案。針對(duì)PhantomJS,CasperJS和PhantomCSS的NodeJS包裝器——PhantomFlow能夠流暢地在代碼中描述用戶流程,同時(shí)生成用于可視化的結(jié)構(gòu)化樹數(shù)據(jù)。
public?static?void?Regular()?throws?Exception?{
File?inputfile?=?new?File("F:\\weka\\eucalyptus_Train.arff");
ArffLoader?loader?=?new?ArffLoader();
loader.setFile(inputfile);
Instances?insTrain?=?loader.getDataSet();
insTrain.setClassIndex(insTrain.numAttributes()-1);
inputfile?=?new?File("F:\\weka\\eucalyptus_Test.arff");
loader.setFile(inputfile);
Instances?insTest?=?loader.getDataSet();
insTest.setClassIndex(insTest.numAttributes()-1);
double?sum?=?insTest.numInstances();
int?right?=?0;
Classifier?clas?=?new?J48();
//Classifier?clas?=?new?weka.classifiers.bayes.BayesNet();
clas.buildClassifier(insTrain);
for(int?i?=?0;?i??sum;?i++)?{
if(clas.classifyInstance(insTest.instance(i))?==?insTest.instance(i).classValue())?{
right++;
}
System.out.println(clas.classifyInstance(insTest.instance(i))+"?: "+insTest.instance(i).classValue());
}
System.out.println("分類準(zhǔn)確率:"+right/sum);
}
svm的話,要用一個(gè)wlsvm的包。 代碼是一樣的,就是Classifier class= new J48()這里要用svm的實(shí)例
據(jù)我所知,java好像對(duì)大數(shù)據(jù)分析方面沒(méi)有什么現(xiàn)成的方法或包可以調(diào)用。
現(xiàn)在做數(shù)據(jù)分析(機(jī)器學(xué)習(xí))用的比較多的是Python和R還有Matlib;
//如果是簡(jiǎn)單的匯總分析,分類,回歸的話,excel就足夠了。java使用數(shù)據(jù)庫(kù)也可以完成。
其中Python算比較簡(jiǎn)單的,有現(xiàn)成的科學(xué)計(jì)算工具和非?;钴S的社區(qū)。
常用的算法:回歸分析,支持向量機(jī)(SVM),決策樹,K-近鄰(KNN),K-均值(k-means)。。。還有比較火的深度學(xué)習(xí)(DL)??梢粤私庖幌隆?/p>
算法思想:
將數(shù)列按有序化(遞增或遞減)排列,查找過(guò)程中采用跳躍式方式查找,即先以有序數(shù)列的中點(diǎn)位置為比較對(duì)象,如果要找的元素值小于該中點(diǎn)元素,則將待查序列縮小為左半部分,否則為右半部分。通過(guò)一次比較,將查找區(qū)間縮小一半。
折半查找是一種高效的查找方法。它可以明顯減少比較次數(shù),提高查找效率。但是,折半查找的先決條件是查找表中的數(shù)據(jù)元素必須有序。
算法步驟描述:
step1 首先確定整個(gè)查找區(qū)間的中間位置
mid = ( left + right )/ 2
step2 用待查關(guān)鍵字值與中間位置的關(guān)鍵字值進(jìn)行比較;
若相等,則查找成功
若大于,則在后(右)半個(gè)區(qū)域繼續(xù)進(jìn)行折半查找
若小于,則在前(左)半個(gè)區(qū)域繼續(xù)進(jìn)行折半查找
Step3 對(duì)確定的縮小區(qū)域再按折半公式,重復(fù)上述步驟。最后,得到結(jié)果:要么查找成功, 要么查找失敗。
折半查找的存儲(chǔ)結(jié)構(gòu)采用一維數(shù)組存放。
折半查找算法舉例
對(duì)給定數(shù)列(有序),按折半查找算法,查找關(guān)鍵字值為30的數(shù)據(jù)元素。
折半查找的算法討論:
優(yōu)點(diǎn): ASL≤log2n,即每經(jīng)過(guò)一次比較,查找范圍就縮小一半。經(jīng)log2n 次計(jì)較就可以完成查找過(guò)程。
缺點(diǎn):因要求有序,所以要求查找數(shù)列必須有序,而對(duì)所有數(shù)據(jù)元素按大小排序是非常費(fèi)時(shí)的操作。另外,順序存儲(chǔ)結(jié)構(gòu)的插入、刪除操作不便利。
考慮:能否通過(guò)一次比較拋棄更多的部分(即經(jīng)過(guò)一次比較,使查找范圍縮得更小),以達(dá)到提高效率的目的?!?/p>
可以考慮把兩種方法(順序查找和折半查找)結(jié)合起來(lái),即取順序查找簡(jiǎn)單和折半查找高效之所長(zhǎng),來(lái)達(dá)到提高效率的目的?實(shí)際上這就是分塊查找的算法思想。
例如:[問(wèn)題分析] 由于數(shù)據(jù)按升序排列,故用折半查找最快捷.
program binsearch;
const max=10;
var num:array[1..max] of integer;
i,n:integer;
procedure search(x,a,b:integer);
var mid:integer;
begin
if a=b then
if x=num[a] then writeln('Found:',a) else writeln('Number not found')
else begin
mid:=(a+b) div 2;
if xnum[mid] then search(x,mid,b);
if xnum[mid] then search(x,a,mid);
if x=num[mid] then writeln('Found:',mid);
end;
end;
begin
write('Please input 10 numbers in order:');
for i:=1 to max do read(num);
write('Please input the number to search:');
readln(n);
search(n,1,max);
end.
Java風(fēng)格的代碼舉例:
//使用折半法進(jìn)行查找
int getIndex(int[] nList, int nCount, int nCode) {
int nIndex = -1;
int jMin = 0;
int jMax = nCount - 1;
int jCur = (jMin+jMax)/2;
do
{
if(nList[jCur] nCode) {
jMax--;
} else if(nList[jCur] nCode) {
jMin++;
} else if(nList[jCur] == nCode) {
nIndex = jCur;
break;
}
jCur = (jMin + jMax)/2;
} while(jMin jMax);
return nIndex;
}
二分查找的性能說(shuō)明
雖然二分查找的效率高,但是要將表按關(guān)鍵字排序。而排序本身是一種很費(fèi)時(shí)的運(yùn)算。既使采用高效率的排序方法也要花費(fèi) O(n lg n) 的時(shí)間。
二分查找只適用順序存儲(chǔ)結(jié)構(gòu)。為保持表的有序性,在順序結(jié)構(gòu)里插入和刪除都必須移動(dòng)大量的結(jié)點(diǎn)。因此,二分查找特別適用于那種一經(jīng)建立就很少改動(dòng)、而又經(jīng)常需要查找的線性表。
對(duì)那些查找少而又經(jīng)常需要改動(dòng)的線性表,可采用鏈表作存儲(chǔ)結(jié)構(gòu),進(jìn)行順序查找。鏈表上無(wú)法實(shí)現(xiàn)二分查找
二分查找的C#實(shí)現(xiàn)代碼:
using System;
using System.Collections.Generic;
using System.Text;
namespace BinschDemo
{
public class BinschDemo
{
public static int Binsch(int[] a, int key)
{
int low = 1;
int high = a.Length;
while (low = high)
{
int mid = (low + high) / 2;
if (key == a[mid])
{
return mid; //返回找到的索引值
}
else
{
if (key a[mid])
high = mid - 1;
else
low = mid + 1;
}
}
return -1; //查找失敗
}
static void Main(string[] args)
{
Console.WriteLine("請(qǐng)輸入10個(gè)遞增數(shù)字: ");
int[] list = new int[10];
for (int i = 0; i 10; i++)
{
Console.Write("數(shù)字 : ", i);
list = Convert.ToInt32(Console.ReadLine());
}
Console.Write("請(qǐng)輸入一個(gè)你要查找的數(shù)字:");
int find = Convert.ToInt32(Console.ReadLine());
int result = Binsch(list, find);
Console.WriteLine(result);
}
}
}
分塊查找又索引查找,它主要用于“分塊有序”表的查找。所謂“分塊有序”是指將線性表L(一維數(shù)組)分成m個(gè)子表(要求每個(gè)子表的長(zhǎng)度相等),且第i+1個(gè)子表中的每一個(gè)項(xiàng)目均大于第i個(gè)子表中的所有項(xiàng)目。“分塊有序”表應(yīng)該包括線性表L本身和分塊的索引表A。因此,分塊查找的關(guān)鍵在于建立索引表A。
(1)建立索引表A(二維數(shù)組)
索引表包括兩部分:關(guān)鍵字項(xiàng)(子表中的最大值)和指針項(xiàng)(子表的第一項(xiàng)在線性表L中位置)
索引表按關(guān)鍵字有序的。
例如:線性表L(有序)為:1 2 3 4 5 6 7 8 9 10 11 12
分成m=3個(gè)子表:
索引表A:二維數(shù)組:第一列為每個(gè)子表的最大值 ,第二列為每個(gè)子表的起始地址
即: 4 0
8 4
12 8
(2)利用索引表A,確定待查項(xiàng)X所在的子表(塊)。
(3)在所確定的子表中可以用“折半查找”法搜索待查項(xiàng)X;若找到則輸出X;否則輸出未找到信息。