当前位置:网站首页>Program simulation perceptron algorithm (gradient descent method sklearn.linear_ Perception method in model)
Program simulation perceptron algorithm (gradient descent method sklearn.linear_ Perception method in model)
2020-11-09 16:06:00 【Come on, get up】
Chapter two The original form simulation of the perceptron
- I'm Xiaobai ; This article code reprint address, there is a note at the end of the article ; Most of the notes are written by themselves , If you have any questions, please give me more advice .
- Simulated perceptron algorithm by gradient descent . The data comes from sklearn.datasets The classic dataset in the middle of .
import numpy as np
import pandas as pd
# Import dataset load_iris.
# The first four columns are calyx length , Calyx width , Petal length , Petal width, etc 4 Attributes used to identify iris ,
from sklearn.datasets import load_iris
import matplotlib.pyplot as plot
# load data
iris = load_iris()
# Constructors DataFrame(data,index,columns),data For data ,index Index rows ,columns Index columns
# Construct data structures
df = pd.DataFrame(data=iris.data, columns=iris.feature_names)
# stay df A new column has been added to : Name label The data of target, That's the type
# The first 5 In the category of iris ( Include Setosa,Versicolour,Virginica Three types of ).
# That is, by determining the calyx length , Calyx width , Petal length , The size of the petal width is used to identify the type of iris .
df['label'] = iris.target
df.columns = ['sepal length', 'sepal width', 'petal length', 'petal width', 'label']
# Print out label The number of column values . And column name 、 type
# print(df.label.value_counts())
# Create array ,
# The first parameter :100 It means take 0-99 That's ok , The former 100 That's ok ; Empathy 100: After 100 That's ok .
# The second parameter ,0,1 For the first and second columns ,-1 It means the last column
data = np.array(df.iloc[:100, [0, 1, -1]])
# Array cutting ,X Cut out the last column ;y Cut only the last column
X, y = data[:, :-1], data[:, -1]
# Data collation , take y Central African 1 Of is changed to -1
y = np.array([1 if i == 1 else -1 for i in y])
# The data are linearly separable , Two categories of data
# Here is a linear equation of one variable
class Model:
def __init__(self):
# w It starts with the same number of independent variables (1,1) front
self.w = np.ones(len(data[0]) - 1, dtype=np.float32)
self.b = 0
# The learning rate is set to 0.1
self.l_rate = 0.1
def sign(self, x, w, b):
# dot The function is a dot product , The difference in * ride .
# dot Matrix multiplication is the operation of matrix , One row by one column .
# * When you're doing matrix multiplication , It's the multiplication of the corresponding elements
y = np.dot(x, w) + b
return y
# Random gradient descent method
def fit(self, X_train, y_train):
is_wrong = False
while not is_wrong:
# Record the number of points of current classification errors , When the number of misclassification points returns to zero , That's the end of classification
wrong_count = 0
# d from 0-49 Traverse
for d in range(len(X_train)):
# there X For a row two column array
X = X_train[d]
y = y_train[d]
if y * self.sign(X, self.w, self.b) <= 0:
self.w = self.w + self.l_rate * np.dot(y, X)
self.b = self.b + self.l_rate * y
wrong_count += 1
if wrong_count == 0:
is_wrong = True
return 'Perceptron Model!'
def score(self):
pass
perceptron = Model()
# Yes perception Make a gradient descent
perceptron.fit(X, y)
print(perceptron.w)
x_points = np.linspace(4, 7, 10)
# The final fitting function is as follows :
# w1*x1 + w2*x2 + b = 0
# among x1 It's the one below x_points,x2 Namely y_
y_ = -(perceptron.w[0] * x_points + perceptron.b) / perceptron.w[1]
plot.plot(x_points, y_)
# scatter The two properties correspond to x,y.
# First draw the front 50 Calyx length and width of each point , The flower type is 0;
# And then draw 50-100, Now the flower type 1
plot.scatter(df[:50]['sepal length'], df[:50]['sepal width'], label='0')
plot.scatter(df[50:100]['sepal length'], df[50:100]['sepal width'], label='1')
# Abscissa name
plot.xlabel('sepal length')
# Ordinate name
plot.ylabel('sepal width')
plot.legend()
plot.show()
The result is shown in Fig.

