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.

4
  • What are the dimensions of D,w and b? Commented Mar 27, 2019 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 Commented Mar 27, 2019 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] . Commented Mar 27, 2019 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. Commented Mar 27, 2019 at 22:11

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

prob already is an array. np.ravel() does not change anything. I will clarify the question.
That [:] is superfluous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.