# NumPy Array Basics - Helpful Methods and Shortcuts

``````

In :

import sys
print(sys.version)
import numpy as np
print(np.__version__)

``````
``````

3.3.2 (v3.3.2:d047928ae3f6, May 13 2013, 13:52:24)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
1.9.2

``````

Now we’ve got our array and we’ve got a method or two that we can use to create them. Let’s learn a bit more about some of the functions that arrays give us.

``````

In :

npa = np.arange(25)

``````
``````

In :

npa

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24])

``````
``````

In :

print(type(npa))

``````
``````

<class 'numpy.ndarray'>

``````

First let’s checkout the type we’ve got, you can see it’s an ndarray which is the fundamental array in numpy. We can get the data type of the array. It’s worth elaborating a bit on dtypes or data types.

Unlike python, numpy has a variety of types. I’ll copy and paste the list here and speak to the different types.

``````

In :

print(npa.dtype)

``````
``````

int64

``````

Data type Description

• bool_
• Boolean (True or False) stored as a byte
• int_
• Default integer type (same as C long; normally either int64 or int32)
• intc
• Identical to C int (normally int32 or int64)
• intp
• Integer used for indexing (same as C ssize_t; normally either int32 or int64)
• int8
• Byte (-128 to 127)
• int16
• Integer (-32768 to 32767)
• int32
• Integer (-2147483648 to 2147483647)
• int64
• Integer (-9223372036854775808 to 9223372036854775807)
• uint8
• Unsigned integer (0 to 255)
• uint16
• Unsigned integer (0 to 65535)
• uint32
• Unsigned integer (0 to 4294967295)
• uint64
• Unsigned integer (0 to 18446744073709551615)
• float_
• Shorthand for float64.
• float16
• alf precision float: sign bit, 5 bits exponent, 10 bits mantissa
• float32
• ingle precision float: sign bit, 8 bits exponent, 23 bits mantissa
• float64
• ouble precision float: sign bit, 11 bits exponent, 52 bits mantissa
• complex_
• Shorthand for complex128.
• complex64
• Complex number, represented by two 32-bit floats (real and imaginary components)
• complex128
• Complex number, represented by two 64-bit floats (real and imaginary component- s)

We’ve got all these different types that obviously have different abilities but let’s try creating some numpy arrays with those different types.

``````

In :

np.array(range(20))

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])

``````

It’s worth starting that numpy arrays can be created in the fly simply by passing a list into the “array” function.

By default, numpy will try to guess what kinds of types you have in there but on creation you can also specify which type you would like. Now numpy arrays all have to have the same type, you can’t have a mix of floats and integers for example.

``````

In :

np.array([1.0,0,2,3])

``````
``````

Out:

array([ 1.,  0.,  2.,  3.])

``````
``````

In :

np.array([1.0,0,2,3]).dtype

``````
``````

Out:

dtype('float64')

``````
``````

In :

np.array([True,2,2.0]).dtype

``````
``````

Out:

dtype('float64')

``````

This allows for consistent memory allocation and vectorization.

Sometimes you can get strange types when you're not quite expecting them.

``````

In :

True == 1

``````
``````

Out:

True

``````
``````

In :

np.array([True,2,2.0])

``````
``````

Out:

array([ 1.,  2.,  2.])

``````

If you have any worry about what kind of array you’re creating, just specify the type.

``````

In :

np.array([True, 1, 2.0], dtype='bool_')

``````
``````

Out:

array([ True,  True,  True], dtype=bool)

``````
``````

In :

np.array([True, 1, 2.0], dtype='float_')

``````
``````

Out:

array([ 1.,  1.,  2.])

``````
``````

In :

np.array([True, 1, 2.0], dtype='uint8')

``````
``````

Out:

array([1, 1, 2], dtype=uint8)

``````
``````

In :

npa

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24])

``````

Numpy arrays also have some nifty functions that make our lives a lot easier. We can get the size, which is functionally equivalent to len in raw python. We can get the shape which gives us a hint as to where we’ll be going next, towards multidimensional arrays.

