knn算法实现鸢尾花分类C语言
时间: 2023-11-17 18:07:24 浏览: 188
KNN实现鸢尾花分类
以下是基于C语言实现的KNN算法鸢尾花分类代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define K 3 // 定义K值
// 定义鸢尾花的数据结构
typedef struct {
double sepallength; // 花萼长度
double sepalwidth; // 花萼宽度
double petallength; // 花瓣长度
double petalwidth; // 花瓣宽度
char class[20]; // 类别
} Iris;
// 读取数据集
void read_data(char* filename, Iris* dataset, int* n) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf("Open file %s failed!\n", filename);
exit(1);
}
char buf[1024];
int i = 0;
while (fgets(buf, 1024, fp)) {
sscanf(buf, "%lf,%lf,%lf,%lf,%s", &dataset[i].sepallength, &dataset[i].sepalwidth, \
&dataset[i].petallength, &dataset[i].petalwidth, dataset[i].class);
i++;
}
*n = i;
fclose(fp);
}
// 计算两点之间的距离
double distance(Iris* p, Iris* q) {
return sqrt(pow(p->sepallength - q->sepallength, 2) + pow(p->sepalwidth - q->sepalwidth, 2) + \
pow(p->petallength - q->petallength, 2) + pow(p->petalwidth - q->petalwidth, 2));
}
// 查找K个最近邻居
void find_k_neighbors(Iris* dataset, int n, Iris* test, Iris** neighbors) {
double dist;
double max_dist = 0.0;
int max_index = 0;
for (int i = 0; i < K; i++) {
neighbors[i] = &dataset[i];
dist = distance(neighbors[i], test);
if (dist > max_dist) {
max_dist = dist;
max_index = i;
}
}
for (int i = K; i < n; i++) {
dist = distance(&dataset[i], test);
if (dist < max_dist) {
neighbors[max_index] = &dataset[i];
max_dist = dist;
for (int j = 0; j < K; j++) {
if (distance(neighbors[j], test) > max_dist) {
max_dist = distance(neighbors[j], test);
max_index = j;
}
}
}
}
}
// 计算类别出现次数
char* get_class(Iris** neighbors) {
int class_count[3] = {0, 0, 0};
for (int i = 0; i < K; i++) {
if (strcmp(neighbors[i]->class, "Iris-setosa") == 0) {
class_count[0]++;
} else if (strcmp(neighbors[i]->class, "Iris-versicolor") == 0) {
class_count[1]++;
} else {
class_count[2]++;
}
}
int max_count = 0;
int max_index = 0;
for (int i = 0; i < 3; i++) {
if (class_count[i] > max_count) {
max_count = class_count[i];
max_index = i;
}
}
if (max_index == 0) {
return "Iris-setosa";
} else if (max_index == 1) {
return "Iris-versicolor";
} else {
return "Iris-virginica";
}
}
int main() {
Iris dataset[150]; // 数据集
Iris test_data; // 测试数据
Iris* neighbors[K]; // 最近邻居
int n; // 数据集的大小
read_data("iris.data", dataset, &n); // 读取数据集
// 手动输入测试数据
printf("Please input the test data (sepal length, sepal width, petal length, petal width):\n");
scanf("%lf%lf%lf%lf", &test_data.sepallength, &test_data.sepalwidth, &test_data.petallength, &test_data.petalwidth);
find_k_neighbors(dataset, n, &test_data, neighbors); // 查找最近邻居
printf("The class of the test data is %s.\n", get_class(neighbors)); // 输出分类结果
return 0;
}
```
注意:本代码中的数据集文件"iris.data"需要自行下载。
阅读全文