Monday, October 5, 2020

Array-Oriented Programming with NumPy-3 (Filling arrays and creating arrays)


NumPy provides functions zeros, ones and full for creating arrays containing 0s, 1s or a specified value,
respectively. By default, zeros and ones create arrays containing float64 values. We’ll show how to customize the element type momentarily. The first argument to these functions must be an integer or a tuple of integers specifying the desired dimensions. For an integer, each function returns a one-dimensional array with the specified number of elements:

In [1]: import numpy as np
In [2]: np.zeros(5)
Out[2]: array([ 0., 0., 0., 0., 0.]) 

For a tuple of integers, these functions return a multidimensional array with the specified dimensions. You can specify the array’s element type with the zeros and ones function’s dtype keyword argument:

In [3]: np.ones((2, 4), dtype=int)
Out[3]: array([[1, 1, 1, 1],
[1, 1, 1, 1]])

The array returned by full contains elements with the second argument’s value and type:

In [4]: np.full((3, 5), 13)
Out[4]:
array([[13, 13, 13, 13, 13],
[13, 13, 13, 13, 13],
[13, 13, 13, 13, 13]])

Creating arrays from Ranges

NumPy provides optimized functions for creating arrays from ranges. We focus on simple evenly spaced integer and floating-point ranges, but NumPy also supports nonlinear ranges. 

a. Creating Integer Ranges with arange

Let’s use NumPy’s arange function to create integer ranges similar to using built-in function range. In each case, arange first determines the resulting array’s number of elements, allocates the memory, then stores the specified range of values in the array:

In [1]: import numpy as np
In [2]: np.arange(5)
Out[2]: array([0, 1, 2, 3, 4])

In [3]: np.arange(5, 10)
Out[3]: array([5, 6, 7, 8, 9])
In [4]: np.arange(10, 1, -2)
Out[4]: array([10, 8, 6, 4, 2])

Though you can create arrays by passing ranges as arguments, always use arange as it’s optimized for arrays.

b. Creating Floating-Point Ranges with linspace

You can produce evenly spaced floating-point ranges with NumPy’s linspace function. The function’s first two arguments specify the starting and ending values in the range, and the ending value is included in the array. The optional keyword argument num specifies the number of evenly spaced values to produce this argument’s default value is 50:

In [5]: np.linspace(0.0, 1.0, num=5)
Out[5]: array([ 0. , 0.25, 0.5 , 0.75, 1. ]) 

c. Reshaping an array

You also can create an array from a range of elements, then use array method reshape to transform the one dimensional array into a multidimensional array. Let’s create an array containing the values from 1 through 20, then reshape it into four rows by five columns:

In [6]: np.arange(1, 21).reshape(4, 5)
Out[6]:
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])


Note the chained method calls in the preceding snippet. First, arange produces an array containing the values 1–20. Then we call reshape on that array to get the 4-by-5 array that was displayed. You can reshape any array, provided that the new shape has the same number of elements as the original. So a six-element one-dimensional array can become a 3-by-2 or 2-by-3 array, and vice versa, but attempting to reshape a 15-element array into a 4-by-4 array (16 elements) causes a ValueError.

Displaying Large arrays

When displaying an array, if there are 1000 items or more, NumPy drops the middle rows, columns or both from the output. The following snippets generate 100,000 elements. The first case shows all four rows but only the first and last three of the 25,000 columns. The notation ... represents the missing data. The second case shows the first and last three of the 100 rows, and the first and last three of the 1000 columns:

In [7]: np.arange(1, 100001).reshape(4, 25000)
Out[7]:
array([[ 1, 2, 3, ..., 24998, 24999,
25000],
[ 25001, 25002, 25003, ..., 49998, 49999,
50000],
[ 50001, 50002, 50003, ..., 74998, 74999,
75000],
[ 75001, 75002, 75003, ..., 99998, 99999,
100000]])

In [8]: np.arange(1, 100001).reshape(100, 1000)
Out[8]:
array([[ 1, 2, 3, ..., 998, 999,
1000],
[ 1001, 1002, 1003, ..., 1998, 1999,
2000],
[ 2001, 2002, 2003, ..., 2998, 2999,
3000],
...,
[ 97001, 97002, 97003, ..., 97998, 97999,
98000],
[ 98001, 98002, 98003, ..., 98998, 98999,
99000],
[ 99001, 99002, 99003, ..., 99998, 99999,
100000]])


Share:

0 comments:

Post a Comment