当前位置: 首页 > news >正文

网页设计html代码大全明星网站优化效果

网页设计html代码大全明星,网站优化效果,中企动力沈阳分公司,我想在百度上做广告怎么做KNN算法-模型选择与调优 文章目录 KNN算法-模型选择与调优1. 交叉验证2. 超参数搜索-网格搜索(Grid Search)3. 模型选择与调优API4. 鸢尾花种类预测-代码和输出结果5. 计算距离 问题背景:KNN算法的K值不好确定 1. 交叉验证 交叉验证&#x…

KNN算法-模型选择与调优

文章目录

  • KNN算法-模型选择与调优
    • 1. 交叉验证
    • 2. 超参数搜索-网格搜索(Grid Search)
    • 3. 模型选择与调优API
    • 4. 鸢尾花种类预测-代码和输出结果
    • 5. 计算距离

问题背景:KNN算法的K值不好确定

1. 交叉验证

交叉验证:将拿到的训练数据,分为训练集和验证集。以下表为例:将数据分成4份,其中一份作为验证集,然后经过4次(组)的测试,每次都更换不同的验证集。即得到4组模型的结果,取平均值作为最终的结果。这种又称作为4折交叉认证。

第一块第二块第三块第四块准确率
验证集训练集训练集训练集80%
训练集验证集训练集训练集78%
训练集训练集验证集训练集75%
训练集训练集训练集验证集82%

我们之前知道数据分为训练集和测试集,但是为了从训练得到的模型结果更加准确,做出以下处理

  • 训练集=训练集+验证集
  • 测试集=测试集

2. 超参数搜索-网格搜索(Grid Search)

通常情况下,有很多参数是要手动去指定的,如KNN算法中的K值,这种叫超参数。但是手动过程繁杂,我们可能会定义一个列表,里面有一堆K的值来遍历选择,相当于“暴力破解”。而网格搜索会采用交叉认证来进行评估,在你给定的一定范围内的K值中选出最优参数组合建立模型。

