演算法小白的第一次嘗試 PLA(感知機演算法)實現

2021-09-12 01:49:49 字數 1879 閱讀 4952

import breeze.linalg.densevector

import org.apache.log4j.

import org.apache.spark.ml.feature.labeledpoint

import org.apache.spark.ml.linalg.vectors

import org.apache.spark.sql.sparksession

import scala.collection.mutable.arraybuffer

/** * @author xiaotangbao

* @date 2019/3/6 10:13

* @version 1.0

* the original form of perceptron learning algorithm,for linearly separable data sets,

* the original form of the perceptron algorithm converges. after a finite number of iterations,

* a hyperplane can be found, and the data set is completely correctly divided.

*/object pla

//待分類點為四維,定義初始的w,b,ata

var w =

densevector

(3.0

,0.8

,1.8

,2.4

) var b =

20.0

var ata =

0.2//基於sgd迭代求解最優w,b,ata

var outflag =

true

while

(outflag)

else}}

//所有測點都完全分類正確,則退出

var num =

0for

(i<

-0 until xi.length)

if(num == xi.length) outflag =

false

}println

("訓練結束"

)println

(w)println

(b)}

//判斷是否被正確分類

def judge

(w:densevector[double]

,b:double,xi:labeledpoint)

:boolean =}--

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

----

--當前糾正:x51

當前糾正:x51

當前糾正:x51

當前糾正:x1

當前糾正:x52

當前糾正:x1

當前糾正:x58

當前糾正:x1

當前糾正:x58

當前糾正:x1

當前糾正:x58

當前糾正:x1

當前糾正:x99

當前糾正:x1

訓練結束

densevector(-

1.7200000000000006,-

0.1399999999999999,-

3.760000000000001

,0.40000000000000024

)19.400000000000002

第一次嘗試

python是資料結構筆記一 引入 以一道題來展示 如果a b c 1000,且a 2 b 2 c 2 a,b,c為自然數 求a,b,c可能的組合 解析 列舉法 思路 a 0.b 0.c 先讓其中的乙個數a去不變,b變化,當b確定乙個數的時候c去變化 也就是利用迴圈巢狀的方式 乙個個的試,那麼就有多...

第一次嘗試

第一次嘗試呢 你好!這是你第一次使用markdown編輯器所展示的歡迎頁。如果你想學習如何使用markdown編輯器,可以仔細閱讀這篇文章,了解一下markdown的基本語法知識。全新的介面設計,將會帶來全新的寫作體驗 在創作中心設定你喜愛的 高亮樣式,markdown將 片顯示選擇的高亮樣式進行展...

SqlLocalDB第一次嘗試

一 介紹 sqllocaldb是vs安裝時附帶的資料庫軟體,相當於精簡版的sql express。二 使用 vs版本為2015,預設安裝位置為 c program files microsoft sql server 130 tools binn 如果能看到sqllocaldb,說明安裝正確,開啟c...