我在csv文件中有这样的数据:
我只想求数字而不是字符串和我的代码如下:
import csv
import math
import copy
class Pearsons:
def __init__(self,fiturs,docs):
self.fiturs = fiturs
self.docs = docs
def x_value(self):
sumx = []
for fitur in self.fiturs:
x = sum(([float(x)]) for x in fitur[1:])
sumx.append(x)
print sumx
def x_pow(self):
sumxp = []
for fitur in self.fiturs:
xval_pow = sum([float(x)**2 for x in fitur[1:]])
sumxp.append(xval_pow)
return sumxp
def y_value(self):
sumy = []
for dokumen in self.docs:
if dokumen[2] == "POSITIF":
sumy.append(1)
else:
sumy.append(0)
sigma_y = sum(sumy)
return sigma_y
def y_pow(self):
sumy = []
for dokumen in self.docs:
if dokumen[2] == 'POSITIF':
sumy.append(1)
else:
sumy.append(0)
ypow = []
for x in sumy:
ypow.append(x ** 2)
sigmapow = sum(ypow)
return sigmapow
def xy_val(self):
list_y = []
for dokumen in self.docs:
if dokumen[2] == 'POSITIF':
list_y.append(1)
else:
list_y.append(0)
# print list_y
sigmaxy = []
for xyfitur in self.fiturs:
xy = []
for i, x in enumerate(list_y):
xy.append(float(xyfitur[i]) * list_y[i])
sigmaxy.append(sum(xy))
return sigmaxy
def pearsons(self):
list_y = []
for dokumen in self.docs:
if dokumen[2] == 'POSITIF':
list_y.append(1)
else:
list_y.append(0)
# Assume len(x) == len(y)
n = len(list_y)
sigmax = self.x_value()
sigmay = self.y_value()
sigmaxpow = self.x_pow()
sigmaypow = self.y_pow()
sigmaxy = self.xy_val()
rpxy_list = []
for i, v in enumerate(sigmax):
rpxy_top = (n * sigmaxy[i]) - (sigmax[i] * sigmay)
rpxy_bot = math.sqrt((n * sigmaxpow[i]) - (sigmax[i] ** 2)) * math.sqrt((n * sigmaypow) - (sigmay ** 2))
if rpxy_bot == 0:
rpxy_list.append(0)
else:
rpxy_list.append(abs(rpxy_top / rpxy_bot))
#print rpxy_list
return rpxy_list
fiturs = []
docs = []
with open("features2.csv", "rb") as f:
fiturcsv = csv.reader(f, None)
fiturs = [x for x in fiturcsv]
fiturs.insert(0, fiturs[0])
with open("datalatih.csv", "rb") as f:
docscsv = csv.reader(f)
docs = [x for x in docscsv]
p = Pearsons(fiturs, docs)
# nb.BernoulliNaiveBayes()
p.x_value()
#p.xy_val()
导致以下错误:
C:/Users/USER/PycharmProjects/Skripsi/coba.py Traceback(最近期) 最后调用):文件“C:/Users/USER/PycharmProjects/Skripsi/coba.py”, 第117行 p.x_value()文件“C:/Users/USER/PycharmProjects/Skripsi/coba.py”,第14行,x_value x = sum([float(x)])for fitur [1:]中的类型TypeError:+:'int'和'list'的不支持的操作数类型
使用退出代码1完成处理
答案 0 :(得分:1)
你在代码
中有额外的]应该是
def x_value(self):
sumx = []
for fitur in self.fiturs:
x = sum([float(x) for x in fitur[1:]])
sumx.append(x)
print sumx
改变了我
[7.0, 7.0, 4.0, 5.0, 4.0, 205.0, 67.5714, 2.0, 5.0, 3.0, 52.0, 2.0, 6.0]