机器学习之感知器完整Python代码实现

本文介绍了一个基于Python实现的感知器学习算法,并通过一个具体的二分类任务进行了验证。该算法从读取数据开始,经过训练过程,最终评估了模型的准确性。

import pandas as pd
import numpy as np
import cv2
import random
import time

from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score

class Perceptron(object):

def __init__(self):
    self.learning_step = 0.00001
    self.max_iteration = 5000

def predict_(self, x):
    wx = sum([self.w[j] * x[j] for j in range(len(self.w))])
    return int(wx > 0)

def train(self, features, labels):
    self.w = [0.0] * (len(features[0]) + 1)

    correct_count = 0
    time = 0

    while time < self.max_iteration:
        index = random.randint(0, len(labels) - 1)
        x = list(features[index])
        x.append(1.0)
        y = 2 * labels[index] - 1
        wx = sum([self.w[j] * x[j] for j in range(len(self.w))])

        if wx * y > 0:
            correct_count += 1
            if correct_count > self.max_iteration:
                break
            continue

        for i in range(len(self.w)):
            self.w[i] += self.learning_step * (y * x[i])

def predict(self,features):
    labels = []
    for feature in features:
        x = list(feature)
        x.append(1)
        labels.append(self.predict_(x))
    return labels

if name == ‘main‘:

print( 'Start read data')

time_1 = time.time()

raw_data = pd.read_csv('D:/tjxlx/xlx/data/train_binary.csv', header=0)
data = raw_data.values

imgs = data[0::, 1::]
labels = data[::, 0]

# 选取 2/3 数据作为训练集, 1/3 数据作为测试集
train_features, test_features, train_labels, test_labels = train_test_split(
    imgs, labels, test_size=0.33, random_state=23323)
# print train_features.shape
# print train_features.shape

time_2 = time.time()
print ('read data cost ', time_2 - time_1, ' second', '\n')

print ('Start training')
p = Perceptron()
p.train(train_features, train_labels)

time_3 = time.time()
print ('training cost ', time_3 - time_2, ' second', '\n')

print ('Start predicting')
test_predict = p.predict(test_features)
time_4 = time.time()
print ('predicting cost ', time_4 - time_3, ' second', '\n')

score = accuracy_score(test_labels, test_predict)
print ("The accruacy socre is ", score)

Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type “copyright”, “credits” or “license()” for more information.

================== RESTART: D:\tjxlx\xlx\perceptron\感知器.py ==================

Warning (from warnings module):
File “D:\li\anaconda\lib\site-packages\sklearn\cross_validation.py”, line 41
“This module will be removed in 0.20.”, DeprecationWarning)
DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
Start read data
read data cost 12.797999858856201 second

Start training
training cost 1.4560000896453857 second

Start predicting
predicting cost 3.135999917984009 second

The accruacy socre is 0.9859307359307359

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值