About BBS

機械学習 リファレンス マニュアル

最終変更者: 怡土順一, 最終変更リビジョン: 467, 最終変更日時: 2009-06-23 14:23:34 +0900 (火, 23 6月 2009)


K近傍法(K Nearest Neighbors)

このアルゴリズムでは,まず学習用サンプルを全て取り込み,次に新たに追加されるサンプルの応答を適当な数(K)の近傍を分析(投票や加重和などを使用することが多い)し,予測する. この手法では,予測のために与えられたベクトルの近傍にある既知の応答を用いて特徴ベクトルを調べるため,「サンプルによる学習(learning by example)」として紹介されることがある.


CvKNearest

K近傍法モデルクラス

class CvKNearest : public CvStatModel
{
public:

    CvKNearest();
    virtual ~CvKNearest();

    CvKNearest( const CvMat* _train_data, const CvMat* _responses,
                const CvMat* _sample_idx=0, bool _is_regression=false, int max_k=32 );

    virtual bool train( const CvMat* _train_data, const CvMat* _responses,
                        const CvMat* _sample_idx=0, bool is_regression=false,
                        int _max_k=32, bool _update_base=false );

    virtual float find_nearest( const CvMat* _samples, int k, CvMat* results,
        const float** neighbors=0, CvMat* neighbor_responses=0, CvMat* dist=0 ) const;

    virtual void clear();
    int get_max_k() const;
    int get_var_count() const;
    int get_sample_count() const;
    bool is_regression() const;

protected:
    ...
};

CvKNearest::train

モデルの学習

bool CvKNearest::train(const CvMat* _train_data, const CvMat* _responses,
                        const CvMat* _sample_idx=0, bool is_regression=false,
                        int _max_k=32, bool _update_base=false );

このメソッドは,K近傍法に必要なモデルを学習させる. これは以下の制限を持つようなtrain「メソッド」の一般的な仕様に従う. CV_ROW_SAMPLEデータレイアウトのみサポート,入力される変数は全て連続変数, 出力される変数はカテゴリ変数(is_regression=false)か,連続変数(is_regression=true), あるいは変数の部分集合(var_idx)のいずれか.データ欠損はサポートされない.

パラメータ_max_kは,メソッドfind_nearestに渡される近傍の最大数を指定する.

パラメータ_update_baseは,モデルを始めから作り直す(_update_base=false)か, 新しい教師データを使って更新する(_update_base=true)かを指定する. 後者の場合,パラメータ_max_kを元の値より大きくしてはならない.


CvKNearest::find_nearest

入力ベクトルの近傍を探す

float CvKNearest::find_nearest(const CvMat* _samples, int k, CvMat* results=0,
        const float** neighbors=0, CvMat* neighbor_responses=0, CvMat* dist=0 ) const;

このメソッドは,各入力ベクトル(行列 _samplesの行に相当)に対し,k≤get_max_k()の最近傍を探す. 回帰の場合,予測された結果は個々の近傍ベクトルの平均値となる.分類の場合,クラスは投票により決定される.

ユーザ定義の分類/回帰による予測のために,このメソッドはオプションとして近傍ベクトル自体へのポインタ (neighbors, k*_samples->rows個のポインタの配列)と, それぞれの出力値(neighbor_responsesk*_samples->rows個の要素を持つベクトル)と, 入力ベクトルと近傍ベクトル間の距離(distk*_samples->rows個の要素を持つベクトル)を返すことができる.

各入力ベクトルに対し,近傍は各ベクトルへの距離でソートされる.

入力ベクトルが一つの場合, 全ての出力行列はオプションとなり, このメソッドにより予測された値が返される.


(例)k近傍法の分類器を用いた,混合ガウス分布からの2次元サンプルの分類

#include "ml.h"
#include "highgui.h"

