import random
nPoints = 1000
#随机获取x列表的数据
xPlot = [(float(i)/float(nPoints) - 0.5) for i in range(nPoints + 1)]
x = [[s] for s in xPlot]
#运行一个随机种子
random.seed(1)
#在x的基础上进行数据的随机获取,每个数据在0.1范围内随机波动
y = [s + numpy.random.normal(scale=0.1) for s in xPlot]
#抽样数为总数的0.3倍
nSample = int(nPoints * 0.30)
# 随机抽样获取测试数据的索引
idxTest = random.sample(range(nPoints), nSample)
idxTest.sort()
# 随机抽样获取训练数据的索引
idxTrain = [idx for idx in range(nPoints) if not (idx in idxTest)]
# 根据上述得到的索引值获取随机抽样出来的测试和训练具体数据
xTrain = [x[r] for r in idxTrain]
xTest = [x[r] for r in idxTest]
yTrain = [y[r] for r in idxTrain]
yTest = [y[r] for r in idxTest]