C++读取鸢尾花数据集
时间: 2023-10-18 19:13:40 浏览: 82
读取鸢尾花数据集可以使用以下代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_LINE_LENGTH 1024
int main() {
FILE *fp;
char line[MAX_LINE_LENGTH];
float sepal_length, sepal_width, petal_length, petal_width;
int label;
fp = fopen("iris.data", "r");
if (fp == NULL) {
fprintf(stderr, "Error opening file.\n");
exit(1);
}
while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) {
sscanf(line, "%f,%f,%f,%f,%d", &sepal_length, &sepal_width, &petal_length, &petal_width, &label);
printf("%.1f, %.1f, %.1f, %.1f, %d\n", sepal_length, sepal_width, petal_length, petal_width, label);
}
fclose(fp);
return 0;
}
```
注意,此代码假设数据集文件名为 `iris.data`,每行包含四个用逗号分隔的浮点数和一个整数标签。可以根据实际情况修改代码。
阅读全文