only size-1 arrays can be converted to Python scalars
python版本:3.6.5
opencv版本:3.2.0
使用的jupyter notebook
源码如下:
import cv2
import numpy as np
import matplotlib.pyplot as plt %matplotlib inline # Feature set containing (x,y) values of 25 known/training data
trainData = np.random.randint(0,100,(25,2)).astype(np.float32) # Labels each one either Red or Blue with numbers 0 and 1
responses = np.random.randint(0,2,(25,1)).astype(np.float32) # Take Red families and plot them
red = trainData[responses.ravel()==0]
plt.scatter(red[:,0],red[:,1],80,'r','^') # Take Blue families and plot them
blue = trainData[responses.ravel()==1]
plt.scatter(blue[:,0],blue[:,1],80,'b','s') # plt.show() newcomer = np.random.randint(0,100,(1,2)).astype(np.float32)
plt.scatter(newcomer[:,0],newcomer[:,1],80,'g','o') knn = cv2.ml.KNearest_create() knn.train(trainData,responses)
ret, results, neighbours ,dist = knn.findNearest(newcomer, 3) print ("result: ", results,"\n")
print ("neighbours: ", neighbours,"\n")
print ("distance: ", dist) plt.show()
错误如下:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-b1c5cdca5e57> in <module>()
20 knn = cv2.ml.KNearest_create()
21
---> 22 knn.train(trainData,responses)
23 ret, results, neighbours ,dist = knn.findNearest(newcomer, 3)
24 TypeError: only size-1 arrays can be converted to Python scalars
错误原因:
传递的参数错误
train函数中第二个参数应该是数组的布局方式
应该根据传入数组的形式填cv2.ml.ROW_SAMPLE或者cv2.ml.COL_SAMPLE
例如我的就是
knn.train(trainData,cv2.ml.ROW_SAMPLE,responses)
参考地址:https://stackoverflow.com/questions/33975695/type-error-only-length-1-arrays-can-be-converted-to-python-scalars
only size-1 arrays can be converted to Python scalars的更多相关文章
- moviepy音视频剪辑:AudioClip帧处理时报TypeError: only size-1 arrays can be converted to Python scalar错
☞ ░ 前往老猿Python博文目录 ░ 一.环境 操作系统:win7 64位 moviepy:1.0.3 numpy:1.19.0 Python:3.7.2 二.应用代码及报错信息 程序代码 if ...
- TypeError: only integer scalar arrays can be converted to a scalar index
TypeError: only integer scalar arrays can be converted to a scalar index 觉得有用的话,欢迎一起讨论相互学习~Follow Me ...
- 【LeetCode】952. Largest Component Size by Common Factor 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetco ...
- 【LeetCode】1460. 通过翻转子数组使两个数组相等 Make Two Arrays Equal by Reversing Sub-arrays (Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 判断排序后是否相等 统计字符出现次数 日期 题目地址: ...
- OpenCV之Python学习笔记
OpenCV之Python学习笔记 直都在用Python+OpenCV做一些算法的原型.本来想留下发布一些文章的,可是整理一下就有点无奈了,都是写零散不成系统的小片段.现在看 到一本国外的新书< ...
- OpenCV计算机视觉学习(9)——图像直方图 & 直方图均衡化
如果需要处理的原图及代码,请移步小编的GitHub地址 传送门:请点击我 如果点击有误:https://github.com/LeBron-Jian/ComputerVisionPractice 1, ...
- numpy常用知识点备忘
常用函数 a.max(axis=0) a.max(axis=1) a.argmax(axis=1) : 每列的最大值(在行方向找最大值).每行的最大值(在列方向找对大致).最大值的坐标 sum()求和 ...
- OpenCV Python教程(3、直方图的计算与显示)
转载请详细注明原作者及出处,谢谢! 本篇文章介绍如何用OpenCV Python来计算直方图,并简略介绍用NumPy和Matplotlib计算和绘制直方图 直方图的背景知识.用途什么的就直接略过去了. ...
- python笔记——遇到一些报错
1.TypeError: data type not understood File "C:\Users\81476\PycharmProjects\untitled1\k-临近算法\kNN ...
随机推荐
- 十一、yield生成器
1.对比range 和 xrange 的区别 >>> print range() [, , , , , , , , , ] >>> print xrange() x ...
- VS2013 EF6连接MySQL步骤
1.安装MySql的VS插件(版本请下载最新版)mysql-for-visualstudio-1.2.3.msihttp://cdn.mysql.com/Downloads/MySQL-for-Vis ...
- LCD驱动程序之层次分析
1.回顾字符设备驱动程序: 字符设备驱动编写的一般步骤: 1)主设备号 2)构造file_operations结构体体 .open = xxxx .read = xxxxx 3)register_ch ...
- nginx docker 命令: command not found
1. ps: command not found 使用如下命令安装 apt-get update && apt-get -y install procps 2. vim: comman ...
- 04-C#笔记-数据类型转化
支持强制类型转换. 常用的转化函数如下: 1 ToBoolean如果可能的话,把类型转换为布尔型. 2 ToByte把类型转换为字节类型. 3 ToChar如果可能的话,把类型转换为单个 Unicod ...
- Comet OJ 夏季欢乐赛 Gree的心房
Comet OJ 夏季欢乐赛 Gree的心房 题目传送门 题目描述 据说每一个走进Gree哥哥心房的小姑娘都没有能够再走出来-- 我们将Gree哥哥的心房抽象成一个n \times mn×m的地图,初 ...
- 刷题记录and日记
- 并发设计模式:Immutability模式
多个线程同时读写同一共享变量存在并发问题,其中的必要条件之一就是 读写 ,如果没有写,只存在读,是不会存在并发问题的. 如果让一个共享变量只有读操作,没有写操作,如此则可以解决并发问题.该理论的具体实 ...
- html规范思维导图(仅限于自己)
- IntelliJ IDEA 插件推荐
1.GenerateAllSetter 自动生成类set方法 2.GsonFormat 根据JSON创建实体 3.Lombok plugin 简化代码 4. .ignore 忽略git提交文件 5.A ...