使用python语言编写一段k-means算法和人类健康关联系统代码
时间: 2023-06-09 13:03:14 浏览: 98
python实现的k-means算法
5星 · 资源好评率100%
好的,以下是使用 Python 编写的 K-means 算法和人类健康关联系统代码:
K-means 算法:
```python
import numpy as np
from sklearn.cluster import KMeans
X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
kmeans = KMeans(n_clusters=2, random_state=0).fit(X)
print(kmeans.labels_)
# Output: [1 1 1 0 0 0]
print(kmeans.predict([[0, 0], [4, 4]]))
# Output: [1 0]
```
人类健康关联系统代码:
```python
import pandas as pd
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
# Load the dataset into a pandas dataframe
health_data = pd.read_csv('health_data.csv')
# Divide the dataset into features and target variable
X = health_data.iloc[:, :-1]
y = health_data.iloc[:, -1]
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Fit the decision tree classifier to the training data
tree = DecisionTreeClassifier(max_depth=3, random_state=42)
tree.fit(X_train, y_train)
# Predict the labels for the testing data
y_pred = tree.predict(X_test)
# Evaluate the accuracy of the model
accuracy = tree.score(X_test, y_test)
print(f"Accuracy of the model: {accuracy}")
```
注意,以上代码只是示例代码。对于 K-means 算法,数据应该根据实际应用进行预处理。对于人类健康关联系统,需要将数据集中的特征和目标变量根据实际需求调整,并且该代码也需要进行适当的修改以适应实际应用场景。
阅读全文