NumPy

  • What is an Array?
  • How to Create an Array?
  • Basic array operations
  • Adding, removing, and sorting elements
  • Knowing about the Array
  • Other Useful Array Operations
  • Universal Functions
  • Transposing and reshaping a matrix
  • Indexing, Slicing and Iterating
  • create an array from existing data

What is an array?

An array is a central data structure of the NumPy library. An array is a grid of values and it contains information about the raw data, how to locate an element, and how to interpret an element. It has a grid of elements that can be indexed in various ways. The elements are all of the same type, referred to as the array dtype.

An array can be indexed by a tuple of nonnegative integers, by booleans, by another array, or by integers. The rank of the array is the number of dimensions. The shape of the array is a tuple of integers giving the size of the array along each dimension.

One way we can initialize NumPy arrays is from Python lists, using nested lists for two- or higher-dimensional data.

For example:>>>

>>> a = np.array([1, 2, 3, 4, 5, 6])

or:>>>

>>> a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])

We can access the elements in the array using square brackets. When you’re accessing elements, remember that indexing in NumPy starts at 0. That means that if you want to access the first element in your array, you’ll be accessing element “0”.>>>

>>> print(a[0])
[1 2 3 4]

How to create a basic array

This section covers np.array()np.zeros()np.ones()np.empty()np.arange()np.linspace()dtype

Basic array operations

This section covers multiplication, dot product,addition, subtraction, multiplication, division, and more

Adding, removing, and sorting elements

This section covers np.sort()np.concatenate()

In addition to sort, which returns a sorted copy of an array, you can use:

  • argsort, which is an indirect sort along a specified axis,
  • lexsort, which is an indirect stable sort on multiple keys,
  • searchsorted, which will find elements in a sorted array, and
  • partition, which is a partial sort.

How do you know the shape and size of an array?

This section covers ndarray.ndimndarray.sizendarray.shape, dtype, ndarray.itemsize, ndarray.data

Other useful array operations

This section covers maximum, minimum, sum, mean, product, standard deviation, and more

Universal Functions

NumPy provides familiar mathematical functions such as sin, cos, and exp. In NumPy, these are called “universal functions”(ufunc). Within NumPy, these functions operate elementwise on an array, producing an array as output.

Transposing and reshaping a matrix

This section covers arr.reshape()arr.transpose()arr.T(), np.flip

Indexing, Slicing and Iterating

One-dimensional arrays can be indexed, sliced and iterated over, much like lists and other Python sequences.

Multidimensional arrays can have one index per axis. These indices are given in a tuple separated by commas:

When fewer indices are provided than the number of axes, the missing indices are considered complete slices:

How to create an array from existing data

This section covers slicing and indexingnp.vstack()np.hstack()np.hsplit().view()copy()

Leave a Reply

Your email address will not be published. Required fields are marked *