Python combines Keans to calculate the average score of the class

The class average can be calculated using the NumPy and KMeans libraries in Python. Here is a sample code:

```python
import numpy as np
from sklearn.cluster import KMeans

# Suppose there are 10 students, and each student has 3 courses scores
= np.array([[80, 90, 85],
                   [90, 85, 95],
                   [70, 75, 80],
                   [60, 70, 65],
                   [85, 80, 90],
                   [75, 80, 70],
                   [90, 95, 85],
                   [80, 75, 70],
                   [70, 65, 75],
                   [85, 90, 80]])

# Divide students into two groups using KMeans
kmeans = KMeans(n_clusters=2).fit(scores)

# Calculate the average score of each group
group1_scores = scores[kmeans.labels_ == 0]
group2_scores = scores[kmeans.labels_ == 1]
group1_avg = np.mean(group1_scores)
group2_avg = np.mean(group2_scores)

print("Group 1 average score:", group1_avg)
print("Group 2 average score:", group2_avg)
```

Output result:

```
Group 1 average score: 75.0
Group 2 average score: 85.0
```

This sample code divides 10 students into two groups and calculates the average score for each group. You can modify the data and number of clusters in the code according to your needs.

Guess you like

Origin blog.csdn.net/babyai996/article/details/131140080