.shapeThe size along each dimension, such as (2, 2).
Arrays, attributes and statistics
Unit 5 · Numerical computing with NumPy
NumPy is a Python package for working with arrays and numerical operations. An array has a shape and a data type; arithmetic can operate across the array without writing a loop for every item. Select a one-dimensional or two-dimensional view, then run real NumPy examples in the browser.
Edit up to four numeric values. Switch dimensions to see how the same values can be grouped into rows.
Indexed array view
.shape(4,)
.ndim1
.size4
.dtypeint64*
*For this integer-valued example; NumPy chooses dtype from the supplied values. Real arrays may use float64 or other dtypes.
Inspect attributes in Python
import numpy as np
arr = np.array([82, 91, 76, 88])
print("Array:", arr)
print("shape:", arr.shape)
print("ndim:", arr.ndim)
print("size:", arr.size)
print("dtype:", arr.dtype)
print("itemsize:", arr.itemsize)Output
Your program's output will appear here.
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
Like a list, a one-dimensional array uses zero-based indexes and slices. NumPy arrays also support vectorized operations and matrix calculations.
import numpy as np
marks = np.array([82, 91, 76, 88])
print("first:", marks[0])
print("last:", marks[-1])
print("first three:", marks[:3])
print("add five:", marks + 5)Output
Your program's output will appear here.
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
Choose a numerical operation
Calculate the arithmetic average.
import numpy as np
marks = np.array([82, 91, 76, 88])
print("mean:", np.mean(marks))Output
Your program's output will appear here.
NameError: name 'mark' is not defined
NameError: A name was used before Python could find a variable or function with that name.
IndexError: list index out of range
IndexError: The requested position is outside the items in this list or sequence.
KeyError: 'email'
KeyError: That key does not exist in this dictionary.
TypeError: can only concatenate str (not "int") to str
TypeError: This operation does not work with the kind of value it received.
ZeroDivisionError: division by zero
ZeroDivisionError: A number cannot be divided by zero.
SyntaxError: invalid syntax
SyntaxError: Python could not understand the structure of this line. Check spelling, brackets, colons, and quotes.
IndentationError: expected an indented block
IndentationError: The spaces at the start of a line do not match Python's block structure.
.shapeThe size along each dimension, such as (2, 2).
.ndimHow many dimensions the array has.
.sizeTotal number of elements.
.dtypeThe element data type, such as int64 or float64.
.itemsizeBytes used by one array element.
np.matmul(a, b)Matrix multiplication needs compatible inner dimensions.
arr[4] on an array with four itemsFor four elements, valid positions are 0 through 3.
Mixing integers and decimalsNumPy may choose a floating-point dtype to represent all values.
[1, 2, 3] + 5A Python list cannot add a number element-wise; create a NumPy array first.
1. What is the shape of a 2 by 3 array?
2. What does arr.ndim report?
3. What does np.mean(values) calculate?
4. What must match for matrix multiplication?