简体   繁体   English

使用 numpy 数组从方法创建一个 numpy 数组

[英]Creating a numpy array from a method using numpy array

I have a 2D numpy-array as input for a basic sigmoid-classifier.我有一个 2D numpy-array 作为基本 sigmoid 分类器的输入。 I would like the classifier to return an array with the probabilities.我希望分类器返回一个带有概率的数组。

import numpy as np


def sigmoid(x):
    sigm = 1 / (1 + np.exp(-x))
    return sigm


def p(D, w, b):
    prob=sigmoid(np.dot(D[:][7],w)+b)
    return prob

How can I change p() so that it returns a 1D numpy array with the probabilities listed in order of the input data ?如何更改 p() 以便它返回一个一维 numpy 数组,其中的概率按输入数据的顺序列出?

Atm "prob" is an array of length 14, however the input array "D" is over 400 in size, so there is an error somewhere in the logic. Atm "prob" 是一个长度为 14 的数组,但是输入数组 "D" 的大小超过 400,因此逻辑中的某处存在错误。

EDIT: The issue is the incorrect slicing of the array.编辑:问题是阵列的不正确切片。 One has to use D[:,7] instead of D[:][7] to extract a column.必须使用D[:,7]而不是D[:][7]来提取一列。


Perhaps like this:也许像这样:

import numpy as np


def sigmoid(x):
    sigm = 1 / (1 + np.exp(-x))
    return sigm


def p(D, w, b):
    prob=sigmoid(np.dot(D[:][7],w)+b)
    return np.ravel(prob)

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

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