Asked  7 Months ago    Answers:  5   Viewed   45 times

NumPy proposes a way to get the index of the maximum value of an array via np.argmax.

I would like a similar thing, but returning the indexes of the N maximum values.

For instance, if I have an array, [1, 3, 2, 4, 5], function(array, n=3) would return the indices [4, 3, 1] which correspond to the elements [5, 4, 3].

 Answers

17

The simplest I've been able to come up with is:

In [1]: import numpy as np

In [2]: arr = np.array([1, 3, 2, 4, 5])

In [3]: arr.argsort()[-3:][::-1]
Out[3]: array([4, 3, 1])

This involves a complete sort of the array. I wonder if numpy provides a built-in way to do a partial sort; so far I haven't been able to find one.

If this solution turns out to be too slow (especially for small n), it may be worth looking at coding something up in Cython.

Tuesday, June 1, 2021
 
penpen
answered 7 Months ago
89

You can use this

$thing = Array
(
        "test1" => "something",
        "test2" => "something",
        "info" => "yes",
        "array" => Array
        (
                "test1" => "something else",
    "test2" => "something else",
    "info" => "maybe",
                "array" => Array
                (
                        "test1" => "something",
                        "info" => "yes"
                )
        )
);



function validPush($array)
{
    foreach ($array as $key => $value)
    {
        if(is_array($value))
           $array[$key] = validPush($value);

        if($key == "info")
            $array['valid'] = ($value == "yes") ? true : false ;
    }
    return $array;
}

var_dump(validPush($thing));

Output

array
  'test1' => string 'something' (length=9)
  'test2' => string 'something' (length=9)
  'info' => string 'yes' (length=3)
  'array' => 
    array
      'test1' => string 'something else' (length=14)
      'test2' => string 'something else' (length=14)
      'info' => string 'maybe' (length=5)
      'array' => 
        array
          'test1' => string 'something' (length=9)
          'info' => string 'yes' (length=3)
          'valid' => boolean true
      'valid' => boolean false
  'valid' => boolean true
Saturday, May 29, 2021
 
daniel__
answered 7 Months ago
16

Here's a nice function that converts a caffe net to a python list of dictionaries, so you can pickle it and read it anyway you want:

import caffe

def shai_net_to_py_readable(prototxt_filename, caffemodel_filename):
  net = caffe.Net(prototxt_filename, caffemodel_filename, caffe.TEST) # read the net + weights
  pynet_ = [] 
  for li in xrange(len(net.layers)):  # for each layer in the net
    layer = {}  # store layer's information
    layer['name'] = net._layer_names[li]
    # for each input to the layer (aka "bottom") store its name and shape
    layer['bottoms'] = [(net._blob_names[bi], net.blobs[net._blob_names[bi]].data.shape) 
                         for bi in list(net._bottom_ids(li))] 
    # for each output of the layer (aka "top") store its name and shape
    layer['tops'] = [(net._blob_names[bi], net.blobs[net._blob_names[bi]].data.shape) 
                      for bi in list(net._top_ids(li))]
    layer['type'] = net.layers[li].type  # type of the layer
    # the internal parameters of the layer. not all layers has weights.
    layer['weights'] = [net.layers[li].blobs[bi].data[...] 
                        for bi in xrange(len(net.layers[li].blobs))]
    pynet_.append(layer)
  return pynet_
Monday, June 14, 2021
 
QuantumMechanic
answered 6 Months ago
60

nptyping is currently useless for static analysis. Quoting a post by the library's developer on its issue tracker,

mypy is just not supported by nptyping (yet)

I wouldn't put much hope in that "yet". NumPy's dtype and shape handling is very hard to fit into the typing/mypy static type model, and nptyping's own design decisions are a poor fit for NumPy itself. For example, it doesn't look like the dev ever considered arrays that aren't 2D, so Array[str, 3] represents a 2D array with 3 rows and unspecified columns instead of a 3-element 1D array. All the implementation is in terms of rows and columns, too.

As far as I can tell, the only real functionality nptyping has is isinstance checks, and even that's buggy.

Monday, October 11, 2021
 
rsk82
answered 2 Months ago
75

Normal dot product would be (using numpy broadcasting)

M3 = np.sum(M1[:, :, None] * M2[None, :, :], axis = 1)

You can do the same thing with any function you want that has an axis keyword.

M3 = np.max(M1[:, :, None] * M2[None, :, :], axis = 1)

Saturday, November 20, 2021
 
TomPuts
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share