def map_data(data_map, axis_num): data_map = np.max(data_map, axis=axis_num) data_map -= data_map.min() data_map = data_map / data_map.max() data_map = np.array(data_map * 255, dtype=np.uint8) return data_map 翻译这段代码
时间: 2024-04-27 19:23:09 浏览: 15
这段代码的作用是将输入的二维数组或多维数组的每个元素映射到0到255之间的整数,并将其作为输出返回。
具体实现步骤如下:
1. 第一个参数data_map是一个二维或多维数组,表示待映射的数据。
2. 第二个参数axis_num表示在哪个轴上求最大值,比如axis_num=0表示在第0维上求最大值,返回一个由每列最大值组成的一维数组。axis_num=1表示在第1维上求最大值,返回一个由每行最大值组成的一维数组。
3. 对于返回的一维数组,我们用np.max函数求得最大值,然后用这个最大值来对原数组进行归一化,即data_map = data_map / data_map.max()。
4. 将0~1之间的数值映射到0~255之间的整数,即data_map = np.array(data_map * 255, dtype=np.uint8)。
5. 返回映射后的数组data_map。
相关问题
def map_data(data_map, axis_num): data_map = np.max(data_map, axis=axis_num) data_map -= data_map.min() data_map = data_map / data_map.max() data_map = np.array(data_map * 255, dtype=np.uint8) return data_map Eigon实现
以下是Eigen实现:
```
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
using namespace std;
MatrixXi map_data(MatrixXi data_map, int axis_num) {
int height = data_map.rows();
int width = data_map.cols();
// reduce the data_map along the given axis_num
VectorXi max_vals(width);
if (axis_num == 0) {
max_vals = data_map.rowwise().maxCoeff();
} else if (axis_num == 1) {
max_vals = data_map.colwise().maxCoeff();
}
// normalize the result
int min_val = max_vals.minCoeff();
int max_val = max_vals.maxCoeff();
MatrixXi result = (data_map.array() - min_val) * 255 / (max_val - min_val);
return result;
}
int main() {
// example usage
MatrixXi data_map(3, 3);
data_map << 1, 2, 3,
4, 5, 6,
7, 8, 9;
MatrixXi result = map_data(data_map, 0);
cout << result << endl;
return 0;
}
```
需要注意的是,Eigen中的Matrix类模板参数中有两个参数,第一个参数是矩阵元素的类型,第二个参数是矩阵的行数和列数,用RowMajor表示按行存储,用ColMajor表示按列存储。在这个实现中,我们用MatrixXi表示矩阵元素为整型,且按列存储。
使用C++ eigen库翻译以下python代码import pandas as pd import numpy as np import time import random def main(): eigen_list = [] data = [[1,2,4,7,6,3],[3,20,1,2,5,4],[2,0,1,5,8,6],[5,3,3,6,3,2],[6,0,5,2,19,3],[5,2,4,9,6,3]] g_csi_corr = np.cov(data, rowvar=True) #print(g_csi_corr) eigenvalue, featurevector = np.linalg.eigh(g_csi_corr) print("eigenvalue:",eigenvalue) eigen_list.append(max(eigenvalue)) #以下代码验证求解csi阈值 eigen_list.append(1.22) eigen_list.append(-54.21) eigen_list.append(8.44) eigen_list.append(-27.83) eigen_list.append(33.12) #eigen_list.append(40.29) print(eigen_list) eigen_a1 = np.array(eigen_list) num1 = len(eigen_list) eigen_a2 = eigen_a1.reshape((-1, num1)) eigen_a3 = np.std(eigen_a2, axis=0) eigen_a4 = eigen_a3.tolist() k = (0.016 - 0.014) / (max(eigen_a4) - min(eigen_a4)) eigen_a5 = [0.014 + k * (i - min(eigen_a4)) for i in eigen_a4] tri_threshold = np.mean(eigen_a5)
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
std::vector<double> eigen_list;
MatrixXd data(6, 6);
data << 1, 2, 4, 7, 6, 3,
3, 20, 1, 2, 5, 4,
2, 0, 1, 5, 8, 6,
5, 3, 3, 6, 3, 2,
6, 0, 5, 2, 19, 3,
5, 2, 4, 9, 6, 3;
MatrixXd g_csi_corr = data.transpose() * data / 6.0;
EigenSolver<MatrixXd> es(g_csi_corr);
VectorXd eigenvalue = es.eigenvalues().real();
std::cout << "eigenvalue: " << eigenvalue.transpose() << std::endl;
eigen_list.push_back(eigenvalue.maxCoeff());
eigen_list.push_back(1.22);
eigen_list.push_back(-54.21);
eigen_list.push_back(8.44);
eigen_list.push_back(-27.83);
eigen_list.push_back(33.12);
//eigen_list.push_back(40.29);
std::cout << "eigen_list: ";
for (std::vector<double>::iterator it = eigen_list.begin(); it != eigen_list.end(); ++it)
std::cout << *it << " ";
std::cout << std::endl;
int num1 = eigen_list.size();
MatrixXd eigen_a2 = Map<MatrixXd>(eigen_list.data(), num1, 1);
VectorXd eigen_a3 = eigen_a2.array().rowwise().mean().transpose();
VectorXd eigen_a4 = (eigen_a2 - eigen_a3.replicate(num1, 1)).array().abs().rowwise().mean().transpose();
double k = 0.002 / (eigen_a4.maxCoeff() - eigen_a4.minCoeff());
VectorXd eigen_a5 = 0.014 + k * (eigen_a4.array() - eigen_a4.minCoeff());
double tri_threshold = eigen_a5.mean();
std::cout << "tri_threshold: " << tri_threshold << std::endl;
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)