#include #include <boost/numeric/ublas/matrix.hpp> #include <omp.h> #include "MINE.h"

using namespace boost::numeric::ublas;

matrix<double, row_major> calc_mic(const matrix<double, row_major>& mat, int threads) { //当样本量小于6个时,终止程序 if(mat.size1()<6){ std::cerr << "error: the number of data rows should be greater than 5." << std::endl; } if(mat.size2()<2){ std::cerr << "error: the number of data columns should be greater than 1." << std::endl; } // 创建空的对称矩阵 matrix<double, row_major> result(mat.size2(), mat.size2()); // 创建MINE对象

// 计算每列两两之间的mic系数,并填充对称矩阵
omp_set_num_threads(threads);
#pragma omp parallel for
for (size_t i = 0; i < mat.size2(); i++) {
    MINE mine(0.5, 15, EST_MIC_APPROX);
    for (size_t j = i+1; j < mat.size2(); j++) {  // 计算上三角部分
        double x[mat.size1()];
        double y[mat.size1()];
        for (size_t k = 0; k < mat.size1(); k++) {
            x[k] = mat(k,i);
            y[k] = mat(k,j);
        }
        mine.compute_score(x, y, mat.size1());
        result(i,j) = mine.mic();
        result(j,i) = result(i,j);  // 填充下三角部分
    }
}

// 将结果存储在3列的矩阵中
matrix<double, row_major> final_result(mat.size2(), 3);
for (size_t i = 0; i < mat.size2(); i++) {
    for (size_t j = 0; j < 3; j++) {
        if (j == 0) {
            final_result(i, j) = i;
        } else if (j == 1) {
            final_result(i, j) = i + 1;
        } else {
            final_result(i, j) = result(i, i + 1);
        }
    }
}

return final_result;

}

修改下面的c++代码实现将结果存在3列的矩阵中matrixdouble row_major calc_micconst matrixdouble row_major& mat int threads 	当样本量小于6个时终止程序	ifmatsize16		stdcerr error the number of data rows should be greater than 5 stdendl

原文地址: https://www.cveoy.top/t/topic/iraM 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录