将下面的c++代码增加如下功能:读取带有行名和列名的数据矩阵然后用mine计算每列两两之间的mic系数最后输出结果为txt文件的对称矩阵:#include cstdlib#include cmath#include iostream#include cppminehusing namespace std;intmain int argc char argv int n = 7; double
#include
using namespace std;
int main (int argc, char **argv) { int n = 7; double x[] = {1.,2.,3.,4.,5.,6.,7.}; double y[] = {1.,2.,3.,4.,3.,2.,1.};
/* build the MINE object */ MINE mine(0.6, 15, EST_MIC_APPROX);
/* compute score */ mine.compute_score(x, y, n);
/* print MIC */ cout << "MIC: " << mine.mic() << "\n";
// Read data matrix from file ifstream inputFile("data.txt"); if (!inputFile) { cerr << "Error opening file." << endl; return 1; }
vector<vector
string line; getline(inputFile, line); // Read the first line (column names) stringstream ss(line); string colName; while (ss >> colName) { colNames.push_back(colName); }
while (getline(inputFile, line)) { stringstream ss(line); string rowName; ss >> rowName; rowNames.push_back(rowName);
vector<double> row;
double value;
while (ss >> value) {
row.push_back(value);
}
data.push_back(row);
}
inputFile.close();
// Calculate MIC for each pair of columns
int numCols = data[0].size();
vector<vector
for (int i = 0; i < numCols; i++) { for (int j = i + 1; j < numCols; j++) { double* x = &data[0][i]; double* y = &data[0][j]; int n = data.size();
mine.compute_score(x, y, n);
micMatrix[i][j] = mine.mic();
}
}
// Write MIC matrix to file ofstream outputFile("mic_matrix.txt"); if (!outputFile) { cerr << "Error opening file." << endl; return 1; }
for (int i = 0; i < numCols; i++) { for (int j = 0; j < numCols; j++) { outputFile << micMatrix[i][j] << " "; } outputFile << endl; }
outputFile.close();
return 0; }
原文地址: https://www.cveoy.top/t/topic/inDq 著作权归作者所有。请勿转载和采集!