1

I have a 2D numpy-array as input for a basic sigmoid-classifier. 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 ?

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.

Valentin Metz
  • 393
  • 6
  • 13
  • What are the dimensions of `D`,`w` and `b`? – Ardweaden Mar 27 '19 at 21:59
  • b is the bias of the function (int); w can be a numpy array or an int; D is a Matrix (2D array), however for now only the data in column 7 is important for the function – Valentin Metz Mar 27 '19 at 22:03
  • I understand this, but are the dimensions of `D` 400x14 for example? In this case, you'd be multiplying a single vector of length 14. To get the column, you'd have to do `D[:,7] `. – Ardweaden Mar 27 '19 at 22:06
  • 1
    You are indeed correct and this just solved it. Thank you. If you write it as an answer I will mark it correct, so it may help others. – Valentin Metz Mar 27 '19 at 22:11

1 Answers1

1

EDIT: The issue is the incorrect slicing of the array. One has to use D[:,7] instead of D[:][7] to extract a column.


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)
Ardweaden
  • 857
  • 9
  • 23