资源简介
实现了感知机的python代码,有例子有图形
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Sat Oct 29 09:23:29 2016
@author: lanlandetian
“““
from numpy import *
import matplotlib.pyplot as plt
#path=‘E:\\机器学习实战源代码\\机器学习实战源代码\\machinelearninginaction\\Ch06\\‘
def loadDataSet(filename):
numFeat=len(open(filename).readline().split())
dataMat=[];labelMat=[]
fr=open(filename)
for line in fr.readlines():
lineArr=[]
lineArr.append(1.0)
curLine=line.strip().split()
for i in range(0numFeat-1):
lineArr.append(float(curLine[i]))
dataMat.append(lineArr)
labelMat.append(float(curLine[-1]))
return dataMatlabelMat
def stocGradAscent(dataMatInclassLabelsnumIter=30):
dataMatrix=array(dataMatIn)
mn=shape(dataMatrix)
alpha=0.001
weights=zeros(n)
for j in range(numIter):
numErr=0
for i in range(m):
h=sum(dataMatrix[i]*weights)
if h*classLabels[i]<=0:
numErr+=1
weights=weights+alpha*(classLabels[i]*dataMatrix[i])
if numErr==0:
break
return weights
def stocGradAscent1(dataMatInclassLabelsnumIter=30):
dataMatrix=array(dataMatIn)
mn=shape(dataMatrix)
weights=zeros(n)
for j in range(numIter):
dataIndex=list(range(m))
for i in range(m):
alpha=4/(1.0+i+j)+0.01
randIndex=int(random.uniform(0len(dataIndex)))
h=sum(weights*dataMatrix[i])
if h*classLabels[i]<=0:
weights=weights+alpha*(classLabels[i]*dataMatrix[i])
del(dataIndex[randIndex])
return weights
def classifyVector(dataMatweights):
dataMat=array(dataMat)
mn=shape(dataMat)
result=[]
for i in range(m):
y=sum(dataMat[i]*weights)
result.append(y)
return result
def plotBestFit(dataMatlabelMatweights):
dataArr=array(dataMat);
n=shape(dataArr)[0];
xcord1=[];ycord1=[];
xcord2=[];ycord2=[];
for i in range(n):
if labelMat[i]==1:
xcord1.append(dataArr[i1]);ycord1.append(dataArr[i2]);
else:
xcord2.append(dataArr[i1]);ycord2.append(dataArr[i2]);
fig=plt.figure();
ax=fig.add_subplot(111);
ax.scatter(xcord1ycord1s=30c=‘red‘marker=‘s‘);
ax.scatter(xcord2ycord2s=30c=‘green‘);
# x=arange(-33.00.1); #x从-3到3 步长为0.1
x=dataArr[:1]
y=(-weights[0]-weights[1]*x)/weights[2];
ax.plot(xy);
plt.xlabel(‘X1‘);plt.ylabel(‘X2‘);
plt.show();
if __name__==‘__main__‘:
dataArrlabelMat=loadDataSet(path+‘testSet.txt‘)
weights=stocGradAscent(dataArrlabelMat)
# weights=stocGradAscent1(dataArrlabelMat)
print(weights)
plotBestFit(dataArrlabelMatweights)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 2911 2016-10-30 20:43 感知机Python实现\perceptron.py
文件 2208 2010-11-04 14:13 感知机Python实现\testSet.txt
目录 0 2016-10-30 20:43 感知机Python实现
----------- --------- ---------- ----- ----
5119 3
相关资源
- Tensorflow-BiLSTM分类
- python 实现将TXT文件内容逐行存到EXC
- python 打开并计算两幅dicom图像感兴趣
- python 决策树代码
- 银行ATM系统(Python实现)
- pygame实现的贪吃蛇游戏RetroSnaker.py
- Python文件
- QT文件转换成Python的自动化工具*.ui转
- fcntl模块 win
- python爬虫爬取企业详细信息
- Kruskal算法python实现
- 蚁群算法的python代码
- 最小二乘法python代码,不用库函数
- sm3 python encode
- openopc for python 3.x
- 张正友相机标定Python代码
- Python调用QQ微信截图
- python图像裁剪
- 海明校验 python源代码 海明码
- mod_wsgi.so
- Python 强大的图论和网络研究工具 ne
- python从入门到精通视频60集全
- wheel 安装包
- Python程序设计与算法基础教程源代码
- python数据处理csv->图表
- 微博评论Python代码实现
- Python最小距离法
- 五子棋AI python实现
- requests 中文文档
- 手机Python图形界面教程
评论
共有 条评论