導航:首頁 > 源碼編譯 > 分類演算法源碼

分類演算法源碼

發布時間:2024-10-26 03:06:30

㈠ 求KNN文本分類演算法java實現源代碼【散分了!!!!】

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
#define NATTRS 5 //number of attributes
#define MAXSZ 1700 //max size of training set
#define MAXVALUE 10000.0 //the biggest attribute's value is below 10000(int)
#define K 5
struct vector {
double attributes[NATTRS];
double classlabel;
};
struct item {
double distance;
double classlabel;
};
struct vector trSet[MAXSZ];//global variable,the training set
struct item knn[K];//global variable,the k-neareast-neighbour set
int curTSize = 0; //current size of the training set
int AddtoTSet(struct vector v)
{
if(curTSize>=MAXSZ) {
cout<<endl<<"The training set has "<<MAXSZ<<" examples!"<<endl<<endl;
return 0;
}
trSet[curTSize] = v;
curTSize++;
return 1;
}
double Distance(struct vector v1,struct vector v2)
{
double d = 0.0;
double tem = 0.0;
for(int i = 0;i < NATTRS;i++)
tem += (v1.attributes[i]-v2.attributes[i])*(v1.attributes[i]-v2.attributes[i]);
d = sqrt(tem);
return d;
}
int max(struct item knn[]) //return the no. of the item which has biggest distance(
//should be replaced)
{
int maxNo = 0;
if(K > 1)
for(int i = 1;i < K;i++)
if(knn[i].distance>knn[maxNo].distance)
maxNo = i;
return maxNo;
}double Classify(struct vector v)//decide which class label will be assigned to
//a given input vetor with the knn method
{
double dd = 0;
int maxn = 0;
int freq[K];
double mfreqC = 0;//the class label appears most frequently
int i;
for(i = 0;i < K;i++)
knn[i].distance = MAXVALUE;
for(i = 0;i < curTSize;i++)
{
dd = Distance(trSet[i],v);
maxn = max(knn);//for every new state of the training set should update maxn
if(dd < knn[maxn].distance) {
knn[maxn].distance = dd;
knn[maxn].classlabel = trSet[i].classlabel;
}
}
for(i = 0;i < K;i++)//freq[i] represents knn[i].classlabel appears how many times
freq[i] = 1;
for(i = 0;i < K;i++)
for(int j = 0;j < K;j++)
if((i!=j)&&(knn[i].classlabel == knn[j].classlabel))
freq[i]+=1;
int mfreq = 1;
mfreqC = knn[0].classlabel;
for(i = 0;i < K;i++)
if(freq[i] > mfreq) {
mfreq = freq[i];//mfreq represents the most frepuences
mfreqC = knn[i].classlabel; //mfreqNo is the item no. with the most frequent
//classlabel
}
return mfreqC;
}
void main()
{ double classlabel;
double c;
double n;
struct vector trExmp;
int i;
ifstream filein("G:\\data\\for knn\\data.txt");
if(filein.fail()){cout<<"Can't open data.txt"<<endl; return;}
while(!filein.eof()) {
filein>>c;
trExmp.classlabel = c;
cout<<trExmp.classlabel<<" "; for(int i = 0;i < NATTRS;i++) {
filein>>n;
trExmp.attributes[i] = n;
cout<<trExmp.attributes[i]<<" ";
} cout<<endl;
if(!AddtoTSet(trExmp))
break;
}filein.close();struct vector testv={{142,188,11,1159,0.5513196},17};
classlabel = Classify(testv);
cout<<"The classlable of the testv is: ";
cout<<classlabel<<endl;
for(i = 0;i < K;i++)
cout<<knn[i].distance<<"\t"<<knn[i].classlabel<<endl;
//cout<<max(knn);
}

㈡ 如何用OpenCV訓練自己的分類器

目標檢測方法最初由Paul Viola [Viola01]提出,並由Rainer Lienhart [Lienhart02]對這一方法進行了改善。該方法的基本步驟為: 首先,利用樣本(大約幾百幅樣本圖片)的 harr 特徵進行分類器訓練,得到一個級聯的boosted分類器。
分類器中的"級聯"是指最終的分類器是由幾個簡單分類器級聯組成。在圖像檢測中,被檢窗口依次通過每一級分類器, 這樣在前面幾層的檢測中大部分的候選區域就被排除了,全部通過每一級分類器檢測的區域即為目標區域。
分類器訓練完以後,就可以應用於輸入圖像中的感興趣區域的檢測。檢測到目標區域分類器輸出為1,否則輸出為0。為了檢測整副圖像,可以在圖像中移動搜索窗口,檢測每一個位置來確定可能的目標。 為了搜索不同大小的目標物體,分類器被設計為可以進行尺寸改變,這樣比改變待檢圖像的尺寸大小更為有效。所以,為了在圖像中檢測未知大小的目標物體,掃描程序通常需要用不同比例大小的搜索窗口對圖片進行幾次掃描。
目前支持這種分類器的boosting技術有四種: Discrete Adaboost, Real Adaboost, Gentle Adaboost and Logitboost。
"boosted" 即指級聯分類器的每一層都可以從中選取一個boosting演算法(權重投票),並利用基礎分類器的自我訓練得到。
根據上面的分析,目標檢測分為三個步驟:
1、 樣本的創建
2、 訓練分類器
3、 利用訓練好的分類器進行目標檢測。

閱讀全文

與分類演算法源碼相關的資料

熱點內容
加密朋克風 瀏覽:910
安卓怎麼把軟體全部置於前台 瀏覽:634
腰椎解壓器價格 瀏覽:644
編譯器生成技術 瀏覽:752
空調壓縮機啟停怎麼辦 瀏覽:454
android手機diy 瀏覽:900
哪個app可以記錄文字和照片 瀏覽:651
韓兆琦史記pdf 瀏覽:360
phppregreplace正則 瀏覽:105
賬戶密碼忘了怎麼辦安卓 瀏覽:403
月輪加密花列個日 瀏覽:828
選定文件移動到文件夾怎麼操作 瀏覽:953
artturbo編譯增強技術 瀏覽:671
10年程序員直播 瀏覽:520
加密鎖檢測深思服務未啟動 瀏覽:442
伺服器託管業務怎麼選 瀏覽:744
廣州黑馬程序員怎麼樣 瀏覽:482
qq號加密碼圖片 瀏覽:174
如何建立游戲本地伺服器 瀏覽:948
懂車帝app直播怎麼開 瀏覽:26