TypeError:不支持的操作数类型 - :' list'并且'漂浮'

时间:2017-05-01 07:43:55

标签: python csv

以下是我的python代码

import csv
import random
import math



def separateByClass(dat):
    separated = {}
    for i in range(len(dat)):
        vector = dat[i]
        if (vector[-1] not in separated):
            separated[vector[-1]] = []
        separated[vector.pop()].append(vector)
    return separated

def splitDataset(dataset, splitRatio):
    trainSize = int(len(dataset) * splitRatio)
    trainSet = []
    copy = list(dataset)
    while len(trainSet) < trainSize:
        index = random.randrange(len(copy))
        trainSet.append(copy.pop(index))
    return [trainSet, copy]

def mean(numbers):
    return sum(numbers)/float(len(numbers))

def stdev(numbers):
    avg = mean(numbers)
    variance = sum([pow(x-avg,2) for x in numbers])/float(len(numbers)-1)
    return math.sqrt(variance)

def summarize(dataset):
    summaries = [(mean(attribute), stdev(attribute)) for attribute in zip(*dataset)]
    del summaries[-1]
    return summaries

def summarizeByClass(dataset):
    separated = separateByClass(dataset)
    summaries = {}
    for classValue, instances in separated.iteritems():
        summaries[classValue] = summarize(instances)
    return summaries

def calculateProbability(x, mean, stdev):
    exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(stdev,2))))
    return (1 / (math.sqrt(2*math.pi) * stdev)) * exponent

def calculateClassProbabilities(summaries, inputVector):
    probabilities = {}
    for classValue, classSummaries in summaries.iteritems():
        probabilities[classValue] = 1
        for i in range(len(classSummaries)):
            mean, stdev = classSummaries[i]
            x = inputVector[i]
            probabilities[classValue] *= calculateProbability(x, mean, stdev)
    return probabilities

def predict(summaries, inputVector):
    probabilities = calculateClassProbabilities(summaries, inputVector)
    bestLabel, bestProb = None, -1
    for classValue, probability in probabilities.iteritems():
        if bestLabel is None or probability > bestProb:
            bestProb = probability
            print bestProb
            bestLabel = classValue
    return bestLabel

def getPredictions(summaries, testSet):
    predictions = []
    for i in range(len(testSet)):
        result = predict(summaries, testSet[i])
        predictions.append(result)
    return predictions

def getAccuracy(testSet, predictions):
    correct = 0
    for i in range(len(testSet)):
        if testSet[i][-1] == predictions[i]:
            correct += 1
    return (correct/float(len(testSet))) * 100.0

def main(str):
    #clustered data
    filename = 'a.csv'
    lines = csv.reader(open(filename, "rb"))
    a=list(lines)
    for i in range(len(a)):
        a[i]=[float(x) for x in a[i]]


    #main data
    filename = 'h.csv'
    lines = csv.reader(open(filename, "rb"))
    data = list(lines)
    for i in range(len(data)):
        data[i] = [float(x) for x in data[i]]
        data[i].append(a[i][1])

    s = separateByClass(data)

    count=True
    for key,values in s.items():
        if count:
            a=values
            count=False
        b=values

    trainingSet1, testSet1 = splitDataset(a, 0.67)
    trainingSet, testSet = splitDataset(b, 0.67)
    trainingSet.extend(trainingSet1)
    testSet.extend(testSet1)
    summaries = summarizeByClass(trainingSet)
    testset=str
    predictions = predict(summaries, testSet)
    accuracy = getAccuracy(testSet, predictions)
    result = predict(summaries, testset)
    returnValue.append(accuracy)
    returnValue.append(result)
    print returnValue

def ab():
    st=[70.0,1.0,4.0,130.0,322.0,0.0,2.0,109.0,0.0,2.4,2.0,3.0,3.0,1.0]
    a=main(st)
    return a
ab()
  

两个文件使用a.csv和h.csv分别包含2个和14个列。   如果不计算准确度,代码运行完全正常。   即仅在使用predict()时。

它给出了如下错误。

Traceback (most recent call last):
File "D:\nowedit\P.py", line 126, in <module>
ab()
File "D:\nowedit\P.py", line 124, in ab
a=main(st)
File "D:\nowedit\P.py", line 115, in main
predictions = predict(summaries, testSet)
File "D:\nowedit\P.py", line 60, in predict
probabilities = calculateClassProbabilities(summaries, inputVector)
File "D:\nowedit\P.py", line 56, in calculateClassProbabilities
probabilities[classValue] *= calculateProbability(x, mean, stdev)
File "D:\nowedit\P.py", line 46, in calculateProbability
exponent = math.exp(-(math.pow(x-mean,2)/(2*math.pow(stdev,2))))
TypeError: unsupported operand type(s) for -: 'list' and 'float'

2 个答案:

答案 0 :(得分:1)

您不能使用list和float作为此数学函数的参数。

答案 1 :(得分:0)

如果将此行添加到calculateProbability(x,mean,stdev)函数中:

print("%s%s%s" % (type(x), type(mean), type(stdev))

你会看到x是一个列表,而mean是浮点数。

如错误所述:

TypeError: unsupported operand type(s) for -: 'list' and 'float'

您不能使用list和float作为此数学函数的参数。