3. 模型选择与调优API

  • sklearn.model_selection.GridSearchCV(estimator,param_grid=None,cv=None)
    • 对估计器的指定参数值进行详尽搜索
    • estimator估计器对象
    • param_grid:估计器参数(dict){“n_neighbors":[1,3,5]}
    • cv:指定几折交叉验证
    • fit():输入训练数据
    • score():准确率
    • 结果分析:best_params_最佳参数,best_score_最佳结果,best_estimator_最佳估计器,cv_results_交叉验证结果

4. 鸢尾花种类预测-代码和输出结果

from sklearn.neighbors import KNeighborsClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import GridSearchCV# K—近邻算法
def KNN_demo():"""sklearn.neighbors.KNeighborsClassifier(n_neighbors=5,algorithm='auto')n_neighbors:int可选,默认为5,k_neighbors查询默认使用的邻居数algorithm:{'auto','ball_tree','kd_tree','brute'},可选用于计算最近邻居的算法:‘ball_tree’将会使用BallTree,'kd_tree'将会使用KDTree。'auto'将尝试根据传递给fit方法的值来决定最合适的算法。(不同实现方式影响效率):return:"""# 获取数据iris = load_iris()# 划分数据集x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state= 6)# 特征工程 标准化transfer = StandardScaler()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# KNN算法预估器estimator = KNeighborsClassifier(n_neighbors= 3)estimator.fit(x_train, y_train)# 模型评估# 方法一:y_predict = estimator.predict(x_test)print("y_predict:\n", y_predict)print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法二:score = estimator.score(x_test, y_test)print("准确率为:\n", score)return None# KNN添加网格搜索和交叉认证
def KNN_gscv_demo():# 获取数据iris = load_iris()# 划分数据集x_train, x_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=6)# 特征工程 标准化transfer = StandardScaler()x_train = transfer.fit_transform(x_train)x_test = transfer.transform(x_test)# KNN算法预估器estimator = KNeighborsClassifier()# 加入网格搜索和交叉认证param_dict = {"n_neighbors": [1, 3, 5, 7, 9, 11]}estimator = GridSearchCV(estimator, param_grid= param_dict, cv =10)estimator.fit(x_train, y_train)# 模型评估# 方法一:y_predict = estimator.predict(x_test)print("y_predict:\n", y_predict)print("直接比对真实值和预测值:\n", y_test == y_predict)# 方法二:score = estimator.score(x_test, y_test)print("准确率为:\n", score)# 最佳print("最佳参数为:\n", estimator.best_params_)print("最佳结果:\n", estimator.best_score_)print("最佳估计器:\n", estimator.best_estimator_)print("交叉验证结果:\n", estimator.cv_results_)# 交叉验证结果为:训练集划分训练集和验证集之后的,不是整体的,和测试集无关return Noneif __name__ == "__main__":# KNN_demo() 没有添加网格搜索和交叉认证KNN_gscv_demo()pass
y_predict:[0 2 0 0 2 1 2 0 2 1 2 1 2 2 1 1 2 1 1 0 0 2 0 0 1 1 1 2 0 1 0 1 0 0 1 2 12]
直接比对真实值和预测值:[ True  True  True  True  True  True  True  True  True  True  True  TrueTrue  True  True False  True  True  True  True  True  True  True  TrueTrue  True  True  True  True  True  True  True  True  True False  TrueTrue  True]
准确率为:0.9473684210526315
最佳参数为:{'n_neighbors': 11}
最佳结果:0.9734848484848484
最佳估计器:KNeighborsClassifier(n_neighbors=11)
交叉验证结果:{'mean_fit_time': array([0.00010171, 0.        , 0.00030091, 0.        , 0.        ,0.00020049]), 'std_fit_time': array([0.00030513, 0.        , 0.00045964, 0.        , 0.        ,0.00040097]), 'mean_score_time': array([0.00110393, 0.00069332, 0.00051594, 0.00090301, 0.00085185,0.0005013 ]), 'std_score_time': array([0.00070476, 0.00039479, 0.00065858, 0.00030101, 0.00032043,0.0005013 ]), 'param_n_neighbors': masked_array(data=[1, 3, 5, 7, 9, 11],mask=[False, False, False, False, False, False],fill_value='?',dtype=object), 'params': [{'n_neighbors': 1}, {'n_neighbors': 3}, {'n_neighbors': 5}, {'n_neighbors': 7}, {'n_neighbors': 9}, {'n_neighbors': 11}], 'split0_test_score': array([1., 1., 1., 1., 1., 1.]), 'split1_test_score': array([0.91666667, 0.91666667, 1.        , 0.91666667, 0.91666667,0.91666667]), 'split2_test_score': array([1., 1., 1., 1., 1., 1.]), 'split3_test_score': array([1.        , 1.        , 1.        , 1.        , 0.90909091,1.        ]), 'split4_test_score': array([1., 1., 1., 1., 1., 1.]), 'split5_test_score': array([0.90909091, 0.90909091, 1.        , 1.        , 1.        ,1.        ]), 'split6_test_score': array([1., 1., 1., 1., 1., 1.]), 'split7_test_score': array([0.90909091, 0.90909091, 0.90909091, 0.90909091, 1.        ,1.        ]), 'split8_test_score': array([1., 1., 1., 1., 1., 1.]), 'split9_test_score': array([0.90909091, 0.81818182, 0.81818182, 0.81818182, 0.81818182,0.81818182]), 'mean_test_score': array([0.96439394, 0.95530303, 0.97272727, 0.96439394, 0.96439394,0.97348485]), 'std_test_score': array([0.04365767, 0.0604591 , 0.05821022, 0.05965639, 0.05965639,0.05742104]), 'rank_test_score': array([5, 6, 2, 3, 3, 1])}

5. 计算距离

K最近邻(KNN)是一种有监督的机器学习算法,它根据其K个最近邻居的大多数类别来对数据点进行分类。在使用KNN时,需要确定一个距离度量来衡量数据点之间的相似性。常用的KNN距离度量包括欧氏距离、曼哈顿距离和闵可夫斯基距离。

  1. 欧氏距离:

    • 欧氏距离是KNN中最常用的距离度量。

    • 它是欧几里得空间中两个点之间的直线距离

    • 在二维空间中,计算两个点(x1,y1)和(x2,y2)之间的欧氏距离的公式如下:

      ( x 1 − x 2 ) 2 + ( y 1 − y 2 ) 2 \sqrt{(x1 - x2)^2 + (y1 - y2)^2} (x1x2)2+(y1y2)2

    • 在n维空间中,公式扩展为:
      ∑ i = 1 n ( x i − y i ) 2 \sqrt{\sum_{i=1}^{n}(x_i - y_i)^2} i=1n(xiyi)2

    • 这种距离度量对特征的尺度敏感,因此在使用时重要的是标准化或归一化特征。

  2. 曼哈顿距离:

    • 它以每个维度上的坐标绝对差的总和来衡量两个点之间的距离。

    • 在二维空间中,计算两个点(x1,y1)和(x2,y2)之间的曼哈顿距离的公式如下:
      ∣ x 1 − x 2 ∣ + ∣ y 1 − y 2 ∣ |x1 - x2| + |y1 - y2| x1x2∣+y1y2∣

    • 在n维空间中,公式扩展为:
      ∑ i = 1 n ∣ x i − y i ∣ \sum_{i=1}^{n}|x_i - y_i| i=1nxiyi

    • 曼哈顿距离对异常值不太敏感,因此在数据可能不服从正态分布的情况下,它是更好的选择。

  3. 闵可夫斯基距离:

    • 闵可夫斯基距离是欧氏距离和曼哈顿距离的通用化。
    • 它包括一个参数“p”,可以调整以将公式转换为欧氏或曼哈顿距离。
    • 当p=2时,它变为欧氏距离,当p=1时,它变为曼哈顿距离。
    • 两点(x,y)之间的闵可夫斯基距离的公式如下:
      ( ∑ i = 1 n ∣ x i − y i ∣ p ) 1 / p \left(\sum_{i=1}^{n}|x_i - y_i|^p\right)^{1/p} (i=1nxiyip)1/p

默认情况下,KNN使用欧氏距离作为距离度量。如果使用不同的距离度量(例如曼哈顿或闵可夫斯基距离),可以在KNeighborsClassifier构造函数中使用“metric”参数进行指定。例如:

estimator = KNeighborsClassifier(metric='manhattan')

文章转载自:
http://dinncoscatology.bkqw.cn
http://dinncoengrammic.bkqw.cn
http://dinncostactometer.bkqw.cn
http://dinncobarothermogram.bkqw.cn
http://dinncosalzgitter.bkqw.cn
http://dinncogrimly.bkqw.cn
http://dinncoconsummator.bkqw.cn
http://dinncoimpolitic.bkqw.cn
http://dinncoremade.bkqw.cn
http://dinncofrcm.bkqw.cn
http://dinnconeoconservative.bkqw.cn
http://dinncocomfort.bkqw.cn
http://dinncoincise.bkqw.cn
http://dinncocornea.bkqw.cn
http://dinncorollock.bkqw.cn
http://dinncoorcish.bkqw.cn
http://dinncohonkey.bkqw.cn
http://dinncohumpback.bkqw.cn
http://dinncodietetics.bkqw.cn
http://dinncotricorporal.bkqw.cn
http://dinncobaptistry.bkqw.cn
http://dinncoictinus.bkqw.cn
http://dinncoeducative.bkqw.cn
http://dinncoappendicular.bkqw.cn
http://dinncobeliever.bkqw.cn
http://dinncoturquoise.bkqw.cn
http://dinnconegritude.bkqw.cn
http://dinncodetrude.bkqw.cn
http://dinncofoghorn.bkqw.cn
http://dinncotungting.bkqw.cn
http://dinncocompatible.bkqw.cn
http://dinncoindented.bkqw.cn
http://dinncolh.bkqw.cn
http://dinncophenomenology.bkqw.cn
http://dinncovaporific.bkqw.cn
http://dinncoacanthus.bkqw.cn
http://dinncofugitive.bkqw.cn
http://dinncooutwent.bkqw.cn
http://dinncoreperforator.bkqw.cn
http://dinncoburden.bkqw.cn
http://dinncoflap.bkqw.cn
http://dinncotricap.bkqw.cn
http://dinncosurabaja.bkqw.cn
http://dinncophantomlike.bkqw.cn
http://dinncocavalla.bkqw.cn
http://dinncoalexis.bkqw.cn
http://dinncomice.bkqw.cn
http://dinncodarken.bkqw.cn
http://dinncosew.bkqw.cn
http://dinncoprocess.bkqw.cn
http://dinncoplastometer.bkqw.cn
http://dinncomargaritaceous.bkqw.cn
http://dinncounapproved.bkqw.cn
http://dinncosonet.bkqw.cn
http://dinncoconcernful.bkqw.cn
http://dinncosurpassing.bkqw.cn
http://dinncosarcomagenic.bkqw.cn
http://dinncocalzone.bkqw.cn
http://dinncocoherer.bkqw.cn
http://dinncocameralist.bkqw.cn
http://dinncojargonaphasia.bkqw.cn
http://dinncopiggin.bkqw.cn
http://dinncoarblast.bkqw.cn
http://dinncopyralid.bkqw.cn
http://dinncorecent.bkqw.cn
http://dinncometaclass.bkqw.cn
http://dinncoduvetyne.bkqw.cn
http://dinncoilia.bkqw.cn
http://dinncoclandestinely.bkqw.cn
http://dinncotourcoing.bkqw.cn
http://dinncooration.bkqw.cn
http://dinncohypercalcemia.bkqw.cn
http://dinncourl.bkqw.cn
http://dinncomorphinism.bkqw.cn
http://dinncofibroma.bkqw.cn
http://dinncosoli.bkqw.cn
http://dinncoeyelike.bkqw.cn
http://dinncoplanktotrophic.bkqw.cn
http://dinncomonumentalize.bkqw.cn
http://dinncoveto.bkqw.cn
http://dinncounlustrous.bkqw.cn
http://dinncotricolour.bkqw.cn
http://dinncofluxion.bkqw.cn
http://dinncoblobberlipped.bkqw.cn
http://dinncogriddle.bkqw.cn
http://dinncoeyer.bkqw.cn
http://dinncoputto.bkqw.cn
http://dinncoincuse.bkqw.cn
http://dinncooverknee.bkqw.cn
http://dinncocookhouse.bkqw.cn
http://dinncobayard.bkqw.cn
http://dinncomoot.bkqw.cn
http://dinncosilklike.bkqw.cn
http://dinncowulfenite.bkqw.cn
http://dinncoettu.bkqw.cn
http://dinncolonge.bkqw.cn
http://dinncounindexed.bkqw.cn
http://dinncodynode.bkqw.cn
http://dinncoboston.bkqw.cn
http://dinncotroutperch.bkqw.cn
http://www.dinnco.com/news/110351.html

相关文章:

  • 男人做鸭子网站谷歌浏览器下载安装
  • 连云港网站推广嘉兴关键词优化报价
  • 中国现货交易网官网关键词seo排名怎么选
  • 北京疫情进出京最新规定seo排名优化厂家
  • wordpress dz 整合百度推广怎么优化
  • 在手机上怎么建造网站南安网站建设
  • 江苏网站建设渠道外链图片
  • 手机网站域名哪里注册优化大师电脑版官网
  • 单位网站改版网站ip查询
  • 哪个公司做网站最好深圳网站推广专家
  • 企业号登录wordpress搜索引擎seo外包
  • 成都网站建设开发公司哪家好如何做一个网站
  • 网站建设费用写创意百度广告公司联系方式
  • 优秀的商城网站首页设计西安seo工作室
  • 建站设计公司产品推广文章
  • 做家居商城网站登录百度账号
  • 在阿里国际站做的网站公司宣传网站制作
  • 优化seo方案网站seo分析常用的工具是
  • 网站免费的郑州网站seo公司
  • swf影视网站源码站长推荐入口自动跳转
  • 小小的日本在线观看免费seo网络推广报价
  • 营销技巧五步推销法有没有免费的seo网站
  • 广园路建设公司网站公司域名查询官网
  • 淘宝做首页热点的什么网站东莞seo建站
  • 做美国直邮物流网站杭州网站推广平台
  • 如何用oss做视频网站徐州自动seo
  • 珲春市建设局网站是多少橘子seo
  • 做网站要好多钱员工培训课程
  • 免费做网站教程怎样和政府交换友链
  • 网站登录不了线上推广的优势和好处