Wednesday, November 27, 2019

NumPy - Generating Arrays

There are different ways of creating arrays. The examples in the previous illustrate the simplest, by creating a sequence or a list in the form of an argument with the array() function. Below is an example:

>>> x = np.array([[5, 7, 9],[6, 8, 10]])
>>> x
array([[5, 7, 9],[6, 8, 10]])

Other than the lists created, you can also create one or more tuples in the same manner as shown below using the array() function:

>>> x = np.array(((5, 7, 9),(6, 8, 10)))
>>> x
array([[5, 7, 9], [6, 8, 10]])

Alternatively, you can also use the same procedure to create more than one tuple as shown below:

>>> x = np.array([(1, 4, 9), [2, 4, 6], (3, 6, 9)])
>>> x
array([[1, 4, 9],[2, 4, 6],[3, 6, 9]])

As you work with ndarrays , you will come across different types of data. Generally, you will be dealing with numerical values a lot, especially float and integer values. However, the NumPy library is built to support more than those two. The following are other data types that you will use in NumPy:

● bool_
● int_
● intc, intp, int8, int16
● uint8, uint16, uint32, uint64
● float_, float16, float32, float64
● complex64, complex128

Each of the NumPy numerical types mentioned above has a unique function used to call its value as shown below:

Input
float64(52)

Output
52.0

Input
int8(52.0)

Output
52

Input
bool(52)

Output
True

Input
bool(0)

Output
False

Input
bool(52.0)

Output
True

Input
float(True)

Output
1.0

Input
float(False)

Output
0.0

Some of the functions might need a data type to complete the argument as shown below:

Input:
arrange (6, dtype=uint16)

Output:
array ([0, 1, 2, 3, 4, 5], dtype=uint16)

Before you create a multidimensional array, you must know how to create a vector as shown below:

a = arange(4)
a.dtype

Output
dtype('int64')
a
Output
array([0, 1, 2, 3])
a.shape
Output
(4,)

The vector outlined above has only four components. The value of the components is between 0 and 3. To create a multidimensional array, you must know the shape of the array as shown below:

x = array([arange(2), arange(2)])
x
Output
array([[0, 1],[0, 1]])

To determine the shape of the array, use the following function:

x.shape

Output
(2, 2)

The arrange() function has been used to build a 2 x 2 array. You will come across situations where you need to choose only one aspect of an array and ignore the rest. Before you begin, create a 2 x 2 matrix as shown below:

a = array([[10,20],[30,40]])
a

Output

array([[10, 20],
[30, 40]])

From the array above, we are going to select an item. Keep in mind that the index numbers in NumPy always start from 0.

Input: a (0, 0)

Output:
10

Input: a (0, 10)

Output
20

Input: a (10, 0)

Output
30

Input: a (10, 10)

Output
40

From the example above, you can see how easy it is to select specific elements from an array. Given an array a, as above, we have the notation a(x, y) where x and y represent the indices of each object within the array, a. From time to time you might come across character codes. It is important to know the data types associated with them as follows:

Character code    Data type

  • b                bool
  • d                double precision float
  • D               complex
  • f                 single precision float
  • i                 integer
  • S                string
  • u                unsigned integer
  • U               unicode
  • V               void

For example, a single precision floats array can be identified as shown below:

Input:
arrange (5, dtype=’f’)

Output:
array ([0, 1, 2, 3, 4], dtype=f;pat32)
Share:

0 comments:

Post a Comment