knn聚类算法python实例
时间: 2023-08-23 19:13:46 浏览: 120
KNN聚类算法是一种简单而有效的分类算法,可以用于对数据进行分类。下面是一个使用Python实现KNN分类算法的示例代码:
```python
# 导入所需的库
import numpy as np
from sklearn.neighbors import KNeighborsClassifier
# 创建训练数据集
X_train = np.array([[1, 2], [1, 4], [2, 2], [2, 3]])
y_train = np.array([0, 0, 1, 1])
# 创建测试数据集
X_test = np.array([[1, 3], [2, 4]])
# 创建KNN分类器对象
knn = KNeighborsClassifier(n_neighbors=3)
# 训练模型
knn.fit(X_train, y_train)
# 进行预测
y_pred = knn.predict(X_test)
# 打印预测结果
print(y_pred)
```
在这个示例中,我们首先导入了所需的库,然后创建了一个训练数据集和一个测试数据集。接下来,我们创建了一个KNN分类器对象,并使用训练数据集对其进行训练。最后,我们使用测试数据集进行预测,并打印出预测结果。
请注意,这只是一个简单的示例,实际使用中可能需要根据具体情况进行参数调整和数据预处理。[2] [3]
相关问题
python knn聚类算法实例
以下是一个简单的 Python KNN 聚类算法的实例:
```python
import numpy as np
import pandas as pd
# 加载数据集
df = pd.read_csv('data.csv')
# 分离数据和标签
X = df.iloc[:, :-1].values
y = df.iloc[:, -1].values
# 将数据集划分为训练集和测试集
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
# 特征缩放
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
# 训练 KNN 模型
from sklearn.neighbors import KNeighborsClassifier
classifier = KNeighborsClassifier(n_neighbors=5)
classifier.fit(X_train, y_train)
# 预测测试集结果
y_pred = classifier.predict(X_test)
# 输出混淆矩阵和准确率
from sklearn.metrics import confusion_matrix, accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
print('Accuracy:', accuracy_score(y_test, y_pred))
```
这个实例中,我们首先加载了一个 CSV 文件作为我们的数据集,然后将数据分为特征和标签。接下来,我们将数据集分为训练集和测试集,并使用标准化技术对特征进行缩放。然后我们创建了一个 KNN 分类器,使用训练集进行训练。最后,我们用测试集进行预测,并输出混淆矩阵和准确率。
knn聚类的python代码
以下是使用Python实现KNN聚类的示例代码:
```python
import numpy as np
from collections import Counter
class KNN():
def __init__(self, k=5):
self.k = k
def fit(self, X, y):
self.X_train = X
self.y_train = y
def predict(self, X):
predicted_labels = [self._predict(x) for x in X]
return np.array(predicted_labels)
def _predict(self, x):
distances = [self._distance(x, x_train) for x_train in self.X_train]
k_indices = np.argsort(distances)[:self.k]
k_labels = [self.y_train[i] for i in k_indices]
most_common = Counter(k_labels).most_common(1)
return most_common[0][0]
def _distance(self, x1, x2):
return np.sqrt(np.sum((x1 - x2) ** 2))
```
使用示例:
```python
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
X, y = make_classification(n_samples=100, n_features=10, n_classes=2, random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
knn = KNN(k=5)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
```
阅读全文