int main( int argc, char** argv )
{
    const int K = 10;
    int i, j, k, accuracy;
    float response;
    int train_sample_count = 100;
    CvRNG rng_state = cvRNG(-1);
    CvMat* trainData = cvCreateMat( train_sample_count, 2, CV_32FC1 );
    CvMat* trainClasses = cvCreateMat( train_sample_count, 1, CV_32FC1 );
    IplImage* img = cvCreateImage( cvSize( 500, 500 ), 8, 3 );
    float _sample[2];
    CvMat sample = cvMat( 1, 2, CV_32FC1, _sample );
    cvZero( img );

    CvMat trainData1, trainData2, trainClasses1, trainClasses2;

    // 学習サンプルの生成
    cvGetRows( trainData, &trainData1, 0, train_sample_count/2 );
    cvRandArr( &rng_state, &trainData1, CV_RAND_NORMAL, cvScalar(200,200), cvScalar(50,50) );

    cvGetRows( trainData, &trainData2, train_sample_count/2, train_sample_count );
    cvRandArr( &rng_state, &trainData2, CV_RAND_NORMAL, cvScalar(300,300), cvScalar(50,50) );

    cvGetRows( trainClasses, &trainClasses1, 0, train_sample_count/2 );
    cvSet( &trainClasses1, cvScalar(1) );

    cvGetRows( trainClasses, &trainClasses2, train_sample_count/2, train_sample_count );
    cvSet( &trainClasses2, cvScalar(2) );

    // 分類器の学習
    CvKNearest knn( trainData, trainClasses, 0, false, K );
    CvMat* nearests = cvCreateMat( 1, K, CV_32FC1);

    for( i = 0; i < img->height; i++ )
    {
        for( j = 0; j < img->width; j++ )
        {
            sample.data.fl[0] = (float)j;
            sample.data.fl[1] = (float)i;

            // 応答の推定と,近傍のラベルの取得
            response = knn.find_nearest(&sample,K,0,0,nearests,0);

            // 多数を占める近傍数の計算
            for( k = 0, accuracy = 0; k < K; k++ )
            {
                if( nearests->data.fl[k] == response)
                    accuracy++;
            }
            // 精度(もしくは確信度)に応じてピクセルをハイライト
            cvSet2D( img, i, j, response == 1 ?
                (accuracy > 5 ? CV_RGB(180,0,0) : CV_RGB(180,120,0)) :
                (accuracy > 5 ? CV_RGB(0,180,0) : CV_RGB(120,120,0)) );
        }
    }

    // 元の学習サンプルの表示
    for( i = 0; i < train_sample_count/2; i++ )
    {
        CvPoint pt;
        pt.x = cvRound(trainData1.data.fl[i*2]);
        pt.y = cvRound(trainData1.data.fl[i*2+1]);
        cvCircle( img, pt, 2, CV_RGB(255,0,0), CV_FILLED );
        pt.x = cvRound(trainData2.data.fl[i*2]);
        pt.y = cvRound(trainData2.data.fl[i*2+1]);
        cvCircle( img, pt, 2, CV_RGB(0,255,0), CV_FILLED );
    }

    cvNamedWindow( "classifier result", 1 );
    cvShowImage( "classifier result", img );
    cvWaitKey(0);

    cvReleaseMat( &trainClasses );
    cvReleaseMat( &trainData );
    return 0;
}

OpenCV-1.0 リファレンス マニュアル
OpenCV-1.1pre リファレンス マニュアル
OpenCVサンプルコード


CV_ASSERT
CV_CALL
CV_CHECK
CV_CMP
CV_ERROR
CV_FUNCNAME
CV_GET_SEQ_ELEM
CV_GRAPH_EDGE_FIELDS
CV_GRAPH_FIELDS
CV_GRAPH_VERTEX_FIELDS
CV_IABS
CV_IMAGE_ELEM
CV_IMAX
CV_IMIN
CV_NODE_HAS_NAME
CV_NODE_IS_COLLECTION
CV_NODE_IS_EMPTY
CV_NODE_IS_FLOW
CV_NODE_IS_INT
CV_NODE_IS_MAP
CV_NODE_IS_REAL
CV_NODE_IS_SEQ
CV_NODE_IS_STRING
CV_NODE_IS_USER
CV_NODE_SEQ_IS_SIMPLE
CV_QUADEDGE2D_FIELDS
CV_RGB
CV_SEQUENCE_FIELDS
CV_SET_FIELDS
CV_SIGN
CV_SUBDIV2D_FIELDS
CV_SUBDIV2D_POINT_FIELDS
CV_SWAP
CV_TREE_NODE_FIELDS
CV_TURN_ON_IPL_COMPATIBILITY
EXIT
MAX
MIN
OPENCV_ASSERT
OPENCV_CALL
OPENCV_ERRCHK
OPENCV_ERROR
__BEGIN__
__END__