2. sklearn A ready-made perceptron method is provided in , We can call
import sklearn
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.linear_model import Perceptron
iris = load_iris()
df = pd.DataFrame(iris.data, columns=iris.feature_names)
df['label'] = iris.target
# df.columns = [
# 'sepal length', 'sepal width', 'petal length', 'petal width', 'label'
# ]
data = np.array(df.iloc[:100, [0, 1, -1]])
X, y = data[:, :-1], data[:, -1]
y = np.array([1 if i == 1 else -1 for i in y])
clf = Perceptron(fit_intercept=True, # true Represents the estimated intercept
max_iter=1000, # The maximum number of training data
shuffle=True, # Whether to retrain after each training
tol=None) # If not set none Then the iteration will be less than 1e-3 end
clf.fit(X, y)
# Output after bonding w
print(clf.coef_)
# Output the intercept after fitting b
print(clf.intercept_)
# Canvas size
plt.figure(figsize=(10, 10))
# Chinese title
# plt.rcParams['font.sans-serif'] = ['SimHei']
# plt.rcParams['axes.unicode_minus'] = False
# plt.title(' Iris linear data example ')
plt.scatter(data[:50, 0], data[:50, 1], c='b', label='Iris-setosa',)
plt.scatter(data[50:100, 0], data[50:100, 1], c='orange', label='Iris-versicolor')
# Draw the wires of the sensor
x_ponits = np.arange(4, 8)
y_ = -(clf.coef_[0][0]*x_ponits + clf.intercept_)/clf.coef_[0][1]
plt.plot(x_ponits, y_)
# Other parts
plt.legend() # Show Legend
plt.grid(False) # Don't show grid
plt.xlabel('sepal length')
plt.ylabel('sepal width')
plt.legend()
plt.show()
The result is shown in Fig.

版权声明
本文为[Come on, get up]所创,转载请带上原文链接,感谢
边栏推荐
- 5 minutes get I use GitHub's 5-year summary of these operations!
- Colleague notes - small program entry point
- Restart the heap_ uaf_ hacknote
- Flink的安装和测试
- 微服务框架 Go-Micro 集成 Nacos 实战之服务注册与发现
- AutoCAD 2020 installation package & Installation Tutorial
- Kubernetes v1.19.3 kubeadm deployment notes (2)
- High quality defect analysis: let yourself write fewer bugs
- 使用art-Template模板获取天气预报信息
- 低功耗蓝牙单芯片为物联网助力
猜你喜欢

高质量的缺陷分析:让自己少写 bug

Programmers before and after buying a house, after reading has cried blind

cad教程 cad2016安装教程

Application of pull wire displacement sensor in slope cracks

Openyurt in depth interpretation: how to build kubernetes native cloud edge efficient collaborative network?

Embedded assembly in IOS

Toolkit Pro助力界面开发:缩短项目开发周期,快速实现具有现代功能区样式的GUI

H264Nalu头部解析

我叫Mongo,收了「查询基础篇」,值得你拥有

In depth analysis of the multi-user shopping mall system from search to create a profit point
随机推荐
.NET报表生成器Stimulsoft Reports.Net 发布最新版v2020.5!
Position promotion | intelligent multimedia group of Microsoft Asia research institute recruits computer vision algorithm Intern
Kubernetes V1.19.3 kubeadm 部署笔记(中)
Autocad2020 full version installation text course, registration activation cracking method
What kind of experience does a doctor have when he turns his secret love brother into a husband?
cad教程 cad2016安装教程
CAD2016软件安装教程
从一次需求改良漫谈php文件分片上传
使用Fastai开发和部署图像分类器应用
Cad2016 download autocad2016 download installation detailed tutorial CAD Download
5分钟GET我使用Github 5 年总结的这些骚操作!
毕业一年后接私活赚了10w,还拿了几家大厂offer!
CAD2016下载AutoCAD2016下载安装详细教程CAD下载
【分享】接口测试如何在post请求中传递文件
标梵IPFS矿机app软件开发软件 IPFSApp开发方法详解
寻找性能更优秀的不可变小字典
拉线式位移传感器在边坡裂缝中的作用
How to use Camtasia to make dynamic animation scene?
MES系统在行业应用里区别于传统式管理
Colleague notes - small program entry point