成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Machine Learning-KNN

wind3110991 / 568人閱讀

摘要:一定義二個人理解其實簡單理解就是通過計算新加入點與附近個點的距離,然后尋找到距離最近的個點,進行占比統(tǒng)計,找到個點中數(shù)量占比最高的,那么新加入的樣本,它的就是頻數(shù)最高的三實踐語言歐拉距離樣本繪圖計算距離歐拉距離求出和相

一、定義

url:https://en.wikipedia.org/wiki...

In pattern recognition, the k-nearest neighbors algorithm (k-NN) is a non-parametric method used for classification and regression.[1] In both cases, the input consists of the k closest training examples in the feature space. The output depends on whether k-NN is used for classification or regression:

In k-NN classification, the output is a class membership. An object is classified by a majority vote of its neighbors, with the object being assigned to the class most common among its k nearest neighbors (k is a positive integer, typically small). If k = 1, then the object is simply assigned to the class of that single nearest neighbor.

In k-NN regression, the output is the property value for the object. This value is the average of the values of its k nearest neighbors.

二、個人理解
其實簡單理解就是:通過計算新加入點與附近K個點的距離,然后尋找到距離最近的K個點,進行占比統(tǒng)計,找到k個點中數(shù)量占比最高的target,那么新加入的樣本,它的target就是頻數(shù)最高的target
三、實踐
語言:python3

歐拉距離:

# -*- coding: utf-8 -*-
"""
Created on Sat Mar 17 11:17:18 2018

@author: yangzinan
"""

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from math import sqrt

from collections import Counter 


# 樣本
x= [
              [3.393533211,2.331273381],
              [3.110073483,1.781539638],
              [1.343808831,3.368360954],
              [3.582294042,4.679179110],
              [2.280362439,2.866990263],
              [7.423436942,4.696522875],
              [5.745051997,3.533989803],
              [9.172168622,2.511101045],
              [7.792783481,3.424088941],
              [7.939820817,0.791637231]
            ]

y= [0,0,0,0,0,1,1,1,1,1]


x_train = np.array(x)
y_train = np.array(y)
              

# 繪圖
plt.scatter(x_train[y_train==0,0],x_train[y_train==0,1],color="red")
plt.scatter(x_train[y_train==1,0],x_train[y_train==1,1],color="green")



x_point = np.array([8.093607318,3.365731514])


plt.scatter(x_point[0],x_point[1],color="blue")
plt.show()


#計算距離 歐拉距離

distances = []

for d in x_train:
    # 求出和x相差的距離
    d_sum = sqrt(np.sum(((d-x)**2)))
    distances.append(d_sum)

print(distances)

#求出最近的點
#按照從小到大的順序,得到下標
nearest = np.argsort(distances)

#指定應該求出的個數(shù)
k = 3
topK_y = []

#求出前K個target
for i in nearest[:k]:
    topK_y.append(y_train[i])


#得到頻數(shù)最高的target,那么新加入點target 就是頻數(shù)最高的
predict_y = Counter(topK_y).most_common(1)[0][0]

print(predict_y)

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉載請注明本文地址:http://m.hztianpu.com/yun/44639.html

相關文章

  • 如何在云服務提供商的平臺上使用Docker Machine

    摘要:大家好,今天我們來了解如何使用在各種云服務提供商的平臺上部署。是一個可以幫助我們在自己的電腦云服務提供商的平臺以及我們數(shù)據(jù)中心的機器上創(chuàng)建機器的應用程序。支持幾個流行的云平臺,如及其它等等,所以我們可以在不同的平臺使用相同的接口來部署。 大家好,今天我們來了解如何使用Docker Machine在各種云服務提供商的平臺上部署Docker。Docker Machine是一個可以幫助我們在自己的...

    noONE 評論0 收藏0
  • python設計模式-狀態(tài)模式

    摘要:很明顯,有有分錢沒有分錢售出糖果糖果售罄四個狀態(tài),同時也對應四個動作投入分錢,退回分錢,轉動曲柄和發(fā)放糖果。狀態(tài)模式的類圖如下狀態(tài)模式是將多個行為封裝在狀態(tài)對象中,的行為隨時可委托到其中一個狀態(tài)中。 問題:有一個糖果公司需要設計一個糖果售賣機,控制流程如下圖,需要怎么實現(xiàn)? showImg(http://media.gusibi.mobi/5aI8Zy9kkfNI8jzRA8VYMG...

    A Loity 評論0 收藏0
  • [譯] 如何在云服務提供商的平臺上使用Docker Machine

    摘要:大家好,今天我們來了解如何使用在各種云服務提供商的平臺上部署。是一個可以幫助我們在自己的電腦云服務提供商的平臺以及我們數(shù)據(jù)中心的機器上創(chuàng)建機器的應用程序。支持幾個流行的云平臺,如及其它等等,所以我們可以在不同的平臺使用相同的接口來部署。 大家好,今天我們來了解如何使用Docker Machine在各種云服務提供商的平臺上部署Docker。Docker Machine是一個可以幫助我們在...

    call_me_R 評論0 收藏0

發(fā)表評論

0條評論

wind3110991

|高級講師

TA的文章

閱讀更多
最新活動
閱讀需要支付1元查看
<