CvANN_MLP
CvANN_MLP::create
CvANN_MLP::train
CvANN_MLP_TrainParams
CvArr
CvAttrList
CvBoost
CvBoost::get_weak_predictors
CvBoost::predict
CvBoost::prune
CvBoost::train
CvBoostParams
CvBoostTree
CvBox2D
CvCapture
CvConDensation
CvConnectedComp
CvConvexityDefect
CvDTree
CvDTree::predict
CvDTree::train
CvDTreeNode
CvDTreeParams
CvDTreeSplit
CvDTreeTrainData
CvEM
CvEM::train
CvEMParams
CvFileNode
CvFileStorage
CvGraph
CvGraphScanner
CvHMM
CvHaarClassifier
CvHaarClassifierCascade
CvHaarFeature
CvHaarStageClassifier
CvHistogram
CvImgObsInfo
CvKNearest
CvKNearest::find_nearest
CvKNearest_train
CvKalman
CvMat
CvMatND
CvMemBlock
CvMemStorage
CvMemStoragePos
CvNormalBayesClassifier
CvNormalBayesClassifier::predict
CvNormalBayesClassifier::train
CvPoint
CvPoint2D32f
CvPoint2D64f
CvPoint3D32f
CvPoint3D64f
CvQuadEdge2D
CvRTParams
CvRTrees
CvRTrees::get_proximity
CvRTrees::get_var_importance
CvRTrees::predict
CvRTrees::train
CvRect
CvSVM
CvSVM::get_default_grid
CvSVM::get_params
CvSVM::get_support_vector
CvSVM::train
CvSVM::train_auto
CvSVMParams
CvScalar
CvSeq
CvSeqBlock
CvSet
CvSize
CvSize2D32f
CvSlice
CvSparseMat
CvStatModel
CvStatModel::CvStatModel
CvStatModel::CvStatModel(data)
CvStatModel::clear
CvStatModel::load
CvStatModel::predict
CvStatModel::read
CvStatModel::save
CvStatModel::train
CvStatModel::write
CvStatModel::~CvStatModel
CvStereoBMState
CvStereoGCState
CvSubdiv2D
CvSubdiv2DPoint
CvTermCriteria
CvTreeNodeIterator
CvTypeInfo
IplImage
RTreesOOBerror

