python中的list indices must be integers or slices, not tuple

import numpy as np
from numpy.random import RandomState
rdm = RandomState(1)
X = rdm.rand(256, 2)
X1 = []
X2 = []
Y = []
for x1,x2 in X:
    if x1<0.5 and x2 < 0.5:
        X1.append([x1,x2])
        Y.append(0)
    elif ((x1 > 0.5) and (x2 > 0.5)):
        X1.append([x1, x2])
        Y.append(0)
    else:
        X2.append([x1, x2])
        Y.append(1)
#等价于:Y=[[int(x1 + x2) < 1] for (x1,x2) in X]
X1N = np.array(X1)
X2N = np.array(X2)
print(type(X2N))
print('------------------------------------')
print(X2N)

import matplotlib.pyplot as plt
plt.scatter(X1N[:,0], X1N[:,1], color = 'red', marker = 'x')
#此处使用X1就会出现list indices must be integers or slices, not tuple错误
#因为X1的类型是list,需要转换为numpy的ndarray
plt.scatter(X2N[:,0], X2N[:,1], color = 'blue', marker = 'o')
plt.show()

上面的例子中是为了自己造一个异或的随机数据集,并且将这个数据集画出来,画出来的过程中就出现这个问题了。

运行上面的代码,就可以画出来了:

python中的list indices must be integers or slices, not tuple_第1张图片

后面再搭个模型做个分类任务,实现一下异或。

你可能感兴趣的:(机器学习,Python)