用c++写一个鸢尾花k近邻算法
时间: 2024-10-18 10:11:25 浏览: 29
在C++中,使用鸢尾花数据集(Iris dataset)实现K近邻(K-Nearest Neighbors, KNN)算法的基本步骤如下:
1. **导入所需库**:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
```
2. **读取数据**:
```cpp
struct IrisSpecies {
double sepal_length;
double sepal_width;
double petal_length;
double petal_width;
std::string species; // 标签,如"Iris-setosa"等
};
std::vector<IrisSpecies> load_data(const std::string& filename) {
std::ifstream file(filename);
std::vector<IrisSpecies> data;
if (file.is_open()) {
double row[4];
while (file >> row[0] >> row[1] >> row[2] >> row[3]) {
data.push_back({row[0], row[1], row[2], row[3], "Iris-" + std::to_string(row[3])});
}
file.close();
} else {
std::cerr << "Error opening file." << std::endl;
}
return data;
}
```
3. **计算距离函数**(这里用欧氏距离为例):
```cpp
double euclidean_distance(IrisSpecies a, IrisSpecies b) {
double sum = 0.0;
for (int i = 0; i < 4; ++i)
sum += pow(a[i] - b[i], 2);
return sqrt(sum);
}
```
4. **KNN分类**:
```cpp
std::string knn_predict(const std::vector<IrisSpecies>& train_data, const IrisSpecies& test_data, int k) {
std::vector<std::pair<double, std::string>> distances(train_data.size(), {std::numeric_limits<double>::max(), ""});
// 计算测试样本与所有训练样本的距离并排序
for (const auto& d : train_data) {
double dist = euclidean_distance(test_data, d);
distances.emplace_back(dist, d.species);
std::sort(distances.begin() + 1, distances.end());
}
// 返回前k个最近邻居中出现最多的类别
std::string most_common = "";
int max_count = 0;
for (int i = 0; i < k; ++i) {
int count = std::count_if(distances.begin(), distances.begin() + k, [d = distances[i].second](const auto& p) { return p.second == d; });
if (count > max_count) {
max_count = count;
most_common = d;
}
}
return most_common;
}
```
5. **主程序示例**:
```cpp
int main() {
std::vector<IrisSpecies> train_data = load_data("iris.data");
// ... (实际应用中划分训练集和测试集)
IrisSpecies test_data; // 假设已经填充好测试样本
int k = 3; // 指定K值
std::string prediction = knn_predict(train_data, test_data, k);
std::cout << "Predicted species: " << prediction << std::endl;
return 0;
}
```
阅读全文