Machine learning for openCV 学习笔记 day1
来源:互联网 发布:js点击按钮弹出对话框 编辑:程序博客网 时间:2024/06/05 09:20
第一部分:基础内容介绍
在OpenCV中,图片的输入是32-bit的浮点型数值(0-1之间),或者是8-bit的整型数值(0-255之间)。
1.Numpy
我们使用功能强大的Numpy工具包去进行矩阵的相关操作
import numpy as np
2. scikit-learn
Python中我们使用scikit-learn来下载一些外网的数据库。我们也可以直接在 www.mldata.org 上面下载数据库
from sklearn import datasetsmnist = datasets.fetch_mldata('MNIST original')
3.Matplotlib
Matplotlib是基于Numpy矩阵搭建的多平台数据可视化库,我们通过Matplotlib来观察我们的数据。通过先加载matplotlib,然后再从中加载matplot.pyplot,我们常用的是matplotlib.pyplot去画图。
import matplotlib as mplimport matploblib.pyplot as plt通过linspace()函数构建一个坐标系,linspace(0,,10,100)表示坐标范围为0-10,取函数100个点
import numpy as npx=np.linspace(0,10,100)plt.plot(x,np.sin(x))
#保存图像:
plt.savefig('name')
知识点总结:
Numpy进行数据操作,scikit-learn进行数据获取,Matplotlib对数据可视化
import numpy as npfrom sklearn import datasetsimport matplotlib.pyplot as pltdigits = datasets.load_digits()print(digits.data.shape)print(digits.images.shape)plt.figure(figsize=(14,4))for image_index in range(10): subplot_index=image_index+1 plt.subplot(2,5,subplot_index) plt.imshow(digits.images[image_index,:,:],cmap='gray')plt.show() #我使用的是Pycharm,不加plt.show()是不能正常显示出图像的结果就是:
第二部分:监督学习
首先在OpenCV中设置一个机器学习的模型遵循以下步骤:
初始化——>设置参数——>训练模型——>预测标签——>模型打分
而我们模型得分分为:
a) 准确率(accuracy)
b) 精准率(precision)
c)召回率(recall)
1.对准确率的理解
import numpy as np
'''random.seed(42)函数限定了随机数种子为42,不同人编辑的程序,随机数生成的肯定不一样若限定的随机数种子相同,则输出
的随机数也是相同的,方便得到一样的结果'''
np.random.seed(42) y_true = np.random.randint(0,2,size=5)print(y_true)y_pred=np.ones(5,dtype=np.int32)print(y_pred)print(np.sum(y_pred==y_true)/len(y_true))from sklearn import metricsprint(metrics.accuracy_score(y_pred,y_true))
如上,当我们随机生成一个[0,1,0,0,0]的真值矩阵,而预测矩阵为[1,1,1,1,1],准确率就为0.2,我们可以用sklearn中的metrics模块来直接估算模型最终的准确率,运行以后可以看出,结果均为0.2。
2.对精准率和召回率的理解
(图片来自维基百科)
import numpy as np
#限定输出的随机数np.random.seed(42) y_ture=np.random.randint(0,2,size=5)y_pred=np.ones(5,dtype=np.int32)truly_a_positive = (y_ture == 1)predicted_a_positive = (y_pred == 1)# You though it was 1, and it actually was a 1true_positive = np.sum(predicted_a_positive*truly_a_positive)print(true_positive)# You though it was 1, and it actually was a 0false_positive = np.sum((y_pred == 1)*(y_ture == 0))print(false_positive)# You though it was 0, and it actually was a 1false_negative = np.sum((y_pred == 0)*(y_ture == 1))print(false_negative)# You though it was 0, and it actually was a 0true_negative = np.sum((y_pred == 0)*(y_ture == 0))print(true_negative)#To make sure we did rightaccuracy = np.sum(true_positive+true_negative)/len(y_ture)print(accuracy)# Precisionprecision = np.sum(true_positive)/np.sum(true_negative+true_positive)print(precision)# Recallrecall = true_positive/(true_positive+false_negative)print(recall)from sklearn import metricsprint(metrics.precision_score(y_ture,y_pred))以上输出依次为:
3.回归问题
以上1和2点是针对于分类问题,分类问题的特点是输出都是离散的,而回归(regression)问题输出则是连续的,这时上面提到的a,b,c点就不起作用了。我们用sklearn中新的平方差模块(mean_squared_error)和 R2 score (r2_score)来进行模型打分。以sin(x)这样一个连续函数距离说明:
import numpy as npx=np.linspace(0,10,100)
y_ture = np.sin(x) + np.random.rand(x.size) - 0.5y_pred = np.sin(x)import matplotlib.pyplot as pltplt.style.use('ggplot')plt.figure(figsize=(10,6))plt.plot(x,y_pred, linewidth=4, label = 'model')plt.plot(x,y_ture,'o',label = 'data')plt.xlabel('x')plt.ylabel('y')plt.legend(loc='lower left')plt.show()结果输出为:
(因为x是随机取的,所以下面算出的方差,以及图像都是不一样的)
1.为了衡量我们模型预测的精准性,我们需要计算模型的均方误差(mean square error)有两种方法:
方法一:
mse = np.mean((y_ture-y_pred)**2)print(mse)
方法二:
from sklearn import metrics# verify our mathprint(metrics.mean_squared_error(y_pred,y_ture))
输出结果:
可以看见计算结果是一样的。
2. 第二种常见的方法就是计算数据的方差来观察预测数据与真实数据的离散程度。我们也是使用常规计算和scikit-learn工具包两种方法进行验证。
方法一:
fvu = np.var(y_ture - y_pred) / np.var(y_ture)print(fvu)print(1.0-fvu)
方法二:
# verify our mathprint(metrics.explained_variance_score(y_ture,y_pred))
输出结果:
此处我的输出结果方差是:0.8420....
3.R2分数
方法一:
r2 = 1.0 - mse / np.var(y_ture)print(r2)
方法二:
print(metrics.r2_score(y_ture,np.mean(y_ture)*np.ones_like(y_ture)))
方法一、二输出的均是:0.8394...
当数据拟合度越高,R2分数越接近1。数据是随机挑选,所以结果和我的不一致也是正常的。
- Machine learning for openCV 学习笔记 day1
- Machine learning for OpenCV 学习笔记 day2
- Machine learning for OpenCV 学习笔记 day4
- Machine learning for OpenCV 学习笔记 day5
- Machine learning for OpenCV 学习笔记 day6
- 《Neural Networks for Machine Learning》学习笔记
- Machine Learning 学习笔记
- machine learning 学习笔记
- Mechine learning for OpenCV 学习笔记 day3
- 机器学习笔记-advice for applying machine learning
- Stanford 机器学习笔记 Week6 Advice for Applying Machine Learning
- 【Stanford机器学习笔记】8-Advice for Applying Machine Learning
- 机器学习课程 Neural Netword for Machine Learning笔记
- Machine Learning 课程学习笔记
- 【Machine Learning】SVM学习笔记
- 学习笔记-machine learning foundaton2
- machine learning 学习笔记<一>
- machine learning学习笔记<二>
- oracleday02(启动流程 认证方式)
- JAVA中关于链表的操作和基本算法
- poj-2806-公共子序列-C语言-动态规划
- 快排思想 求第(前)k大数
- oracle之union用法实例
- Machine learning for openCV 学习笔记 day1
- 解决AndroidStudio出现Error:svn: E155004: Run 'svn cleanup' to remove locks
- (读书笔记)《玩着玩着就能成为PPT高手》——反面教材
- Android 使用WebView加载含有echarts的页面,截图不显示的解决方式
- 设计模式中接口回调的意义
- ajax提交多个参数时,如果需要在url上拼接的写法
- 艾奇全能视频转换器 v4.10.1102免费版
- window下的native链接虚拟机上的mysql数据库
- Java迭代器移除元素注意的问题