• 大小:
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-11-20
  • 语言: Python
  • 标签: python3  ml  

资源简介

《机器学习实战》Python3代码

资源截图

代码片段和文件信息

# -*- coding: utf-8 -*-
“““
Created on Tue Jul  3 12:06:45 2018

@author: Administrator
“““

from os import listdir
from numpy import *
from KNN_algr import *

def img2vec(filename):
    ‘‘‘this is to...将32X32的图像转化为1X1024的行向量‘‘‘
    returnvec = zeros((11024))
    
    with open(filename) as fp:
        for i in range(32):
            line = fp.readline()
            for j in range(32):
                returnvec[0 32*i + j] = int(line[j])
    # returnVEC按32进位,j代表每位的32个元素    
    return returnvec

def HandWritingTest(train_dirtest_dir):
    labels = []
    File_list = listdir(train_dir)
    # 将目录内的文件按名字放入列表,使用函数解析为数字
    m = len(File_list)
    train_mat = zeros((m1024))
    for i in range(m):
        fname = File_list[i]
        fstr = fname.split(‘.‘)[0]
        fnumber = int(fstr.split(‘_‘)[0])
    # 比如‘digits/testDigits/0_13.txt‘,被拆分为013txt
    # 此处0即为标签数字   
        labels.append(fnumber)
        train_mat[i:] = img2vec(‘%s/%s‘ % (train_dirfname))
    # labels is label_vec,同之前的KNN代码相同,存储标签
    
    test_File_list = listdir(test_dir)
    error = 0.0
    test_m = len(test_File_list)
    for i in range(test_m):
        fname = test_File_list[i]
        fstr = fname.split(‘.‘)[0]
        fnumber = int(fstr.split(‘_‘)[0])
        vec_test = img2vec(‘digits/testDigits/%s‘ % fname)
        Result = classify_KNN(vec_test train_mat labels 3)
        print(“the classifier came with: %d the real answer is :%d “ \
                 % (Result fnumber))
        if(Result != fnumber) : error += 1
    # 这部分和Test模块相同,直接copy过来就好
    print(“the accuracy is %f | the error_rate is %f “ % \
          (1- (float(error) /float(test_m))(float(error) /float(test_m))))

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\
     文件          66  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\.gitattributes
     文件        2881  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\README.md
     目录           0  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\
     文件        1867  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\Digit_recog.py
     文件         634  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\Feature_scaling.py
     文件        1075  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\KNN_algr.py
     文件         989  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\Parse_data.py
     文件        1468  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\TestData.py
     文件           1  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\__init__.py
     文件         873  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\__main__.py
     文件       26067  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\datingTestSet2.txt
     目录           0  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\
     目录           0  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_0.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_1.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_10.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_11.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_12.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_13.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_14.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_15.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_16.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_17.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_18.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_19.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_2.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_20.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_21.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_22.txt
     文件        1056  2018-08-19 03:49  Machine-Learning-in-Action-Python3-master\ch01-KNN\digits\testDigits\0_23.txt
............此处省略5878个文件信息

评论

共有 条评论