Prev: Array construction       |       Next: Array indexing

# Array construction (cont'd)¶

• arange(): Use alterantively the 'arange' method to construct an array by defining the range of its member items.
In [1]:
import numpy as np

ar = np.arange(12)     # constructs an array with successive integers in the range [0,11]
print(ar)
[ 0  1  2  3  4  5  6  7  8  9 10 11]

### The arange general syntax is:¶

numpy.arange([start, ]stop [,step, dtype=None])
• The 'stop' value is not included in array (similar to the lists 'range' behavior)
In [2]:
import numpy as np

ar = np.arange(10, 20, 2, dtype=float)
print(ar)
[ 10.  12.  14.  16.  18.]

### Array important properties:¶

• shape: a tuple representing how the array member data are distributed over the array n-dimensions; for example a 16-member array can be of 2x8 or 1x16 or 4x4 or 2x2x2x2 shape
• ndim: dimensions (indices required to refer to an array member item)
• dtype: the type of array members, for example, int32, float64, etc.
• itemsize: size of each array member in memory in bytes
• size: lenght of the array (number of its members)
In [3]:
import numpy as np

ar = np.arange(3, 19, 3, dtype=int)
print(ar)

print(ar.shape, ar.ndim, ar.dtype.name, ar.itemsize, ar.size, type(ar), sep=', ')
[ 3  6  9 12 15 18]
(6,), 1, int32, 4, 6, <class 'numpy.ndarray'>
• This is a one-dimensional array (ndim=1) with integers (int32) having 6 members (size=6) each of wich occupies in memory 4 bytes (itemsize)
• The array shape is currently (6,) meaning: 6 members in 1 axis (dimension)

### Reshaping arrays¶

• Use reshape or shape to change/define the array dimensions and the member items arangement
In [4]:
import numpy as np

# Use 'reshape' method when creating the array to right away define its shape
ar = np.arange(16).reshape(2,8)
print(ar,'\n')
print(ar.ndim, ar.shape)
[[ 0  1  2  3  4  5  6  7]
[ 8  9 10 11 12 13 14 15]]

2 (2, 8)
In [5]:
# Use 'reshape' to change the array shape as needed
ar = ar.reshape(2,2,4)
print(ar,'\n')
print(ar.ndim, ar.shape)
print()
[[[ 0  1  2  3]
[ 4  5  6  7]]

[[ 8  9 10 11]
[12 13 14 15]]]

3 (2, 2, 4)

• 'shape' works similarly by asigning a tuple that represents distribution across array dimensions
In [6]:
import numpy as np
ar = np.arange(16).reshape(2,8)

ar.shape = (4,4)
print(ar)
print(ar.ndim, ar.shape,'\n')

ar.shape = (2,2,4)
print(ar)
print(ar.ndim, ar.shape)
[[ 0  1  2  3]
[ 4  5  6  7]
[ 8  9 10 11]
[12 13 14 15]]
2 (4, 4)

[[[ 0  1  2  3]
[ 4  5  6  7]]

[[ 8  9 10 11]
[12 13 14 15]]]
3 (2, 2, 4)

## Array construction: Special cases¶

• You can construct arrays of special format, as follows:
• zeros(): generate an array full of zeros
• ones(): generate an array full of ones
• eye(): generate an array with '1's in diagonals
• linspace(): evenly distribute array values over a 'line'
• zeros() and ones(): When we know the array size but not its member values we may start by constructing an array full of '0's or '1's as placeholders
• zeros() constructs an array full of '0'
In [7]:
import numpy as np

ar = np.zeros([3,5])
# it works the same if you use tuple
ar = np.zeros((2,4))
print(ar,'\n')
[[ 0.  0.  0.  0.]
[ 0.  0.  0.  0.]]

• ones() creates an array full of '1'
In [8]:
import numpy as np

br = np.ones((2,5))
print(br,'\n')
[[ 1.  1.  1.  1.  1.]
[ 1.  1.  1.  1.  1.]]

• eye() creates a square array (accepts one parameter only) with '1's as diagonal items
In [9]:
import numpy as np

dr = np.eye(4)
print(dr,'\n')
[[ 1.  0.  0.  0.]
[ 0.  1.  0.  0.]
[ 0.  0.  1.  0.]
[ 0.  0.  0.  1.]]

• linspace() returns evenly spaced numbers over a specified interval [start, stop]. If endpoint=False then the end-point is excluded
In [10]:
import numpy as np

er1 = np.linspace(start=1,stop=10,num=10,endpoint=True)
print(er1,'\n')
er2 = np.linspace(0,10,5,True)
print(er2,'\n')
er3 = np.linspace(0,10,5,False)
print(er3,'\n')
[  1.   2.   3.   4.   5.   6.   7.   8.   9.  10.]

[  0.    2.5   5.    7.5  10. ]

[ 0.  2.  4.  6.  8.]

• empty() returns an array whose initial content is random. 'Random' here means that the content are not initialized in any way but depend on the state of the memory. By default, the dtype of the created array is float64.
In [11]:
import numpy as np

ar = np.empty([2,20])
print(ar,'\n')
[[  7.83579172e-317   1.39326512e-321   0.00000000e+000   0.00000000e+000
1.90979621e-313   2.03889433e+180   4.82337723e+228   4.56317366e-144
4.98129035e+151   7.48465114e+251   1.91077947e+214   7.34295013e+223
4.54814392e-144   5.83001600e+199   7.48468178e+251   6.21041497e+175
6.22651688e+228   9.10016855e+276   4.11200729e+223   5.28595592e-085]
[  6.90385432e-072   1.71877956e+161   9.16522712e+135   1.40022637e+195
1.92174141e-057   8.82142681e+199   4.07191568e+223   9.15279752e+242
7.10579906e+159   8.02963163e-095   4.07175032e+223   1.71872056e+161
8.50039893e-096   1.75300433e+243   9.24785801e+135   1.07441485e+160
1.06010578e-153   4.24819624e+180   3.68423986e+180   9.92169729e+247]]