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?
T property is used to transpose index and columns of the data frame.
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.
In Python, numpy. size() function count the number of elements along a given axis.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With