我只想使用numpy为分类问题生成一个多类测试数据集。例如,X是维数(Mxn)的数组,y是维数(Mx1)的数组,假设有k个。类的数量。请帮我弄一下代码。其中,X表示要素,y表示标注
发布于 2018-11-21 17:42:18
您可以像这样使用np.random.randint:
import numpy as np
m = 4
n = 4
k = 5
X = np.random.randint(0,2,(m,n))
X
array([[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 1, 0, 0],
[1, 1, 1, 1]])
y = np.random.randint(0,k,m)
y
array([3, 3, 0, 4])发布于 2020-07-07 19:26:23
您可以使用numpy创建多类数据集,如下所示:
def generate_dataset(size, classes=2, noise=0.5):
# Generate random datapoints
labels = np.random.randint(0, classes, size)
x = (np.random.rand(size) + labels) / classes
y = x + np.random.rand(size) * noise
# Reshape data in order to merge them
x = x.reshape(size, 1)
y = y.reshape(size, 1)
labels = labels.reshape(size, 1)
# Merge the data
data = np.hstack((x, y, labels))
return data当使用matplotlib可视化时,生成的数据将如下所示-

您可以使用classes和noise参数更改类的数量和数据的分布。这里我保持了x轴和y轴值之间的线性关系,也可以根据需要改变。
https://stackoverflow.com/questions/53406682
复制相似问题