4

I'm using fuzzy art algorithm to be applying on my data and everything works fine in case of results but when it come to plot the result the interpreter says: RuntimeError: matplotlib does not support generators as input.

By the way, i'm using python as a programming language and matplotlib to plot the results.

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

The result of the interpreter in the image below:

enter image description here

How can i solve this problem to plot my results. Thanks in advance

Ali Barani
  • 238
  • 2
  • 7
  • 17

2 Answers2

11

the function map defines a generator instead of returning an object such as a list or a tuple.

In the function myPlot, you use map to create your scatter plot data, which is what matplotlib errors on. These calls should first be converted to a list or tuple, e.g., list(map(...)).

amdex
  • 761
  • 3
  • 10
  • Thanks through editing the two lines which defines x and y to list(map(...)) it solves the problem and now i can see the results are plotting. – Ali Barani Jul 05 '19 at 14:23
1

In that example there is a line which produces a list in python 2.7 but in python 3 it is just a generator. The "map" is strange anyways, so I'd recommend to replace that line.

bsikriwal
  • 178
  • 3
  • 15