用C语言做一个三类的fisher分类器
时间: 2024-01-30 08:02:34 浏览: 27
以下是一个简单的三类Fisher分类器的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 3 // 三类
#define M 2 // 二维特征向量
double** allocate_matrix(int rows, int cols) {
double** matrix = (double**) malloc(rows * sizeof(double*));
for (int i = 0; i < rows; i++) {
matrix[i] = (double*) malloc(cols * sizeof(double));
}
return matrix;
}
void free_matrix(double** matrix, int rows) {
for (int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
}
void print_matrix(double** matrix, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%f\t", matrix[i][j]);
}
printf("\n");
}
}
void transpose(double** matrix, int rows, int cols, double** result) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[j][i] = matrix[i][j];
}
}
}
void multiply(double** matrix1, int rows1, int cols1, double** matrix2, int rows2, int cols2, double** result) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0.0;
for (int k = 0; k < cols1; k++) {
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
}
void subtract(double** matrix1, double** matrix2, int rows, int cols, double** result) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}
double** inverse(double** matrix, int n) {
double** inverse = allocate_matrix(n, n);
double det = 1.0 / (matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]);
inverse[0][0] = det * matrix[1][1];
inverse[0][1] = det * -matrix[0][1];
inverse[1][0] = det * -matrix[1][0];
inverse[1][1] = det * matrix[0][0];
return inverse;
}
double** compute_mean(double** matrix, int rows, int cols) {
double** mean = allocate_matrix(1, cols);
for (int j = 0; j < cols; j++) {
mean[0][j] = 0.0;
for (int i = 0; i < rows; i++) {
mean[0][j] += matrix[i][j];
}
mean[0][j] /= rows;
}
return mean;
}
double** compute_covariance(double** matrix, int rows, int cols) {
double** covariance = allocate_matrix(cols, cols);
double** mean = compute_mean(matrix, rows, cols);
double** centered = allocate_matrix(rows, cols);
subtract(matrix, mean, rows, cols, centered);
double** transpose_centered = allocate_matrix(cols, rows);
transpose(centered, rows, cols, transpose_centered);
multiply(transpose_centered, cols, rows, centered, rows, cols, covariance);
for (int i = 0; i < cols; i++) {
for (int j = 0; j < cols; j++) {
covariance[i][j] /= rows - 1;
}
}
free_matrix(mean, 1);
free_matrix(centered, rows);
free_matrix(transpose_centered, cols);
return covariance;
}
double** compute_fisher_coefficients(double** matrix1, int rows1, double** matrix2, int rows2, double** matrix3, int rows3) {
double** mean1 = compute_mean(matrix1, rows1, M);
double** mean2 = compute_mean(matrix2, rows2, M);
double** mean3 = compute_mean(matrix3, rows3, M);
double** covariance1 = compute_covariance(matrix1, rows1, M);
double** covariance2 = compute_covariance(matrix2, rows2, M);
double** covariance3 = compute_covariance(matrix3, rows3, M);
double** pooled_covariance = allocate_matrix(M, M);
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
pooled_covariance[i][j] = covariance1[i][j] + covariance2[i][j] + covariance3[i][j];
}
}
for (int i = 0; i < M; i++) {
for (int j = 0; j < M; j++) {
pooled_covariance[i][j] /= (rows1 + rows2 + rows3) - N;
}
}
double** inverse_pooled_covariance = inverse(pooled_covariance, M);
double** difference1 = allocate_matrix(1, M);
subtract(mean1, mean2, 1, M, difference1);
double** difference2 = allocate_matrix(1, M);
subtract(mean2, mean3, 1, M, difference2);
double** coefficients = allocate_matrix(M, N - 1);
multiply(difference1, 1, M, inverse_pooled_covariance, M, M, coefficients);
transpose(coefficients, M, N - 1, coefficients);
multiply(difference2, 1, M, inverse_pooled_covariance, M, M, &coefficients[N - 2]);
free_matrix(mean1, 1);
free_matrix(mean2, 1);
free_matrix(mean3, 1);
free_matrix(covariance1, M);
free_matrix(covariance2, M);
free_matrix(covariance3, M);
free_matrix(pooled_covariance, M);
free_matrix(inverse_pooled_covariance, M);
free_matrix(difference1, 1);
free_matrix(difference2, 1);
return coefficients;
}
int classify(double** coefficients, double* sample) {
double max_score = -INFINITY;
int max_class = -1;
for (int i = 0; i < N - 1; i++) {
double score = 0.0;
for (int j = 0; j < M; j++) {
score += coefficients[i][j] * sample[j];
}
if (score > max_score) {
max_score = score;
max_class = i;
}
}
return max_class;
}
int main() {
double** matrix1 = allocate_matrix(3, 2);
matrix1[0][0] = 1.0;
matrix1[0][1] = 2.0;
matrix1[1][0] = 2.0;
matrix1[1][1] = 3.0;
matrix1[2][0] = 3.0;
matrix1[2][1] = 4.0;
double** matrix2 = allocate_matrix(3, 2);
matrix2[0][0] = 4.0;
matrix2[0][1] = 5.0;
matrix2[1][0] = 5.0;
matrix2[1][1] = 6.0;
matrix2[2][0] = 6.0;
matrix2[2][1] = 7.0;
double** matrix3 = allocate_matrix(3, 2);
matrix3[0][0] = 7.0;
matrix3[0][1] = 8.0;
matrix3[1][0] = 8.0;
matrix3[1][1] = 9.0;
matrix3[2][0] = 9.0;
matrix3[2][1] = 10.0;
double** coefficients = compute_fisher_coefficients(matrix1, 3, matrix2, 3, matrix3, 3);
double sample1[2] = {0.5, 1.5};
int class1 = classify(coefficients, sample1);
printf("Sample (%f, %f) belongs to class %d\n", sample1[0], sample1[1], class1);
double sample2[2] = {5.5, 6.5};
int class2 = classify(coefficients, sample2);
printf("Sample (%f, %f) belongs to class %d\n", sample2[0], sample2[1], class2);
double sample3[2] = {8.5, 9.5};
int class3 = classify(coefficients, sample3);
printf("Sample (%f, %f) belongs to class %d\n", sample3[0], sample3[1], class3);
free_matrix(matrix1, 3);
free_matrix(matrix2, 3);
free_matrix(matrix3, 3);
free_matrix(coefficients, N - 1);
return 0;
}
```
在这个例子中,我们使用了三个由二维特征向量组成的类别,使用Fisher分类器对新的样本进行分类。在`compute_fisher_coefficients`函数中,我们计算了Fisher系数,然后在`classify`函数中使用它们来对新的样本进行分类。在这个例子中,我们使用了一些硬编码的样本,但是你可以将其替换为任何其他的数据集。
相关推荐
![](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)
![](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)