0% found this document useful (0 votes)
17 views

Aggregate Functions

Aggregate functions perform operations on arrays to produce single results like sums, mins, maxes, and means. Indexing allows accessing individual elements or slices of arrays using square brackets. Negative indexes count backwards from the end of the array. Multiple elements can be selected by passing an array of indexes.

Uploaded by

Madhav Arora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Aggregate Functions

Aggregate functions perform operations on arrays to produce single results like sums, mins, maxes, and means. Indexing allows accessing individual elements or slices of arrays using square brackets. Negative indexes count backwards from the end of the array. Multiple elements can be selected by passing an array of indexes.

Uploaded by

Madhav Arora
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Aggregate Functions

Aggregate functions perform an operation on a set of values, an array for example, and
produce a single result. Therefore, the sum of all the elements in an array is an aggregate
function. Many functions of this kind are implemented within the class ndarray.
>>> a = np.array([3.3, 4.5, 1.2, 5.7, 0.3])
>>> a.sum()
15.0
>>> a.min()
0.29999999999999999
>>> a.max()
5.7000000000000002
>>> a.mean()
3.0
>>> a.std()
2.0079840636817816
Indexing, Slicing, and Iterating
In the previous sections, you saw how to create an array and how to perform operations
on it. In this section, you will see how to manipulate these objects. You’ll learn how to
select elements through indexes and slices, in order to obtain the values contained in
them or to make assignments in order to change their values. Finally, you will also see
how you can make iterations within them.

Indexing
Array indexing always uses square brackets ([ ]) to index the elements of the array so
that the elements can then be referred individually for various, uses such as extracting a
value, selecting items, or even assigning a new value.
When you create a new array, an appropriate scale index is also automatically
created (see Figure 3-3).

In order to access a single element of an array, you can refer to its index.
>>> a = np.arange(10, 16)
>>> a
array([10, 11, 12, 13, 14, 15])
>>> a[4]
14
The NumPy arrays also accept negative indexes. These indexes have the same
incremental sequence from 0 to –1, –2, and so on, but in practice they cause the final
element to move gradually toward the initial element, which will be the one with the
more negative index value.
>>> a[–1]
15
>>> a[–6]
10
To select multiple items at once, you can pass array of indexes in square brackets.
>>> a[[1, 3, 4]]
array([11, 13, 14])
Chapter 3 The NumPy Library

You might also like