• 大小: 2KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-19
  • 语言: Python
  • 标签: 感知机  Python  

资源简介

实现了感知机的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


评论

共有 条评论