cv2DRotationMatrix
cv3dTrackerCalibrateCameras
cv3dTrackerLocateObjects
cvAbs
cvAbsDiff
cvAbsDiffS
cvAcc
cvAdaptiveThreshold
cvAdd
cvAddS
cvAddWeighted
cvAlloc
cvAnd
cvAndS
cvApproxChains
cvApproxPoly
cvArcLength
cvAvg
cvAvgSdv
cvBackProjectPCA
cvBoundingRect
cvBoxPoints
cvCalcBackProject
cvCalcBackProjectPatch
cvCalcCovarMatrix
cvCalcCovarMatrixEx
cvCalcDecompCoeff
cvCalcEMD2
cvCalcEigenObjects
cvCalcGlobalOrientation
cvCalcHist
cvCalcImageHomography
cvCalcMotionGradient
cvCalcOpticalFlowBM
cvCalcOpticalFlowHS
cvCalcOpticalFlowLK
cvCalcOpticalFlowPyrLK
cvCalcPCA
cvCalcPGH
cvCalcProbDensity
cvCalcSubdivVoronoi2D
cvCalibrateCamera2
cvCalibrationMatrixValues
cvCamShift
cvCanny
cvCartToPolar
cvCbrt
cvCheckArr
cvCheckArray
cvCheckContourConvexity
cvCircle
cvClearGraph
cvClearHist
cvClearMemStorage
cvClearND
cvClearSeq
cvClearSet
cvClearSubdivVoronoi2D
cvClipLine
cvClone
cvCloneGraph
cvCloneImage
cvCloneMat
cvCloneMatND
cvCloneSeq
cvCloneSparseMat
cvCmp
cvCmpS
cvCompareHist
cvComputeCorrespondEpilines
cvConDensInitSampleSet
cvConDensUpdateByTime
cvContourArea
cvContourFromContourTree
cvConvert
cvConvertImage
cvConvertPointsHomogeneous
cvConvertScale
cvConvertScaleAbs
cvConvexHull2
cvConvexityDefects
cvCopy
cvCopyHist
cvCopyMakeBorder
cvCornerEigenValsAndVecs
cvCornerHarris
cvCornerMinEigenVal
cvCountNonZero
cvCreate2DHMM
cvCreateCameraCapture
cvCreateChildMemStorage
cvCreateConDensation
cvCreateContourTree
cvCreateData
cvCreateFeatureTree
cvCreateFileCapture
cvCreateGraph
cvCreateGraphScanner
cvCreateHist
cvCreateImage
cvCreateImageHeader
cvCreateKalman
cvCreateMat
cvCreateMatHeader
cvCreateMatND
cvCreateMatNDHeader
cvCreateMemStorage
cvCreateObsInfo
cvCreatePOSITObject
cvCreateSeq
cvCreateSet
cvCreateSparseMat
cvCreateStereoBMState
cvCreateStereoGCState
cvCreateStructuringElementEx
cvCreateSubdivDelaunay2D
cvCreateTrackbar
cvCreateVideoWriter
cvCrossProduct
cvCvtColor
cvCvtPixToPlane
cvCvtPlaneToPix
cvCvtScale
cvCvtScaleAbs
cvCvtSeqToArray
cvDCT
cvDFT
cvDecRefData
cvDeleteMoire
cvDestroyAllWindows
cvDestroyWindow
cvDet
cvDilate
cvDistTransform
cvDiv
cvDotProduct
cvDrawChessBoardCorners
cvDrawContours
cvDynamicCorrespondMulti
cvEViterbi
cvEigenDecomposite
cvEigenProjection
cvEigenVV
cvEllipse
cvEllipse2Poly
cvEllipseBox
cvEndFindContours
cvEndWriteSeq
cvEndWriteStruct
cvEqualizeHist
cvErode
cvError
cvErrorStr
cvEstimateHMMStateParams
cvEstimateObsProb
cvEstimateTransProb
cvExp
cvExtractSURF
cvFastArctan
cvFillConvexPoly
cvFillPoly
cvFilter2D
cvFindChessboardCorners
cvFindContours
cvFindCornerSubPix
cvFindExtrinsicCameraParams2
cvFindFeatures
cvFindFeaturesBoxed
cvFindFundamentalMat
cvFindGraphEdge
cvFindGraphEdgeByPtr
cvFindHomography
cvFindNearestPoint2D
cvFindNextContour
cvFindRuns
cvFindStereoCorrespondence
cvFindStereoCorrespondenceBM
cvFindStereoCorrespondenceGC
cvFindType
cvFirstType
cvFitEllipse2
cvFitLine2D
cvFlip
cvFloodFill
cvFlushSeqWriter
cvFree
cvGEMM
cvGet1D
cvGet2D
cvGet3D
cvGetAffineTransform
cvGetCaptureProperty
cvGetCentralMoment
cvGetCol
cvGetCols
cvGetDiag
cvGetDims
cvGetElemType
cvGetErrMode
cvGetErrStatus
cvGetFileNode
cvGetFileNodeByName
cvGetFileNodeName
cvGetGraphVtx
cvGetHashedKey
cvGetHistValue_1D
cvGetHistValue_2D
cvGetHistValue_3D
cvGetHistValue_nD
cvGetHuMoments
cvGetImage
cvGetImageCOI
cvGetImageROI
cvGetMat
cvGetMinMaxHistValue
cvGetModuleInfo
cvGetND
cvGetNextSparseNode
cvGetNormalizedCentralMoment
cvGetNumThreads
cvGetOptimalDFTSize
cvGetPerspectiveTransform
cvGetQuadrangleSubPix
cvGetRawData
cvGetReal1D
cvGetReal2D
cvGetReal3D
cvGetRealND
cvGetRectSubPix
cvGetRootFileNode
cvGetRow
cvGetRows
cvGetSeqElem
cvGetSeqReaderPos
cvGetSetElem
cvGetSize
cvGetSpatialMoment
cvGetSubRect
cvGetTextSize
cvGetThreadNum
cvGetTickCount
cvGetTickFrequency
cvGetTrackbarPos
cvGetWindowHandle
cvGetWindowName
cvGoodFeaturesToTrack
cvGrabFrame
cvGraphAddEdge
cvGraphAddEdgeByPtr
cvGraphAddVtx
cvGraphEdgeIdx
cvGraphFindEdge
cvGraphFindEdgeByPtr
cvGraphRemoveEdge
cvGraphRemoveEdgeByPtr
cvGraphRemoveVtx
cvGraphRemoveVtxByPtr
cvGraphVtxDegree
cvGraphVtxDegreeByPtr
cvGraphVtxIdx
cvGuiBoxReport
cvHaarDetectObjects
cvHoughCircles
cvHoughLines2
cvImgToObs_DCT
cvInRange
cvInRangeS
cvIncRefData
cvInitFont
cvInitImageHeader
cvInitLineIterator
cvInitMatHeader
cvInitMatNDHeader
cvInitMixSegm
cvInitSparseMatIterator
cvInitSystem
cvInitTreeNodeIterator
cvInitUndistortMap
cvInitUndistortRectifyMap
cvInpaint
cvInsertNodeIntoTree
cvIntegral
cvInv
cvInvSqrt
cvInvert
cvIsInf
cvIsNaN
cvKMeans2
cvKalmanCorrect
cvKalmanPredict
cvKalmanUpdateByMeasurement
cvKalmanUpdateByTime
cvLUT
cvLaplace
cvLine
cvLoad
cvLoadHaarClassifierCascade
cvLoadImage
cvLog
cvLogPolar
cvMahalanobis
cvMakeAlphaScanlines
cvMakeHistHeaderForArray
cvMakeScanlines
cvMakeSeqHeaderForArray
cvMat
cvMatMul
cvMatMulAdd
cvMatchContourTrees
cvMatchShapes
cvMatchTemplate
cvMax
cvMaxRect
cvMaxS
cvMeanShift
cvMemStorageAlloc
cvMemStorageAllocString
cvMerge
cvMin
cvMinAreaRect2
cvMinEnclosingCircle
cvMinMaxLoc
cvMinS
cvMirror
cvMixChannels
cvMixSegmL2
cvMoments
cvMorphEpilinesMulti
cvMorphologyEx
cvMoveWindow
cvMul
cvMulAddS
cvMulSpectrums
cvMulTransposed
cvMultiplyAcc
cvNamedWindow
cvNextGraphItem
cvNextTreeNode
cvNorm
cvNormalize
cvNormalizeHist
cvNot
cvNulDevReport
cvOpenFileStorage
cvOr
cvOrS
cvPOSIT
cvPerspectiveTransform
cvPointPolygonTest
cvPointSeqFromMat
cvPolarToCart
cvPolyLine
cvPostWarpImage
cvPow
cvPreCornerDetect
cvPreWarpImage
cvPrevTreeNode
cvProjectPCA
cvProjectPoints2
cvPtr1D
cvPtr2D
cvPtr3D
cvPtrND
cvPutText
cvPyrDown
cvPyrMeanShiftFiltering
cvPyrSegmentation
cvPyrUp
cvQueryFrame
cvQueryHistValue_1D
cvQueryHistValue_2D
cvQueryHistValue_3D
cvQueryHistValue_nD
cvRNG
cvRandArr
cvRandInt
cvRandReal
cvRandShuffle
cvRange
cvRead
cvReadByName
cvReadChainPoint
cvReadInt
cvReadIntByName
cvReadRawData
cvReadRawDataSlice
cvReadReal
cvReadRealByName
cvReadString
cvReadStringByName
cvRectangle
cvRedirectError
cvReduce
cvRegisterModule
cvRegisterType
cvRelease
cvRelease2DHMM
cvReleaseCapture
cvReleaseConDensation
cvReleaseData
cvReleaseFeatureTree
cvReleaseFileStorage
cvReleaseGraphScanner
cvReleaseHaarClassifierCascade
cvReleaseHist
cvReleaseImage
cvReleaseImageHeader
cvReleaseKalman
cvReleaseMat
cvReleaseMatND
cvReleaseMemStorage
cvReleaseObsInfo
cvReleasePOSITObject
cvReleaseSparseMat
cvReleaseStereoBMState
cvReleaseStereoGCState
cvReleaseStructuringElement
cvReleaseVideoWriter
cvRemap
cvRemoveNodeFromTree
cvRepeat
cvReprojectImageTo3D
cvResetImageROI
cvReshape
cvReshapeMatND
cvReshapeND
cvResize
cvResizeWindow
cvRestoreMemStoragePos
cvRetrieveFrame
cvRodrigues2
cvRound
cvRunHaarClassifierCascade
cvRunningAvg
cvSVBkSb
cvSVD
cvSampleLine
cvSave
cvSaveImage
cvSaveMemStoragePos
cvScale
cvScaleAdd
cvSegmentMotion
cvSeqElemIdx
cvSeqInsert
cvSeqInsertSlice
cvSeqInvert
cvSeqPartition
cvSeqPop
cvSeqPopFront
cvSeqPopMulti
cvSeqPush
cvSeqPushFront
cvSeqPushMulti
cvSeqRemove
cvSeqRemoveSlice
cvSeqSearch
cvSeqSlice
cvSeqSort
cvSet
cvSet1D
cvSet2D
cvSet3D
cvSetAdd
cvSetCaptureProperty
cvSetData
cvSetErrMode
cvSetErrStatus
cvSetHistBinRanges
cvSetIPLAllocators
cvSetIdentity
cvSetImageCOI
cvSetImageROI
cvSetImagesForHaarClassifierCascade
cvSetMemoryManager
cvSetMouseCallback
cvSetND
cvSetNew
cvSetNumThreads
cvSetReal1D
cvSetReal2D
cvSetReal3D
cvSetRealND
cvSetRemove
cvSetRemoveByPtr
cvSetSeqBlockSize
cvSetSeqReaderPos
cvSetTrackbarPos
cvSetZero
cvShowImage
cvSmooth
cvSnakeImage
cvSobel
cvSolveCubic
cvSolvePoly
cvSplit
cvSqrt
cvSquareAcc
cvStartAppendToSeq
cvStartFindContours
cvStartNextStream
cvStartReadChainPoints
cvStartReadRawData
cvStartReadSeq
cvStartWriteSeq
cvStartWriteStruct
cvStdErrReport
cvStereoCalibrate
cvStereoRectify
cvStereoRectifyUncalibrated
cvSub
cvSubRS
cvSubS
cvSubdiv2DEdgeDst
cvSubdiv2DEdgeOrg
cvSubdiv2DGetEdge
cvSubdiv2DLocate
cvSubdiv2DNextEdge
cvSubdiv2DRotateEdge
cvSubdivDelaunay2DInsert
cvSubstituteContour
cvSum
cvT
cvThreshHist
cvThreshold
cvTrace
cvTransform
cvTranspose
cvTreeToNodeSeq
cvTypeOf
cvUndistort2
cvUndistortPoints
cvUniformImgSegm
cvUnregisterType
cvUpdateMotionHistory
cvUseOptimized
cvWaitKey
cvWarpAffine
cvWarpPerspective
cvWarpPerspectiveQMatrix
cvWatershed
cvWrite
cvWriteComment
cvWriteFileNode
cvWriteFrame
cvWriteInt
cvWriteRawData
cvWriteReal
cvWriteString
cvXor
cvXorS
cvZero
cvmGet
cvmSet
error_handling_sample