不使用深度学习的水果识别python代码实现
时间: 2023-08-25 14:07:35 浏览: 77
以下是一个使用机器学习算法的水果识别 Python 代码示例。它不使用深度学习,而是使用传统的机器学习算法(K近邻算法)。
```python
import pandas as pd
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.neighbors import KNeighborsClassifier
# 加载数据集
fruits = pd.read_table('fruits.txt')
# 构建特征向量
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(fruits['name'])
# 构建分类器
knn = KNeighborsClassifier(n_neighbors=3)
knn.fit(X, fruits['label'])
# 测试分类器
test_fruit = ['长条形,黄色,有小黑点']
test_vector = vectorizer.transform(test_fruit)
prediction = knn.predict(test_vector)
print(prediction)
```
这段代码首先加载了一个水果数据集,然后使用 CountVectorizer 将每个水果的名称转换为特征向量。接着,使用 K 近邻算法构建分类器,并将特征向量和标签作为输入。最后,代码使用测试数据进行预测,并输出预测结果。
请注意,这段代码仅用于演示目的。实际应用中,您可能需要更多的数据预处理、特征提取和调整参数等步骤。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)