Python – 简易识别

有如下类似训练数据多组

主要目的是通过某种算法实现计算机可以学习认识数字0
比如有如下算法(暂时不合理)

# -*- TomYuan
import numpy as np
import os
import matplotlib.pyplot as plt
'''
0000000000000000000000000
0000000000000000000000000
0000000001111111000000000
0000000010000000100000000
0000000100000000010000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000001000000000001000000
0000000100000000010000000
0000000010000000100000000
0000000001111111000000000
0000000000000000000000000
0000000000000000000000000
'''
def readData(fileName):
    #define array (25 * 22) {m * n}
    m = 25
    n = 22
    matrix = [0] * n
    fr = open(fileName)
    l = 0;
    for line in fr.readlines():
        #put all number into array
        matrix[l] = [0] * (len(line) - 1)
        for i in range(1, len(line)):
            # print (line[i - 1])
            matrix[l][i - 1] = line[i - 1]
        l = l + 1
    return matrix

def calMatrix(matrix):
    ret = 0;
    for i in range(1, 22):
        for j in range(1, 25):
            if matrix[i][j] == '1':
                ret = ret + 1
                #
                if matrix[i - 1][j] == '1':
                    ret = ret + 2
                else:
                    ret = ret - 2
                #
                if matrix[i - 1][j - 1] == '1':
                    ret = ret + 2
                else:
                    ret = ret - 2
                #
                if matrix[i][j - 1] == '1':
                    ret = ret + 2
                else:
                    ret = ret - 2
                ##
                #
                if matrix[i + 1][j] == '1':
                    ret = ret + 2
                else:
                    ret = ret - 2
                #
                if matrix[i + 1][j + 1] == '1':
                    ret = ret + 2
                else:
                    ret = ret - 2
                #
                if matrix[i][j + 1] == '1':
                    ret = ret + 2
                else:
                    ret = ret - 2
                ##
                #
                if matrix[i - 1][j + 1] == '1':
                    ret = ret + 2
                else:
                    ret = ret - 2
                #
                if matrix[i + 1][j - 1] == '1':
                    ret = ret + 2
                else:
                    ret = ret - 2
    return ret
print(calMatrix(readData("data5.set")))

我们来思考下如何实现一个非常简单的聚类,类似这样

欢迎留言

avatar
  Subscribe  
Notify of