用pycham实现,用KNN进行水果分类
时间: 2024-09-27 11:09:05 浏览: 52
在Python中,我们可以使用sklearn库中的KNeighborsClassifier(K近邻算法)来进行水果分类,特别是当数据集较小并且特征相对直观时,KNN是一个简单而有效的选择。以下是基本步骤:
1. **导入所需库**:
```python
import numpy as np
from sklearn.datasets import load_fruits
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
```
2. **加载水果数据集**:
```python
fruits = load_fruits()
X = fruits.data
y = fruits.target
```
这里我们假设`load_fruits()`函数是从sklearn.datasets模块加载了预处理好的数据。
3. **数据预处理**:
- 分割训练集和测试集
- 可能需要标准化数据,以便于KNN算法处理数值特征的尺度差异
```python
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
```
4. **创建并训练KNN模型**:
```python
knn = KNeighborsClassifier(n_neighbors=5) # 选择邻居数,如5代表最近的5个样本影响预测结果
knn.fit(X_train_scaled, y_train)
```
5. **做预测并评估模型**:
```python
y_pred = knn.predict(X_test_scaled)
accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy}")
```
阅读全文