``````

In :

npa.size

``````
``````

Out:

25

``````
``````

In :

npa.shape

``````
``````

Out:

(25,)

``````

We can also get things like the maximum, the minimum, the mean, the standard deviation, and variance. This makes it super easy to perform calculations quickly.

``````

In :

print(npa.min(), npa.max())

``````
``````

0 24

``````
``````

In :

npa.std()

``````
``````

Out:

7.2111025509279782

``````
``````

In :

npa.var()

``````
``````

Out:

52.0

``````

We can also just get the locations of certain values with argmin, argmax.

``````

In :

npa.argmin()

``````
``````

Out:

0

``````
``````

In :

npa.argmax()

``````
``````

Out:

24

``````
``````

In :

np.arange(1,10)

``````
``````

Out:

array([1, 2, 3, 4, 5, 6, 7, 8, 9])

``````
``````

In :

np.arange(10,1,-1)

``````
``````

Out:

array([10,  9,  8,  7,  6,  5,  4,  3,  2])

``````

Now that we’ve seen how we can play around with arrays and some of their helper functions. We can elaborate on all the different ways of creating them. We’ve seen arange but we can actually do some cool things with it. We can count up or down. We can also fill in between two numbers. Say we want 1 to 2 broken up into 5 numbers.

``````

In :

np.linspace(1,2,5)

``````
``````

Out:

array([ 1.  ,  1.25,  1.5 ,  1.75,  2.  ])

``````
``````

In :

np.linspace(0,10,11)

``````
``````

Out:

array([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.])

``````

What about converting that to integers? We can use the as type method to convert it.

We can convert it to an integer

``````

In :

np.linspace(0,10,11).astype('int')

``````
``````

Out:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])

``````

a 32bit float or any other type

``````

In :

np.linspace(0,10,11).astype(np.float32)

``````
``````

Out:

array([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.], dtype=float32)

``````
``````

In :

np.float32(5)

``````
``````

Out:

5.0

``````

Now there is one type that hasn’t been listed yet that is a bit difficult to describe. is nan or not a number. This will certainly come up and can throw off your analysis. Now nan’s are technically floats and can be created when you divide two 0 floating point numbers.

``````

In :

problem = np.array([0.])/np.array([0.])
# python throws an error because it knows something is wrong

``````
``````

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/IPython/kernel/__main__.py:1: RuntimeWarning: invalid value encountered in true_divide
if __name__ == '__main__':

``````
``````

In :

problem

``````
``````

Out:

nan

``````
``````

In :

np.nan

``````
``````

Out:

nan

``````
``````

In :

ar = np.linspace(0,10,11).astype(np.float32)
ar

``````
``````

Out:

array([  0.,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.], dtype=float32)

``````
``````

In :

ar = np.nan

``````
``````

In :

ar

``````
``````

Out:

array([ nan,   1.,   2.,   3.,   4.,   5.,   6.,   7.,   8.,   9.,  10.], dtype=float32)

``````

If you’ve got a nan value in your array. It will throw off all your calculations. You’ve got to explicitly check for it in numpy.

``````

In :

ar.min()

``````
``````

Out:

nan

``````
``````

In :

ar.max()

``````
``````

Out:

nan

``````
``````

In :

ar.mean()

``````
``````

Out:

nan

``````

We’re going to build on this knowledge but at this point all that we need to know is that

• arrays need to have the same values and
• nan values affect statistics derived from our arrays like the mean
``````

In :

np.isnan(ar.max())

``````
``````

Out:

True

``````

As I said we’ll dive into this in more detail in the future. I just want you to be aware that there is a representation of illegal float values in numpy and pandas. In numpy those aren’t handled with out being explicit while in pandas they do some of the work for you.

Now that we understand more about array creation, some array properties, and data types. Let’s dive a bit more into vectorization.

``````

In [ ]:

``````