http://python.jobbole.com/81019/
为了学习Beyas并且练习python,按照上面地址的文章里面,一步步的实现。由于python基础比较弱,所以最简单的程序,也有可能需要我没见过并且解决不了的问题。
1. 处理数据。
用的数据是pima-indians-diebetes.csv,上文贴出的该数据集的地址已经失效,从CSDN的下载区下载的。
数据共包括768行9列(8列属性,一列label),
def loadCsv(filename):
lines = csv.reader(open(filename, "r"))
dataset = list(lines) #好像是把一个迭代器变成list
print('lines:', lines)
print('dataset:', dataset)
for i in range(len(dataset)):
dataset[i] = [float(x) for x in dataset[i]] #把string变成float
print('dataset after process:', dataset)
return dataset
lines: <_csv.reader object at 0x000001DF4F980048>
dataset[0]: ['6', '148', '72', '35', '0', '33.6', '0.627', '50', '1']
dataset[0] after process: [6.0, 148.0, 72.0, 35.0, 0.0, 33.6, 0.627, 50.0, 1.0]
# **************************************************************
# split dataset into two parts: train and test
def splitDataset(dataset,splitRatio):
'''
split dataset into two parts:
train and test
'''
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]
splitRatio=0.67
train, test = splitDataset(dataset_pima, splitRatio)
print('** Get {0} train samples and {1} test sample.'.format(len(train), len(test)))
出问题的是最后这句:
print('** Get {0} train samples and {1} test sample.'.format(len(train), len(test)))
print('** Get {0} train samples and {1} test sample.').format(len(train), len(test))
原文是第二句,差别在.format前的“)”,如果是第二种,相当于print().format,而print是打印语句,返回为None,则None是没有format属性的。