Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shorthand notation for numpy.array() [closed]

Tags:

python

numpy

Are there any accepted shorthand notations for numpy.array()? For me the biggest drawback of using numpy, as compared to dedicated numerical langages, is that there is no compact notation for array creation.

My typical verbose usage would be:

import numpy as np
a = np.array([1,2,3])

Can anyone provide examples of shorthand notation for numpy array creation as used in existing mature projects?

like image 467
amicitas Avatar asked Jan 21 '13 21:01

amicitas


People also ask

What does .T mean in Python?

T property is used to transpose index and columns of the data frame.

What does .T mean in NumPy?

The . T accesses the attribute T of the object, which happens to be a NumPy array. The T attribute is the transpose of the array, see the documentation.

What does size () do in NumPy?

In Python, numpy. size() function count the number of elements along a given axis.


1 Answers

Based on DSM's comment, here is a possible short hand:

One could first define the following:

import numpy as np
class ShorthandArray(object):
    def __getitem__(self, key):
        if isinstance(key, tuple):
            return np.array(key)
        else:
            return np.array([key])

_ = ShorthandArray()

Now array creation can be now be done using:

a = _[1,2,3] 

This will also work for multi-dimensional arrays:

a = _[[1,2,3]]


This is certainly compact, but is totally non-standard python notation. Brackets are meant for item access, not for class creation. I can see this potentially creating a great deal of confusion.

like image 178
amicitas Avatar answered Sep 17 '22 04:09

amicitas