RuntimeError:matplotlib不支持将生成器作为输入

时间:2019-07-05 13:55:36

标签: python-3.x neural-network fuzzy-logic

我正在使用模糊艺术算法将其应用于我的数据,并且在出现结果的情况下一切正常,但是在绘制结果时,解释器会说:RuntimeError:matplotlib不支持将生成器作为输入。

顺便说一句,我正在使用python作为编程语言,并使用matplotlib绘制结果。

import numpy as np
from sys import argv
import matplotlib.pyplot as plt
from random import shuffle

def scaledList(list):
    min = None
    max = None
    for i in range(len(list)):
        if min is None or min > list[i]:
            min = list[i]
        if max is None or max < list[i]:
            max = list[i]
    for i in range(len(list)):
        list[i] = (float)(list[i] - min)/(max - min)
    #print "min = " + str(min) + ", max = " + str(max)
    return list

def complementCodedConversion(list1, list2):
    list = []
    for i in range(len(list1)):
        complementVector = (list1[i], list2[i], 1-list1[i], 1-list2[i])
        list.append(complementVector)
    return list

def fuzzyAnd(tuple1, tuple2):
    ls = []
    for i in range(len(tuple1)):
        minimum = min(tuple1[i], tuple2[i])
        ls.append(minimum)
    #print(ls)
    return ls


def printData(list):
    for i in range(len(list)):
        print (list[i])


def myPlot(dataList, boxList):
    x = map(lambda item:item[0], dataList)
    y = map(lambda item:item[1], dataList)

    plt.scatter(x, y)
    for i in range(len(boxList)):
        (a,b,c,d) = boxList[i]
        plt.plot([a,c,c,a,a], [b, b, d, d, b])

    plt.show()



training = np.loadtxt(fname="C:\\Users\\Ali\Desktop\\fuzzy-art-neural-network-master\\TrainingData.txt")
testing = np.loadtxt(fname="C:\\Users\\Ali\Desktop\\fuzzy-art-neural-network-master\\TestingData.txt")

trainingData = complementCodedConversion(scaledList(training[:, 2].tolist()), scaledList(training[:, 3].tolist()))
testingData = complementCodedConversion(scaledList(testing[:, 2].tolist()), scaledList(testing[:, 3].tolist()))

#printData(trainingData)

#parameters setting
#learning rate
beta = 1
#vigilance
rho = 0.8
alpha = 0.000001
categoryList = []
#shuffle(trainingData)

while True:
    #shuffle(trainingData)
    len1 = len(categoryList)
    for i in range(len(categoryList)):
        tjList = []
        for j in range(len(categoryList)):
            summation1 = fuzzyAnd(trainingData[i], categoryList[j])
            #summation2 = summation1[i] / (alpha+sum(categoryList[j]))
            ss1 = sum(categoryList[j])
            ss2 = alpha + ss1
            summation2 = summation1[i] / ss2
            tjList.append((summation2))

        tjList = sorted(tjList, key=lambda item:item[1])
        noMatchFlag = True
        while len(tjList) != 0 :
            (index, value) = tjList.pop(0)
            if sum(fuzzyAnd(trainingData[i], categoryList[index]))/sum(trainingData[i]) >= rho :
                categoryList[index] = map(lambda x, y: x*beta + y*(1-beta),
                                          fuzzyAnd(trainingData[i], categoryList[index]), categoryList[index])
                noMatchFlag = False
                break
        if noMatchFlag:
            categoryList.append(trainingData[i])

    novellist = []
    for i in range(len(testingData)):
        tjList = []
        for j in range(len(categoryList)):
            tjList.append((j, sum(fuzzyAnd(testingData[i], categoryList[j]))/(alpha+sum(categoryList[j]))))
        tjList = sorted(tjList, key=lambda item:item[1])
        noMatchFlag = True
        while len(tjList) != 0 :
            (index, value) = tjList.pop(0)
            if sum(fuzzyAnd(testingData[i], categoryList[index]))/sum(testingData[i]) >= rho :
                noMatchFlag = False
                break
        if noMatchFlag:
            novellist.append(i)

    print ("***************")
    printData(categoryList)
    print ("***************")
    print ("Novel list:")
    print (novellist)
    myPlot(trainingData, categoryList)
    #print "novelist: ", len(novellist)
    len2 = len(categoryList)
    if len2 == len1 :
        break

#print "number of centers: ", len2

下图中解释器的结果:

enter image description here

我该如何解决此问题以绘制结果。 预先感谢

2 个答案:

答案 0 :(得分:2)

函数map定义了生成器,而不是返回诸如列表或元组之类的对象。

在函数myPlot中,您使用map创建散点图数据,这是matplotlib错误的根源。这些调用应首先转换为列表或元组,例如list(map(...))

答案 1 :(得分:0)

在该示例中,有一行代码在python 2.7中生成了一个列表,但是在python 3中,它只是一个生成器。无论如何,“地图”很奇怪,所以我建议替换该行。