读取iris数据集中的花萼长度数据
时间: 2023-04-03 15:04:03 浏览: 103
iris的花萼长度-数据集
可以使用以下代码读取iris数据集中的花萼长度数据:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 1024
#define NUM_FEATURES 4
#define NUM_SAMPLES 150
int main() {
FILE *fp;
char line[MAX_LINE_LENGTH];
float sepal_length[NUM_SAMPLES];
fp = fopen("iris.data", "r");
if (fp == NULL) {
printf("Failed to open file\n");
exit(1);
}
int i = 0;
while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) {
char *token = strtok(line, ",");
for (int j = 0; j < NUM_FEATURES; j++) {
if (j == 0) {
sepal_length[i] = atof(token);
}
token = strtok(NULL, ",");
}
i++;
}
fclose(fp);
// Print out the sepal length data
for (int i = 0; i < NUM_SAMPLES; i++) {
printf("%f\n", sepal_length[i]);
}
return 0;
}
```
阅读全文