簡體   English   中英

如何使用兩個隨機變量列表在python中創建點列表(二維數組)

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

我嘗試使用一些隨機數據訓練Perceptron。 看來我的數據與Perceptron類的輸入格式不匹配:

這是我用來創建數據的托盤的方法:

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),))

這是我必須在上面准備數據的格式:

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])

如何准備這種格式的數據?

使用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 ]])

您不能在X1X2的定義中添加方括號,因為np.random.randn(Q1,1)已經是一個數組。 只需用以下命令替換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