【问题标题】:How can I create a list of points (two dimensional arrays) in python using two lists of random variables如何使用两个随机变量列表在 python 中创建点列表(二维数组)
【发布时间】:2018-10-25 20:47:43
【问题描述】:

我尝试使用一些随机数据来训练感知器。看来我的数据与 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])

如何以这种格式准备我的数据?

【问题讨论】:

    标签: python arrays numpy reshape


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你不能在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)
      

      【讨论】:

      • 谢谢,我从我的代码和问题中删除了它们,但它对我没有帮助。
      猜你喜欢
      • 2021-03-11
      • 2021-02-28
      • 2014-02-14
      • 2022-06-14
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多