简体   繁体   English

如何使用两个随机变量列表在python中创建点列表(二维数组)

[英]How can I create a list of points (two dimensional arrays) in python using two lists of random variables

I try to train a Perceptron using some random data. 我尝试使用一些随机数据训练Perceptron。 It seems my data doesn't match the format of inputs for the Perceptron class: 看来我的数据与Perceptron类的输入格式不匹配:

Here is the way I tray to create my data: 这是我用来创建数据的托盘的方法:

Q1 = 100
X1 = 1 + 0.5*np.random.randn(Q1,1)
X2 = 1 + 0.5*np.random.randn(Q1,1)


my_training_inputs = np.array(list(zip(X1, X2)))
my_labels = np.ones((len(X1),))

Here is the format I must prepare my data above: 这是我必须在上面准备数据的格式:

training_inputs = []
training_inputs.append(np.array([1, 1]))
training_inputs.append(np.array([1, 0]))
training_inputs.append(np.array([0, 1]))
training_inputs.append(np.array([0, 0]))

labels = np.array([1, 0, 0, 0])

How can I prepare my data in this format? 如何准备这种格式的数据?

Use np.concatenate : 使用np.concatenate

Q1 = 100
X1 = 1 + 0.5*np.random.randn(Q1,1)
X2 = 1 + 0.5*np.random.randn(Q1,1)

my_training_inputs = np.concatenate((X1,X2), axis=1)
my_labels = np.ones((len(X1),))   

print(my_labels.shape)
#(100,)

print(my_training_inputs.shape)
#(100, 2)

#print the 4 first samples (inputs)
print(my_training_inputs[0:4])
#array([[0.754558  , 0.76998302],
#       [0.0716354 , 1.34796436],
#       [1.25007314, 1.61079584],
#       [0.74931903, 0.9899375 ]])

You must not add brackets in the definition of X1 and X2 , because np.random.randn(Q1,1) already is an array. 您不能在X1X2的定义中添加方括号,因为np.random.randn(Q1,1)已经是一个数组。 Simply replace the definition of X1 and X2 by this: 只需用以下命令替换X1X2的定义:

X1 = 1 + 0.5*np.random.randn(Q1,1)
X2 = 1 + 0.5*np.random.randn(Q1,1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM