What is NumPy?
NumPy — it is a versatile package for handling arrays. It provides a highperformance multidimensional array object and tools for working with these arrays.
This is a fundamental package for scientific computing with Python. It contains various functions, including the following important ones:
Besides its obvious scientific uses, NumPy can also be used as an efficient multidimensional shared data container …
Arbitrary data types can be defined using Numpy, allowing NumPy to easily and quickly integrate with a wide variety of databases.
Setting :
pip install numpy
Note. All of the examples below will not work in the interactive IDE.
1. Arrays in NumPy: The main object of NumPy is a homogeneous multidimensional array.
Example:
[[1, 2, 3], [ 4, 2, 5]] Here, rank = 2 (as it is 2dimensional or it has 2 axes) first dimension (axis) length = 2, second dimension has length = 3 overall shape can be expressed as: (2, 3)

Output:
Array is of type: No. of dimension s: 2 Shape of array: (2, 3) Size of array: 6 Array stores elements of type: int64
2. Creating Arrays. There are various ways to create arrays in NumPy.
Note: the type of an array can be explicitly specified when the array is created.
# Python program for demonstration
# array creation methods
import
numpy as np
# Create an array from a list with the float type
a
=
np.array ([[
1
,
2 ,
4
], [
5
,
8
,
7
]], dtype
=
`float`
)
print
(
"Array created using passed list: "
, a)
# Create an array from a tuple
b
=
np.array ((
1
,
3
,
2
))
print
(
"Array created using passed tuple:"
, b)
# Create a 3X4 array with all zeros
c
=
np.zeros ((
3 ,
4
))
print
(
"An array initialized with all zeros:"
, c)
# Create an array of constant complex values
d
=
np.full ((
3
,
3
),
6
, dtype
=
`complex`
)
print
(
" An array initialized with al l 6s. "
" Array type is complex: "
, d)
# Create an array with random values
e
=
np.random.random ((
2
,
2
))
print
(
"A random array:"
, e)
# Create a sequence of integers
# from 0 to 30 in steps of 5
f
=
np.arange (
0
,
30
,
5
)
print
(
"A sequential array with steps of 5: "
, f)
# Create a sequence of 10 values ranging from 0 to 5
g
= np.linspace (
0
,
5
,
10
)
print
(
"A sequential array with 10 values between"
"0 and 5:"
, g)
# Convert 3X4 array to 2X2X3 array
arr
=
np.array ([[
1
,
2
,
3
,
4
],
[
5
,
2
,
4
,
2
],
[
1
,
2
,
0
,
1
]])
newarr
=
arr.reshape (
2
,
2
,
3
)
print
(
"Original array:"
, arr)
print
(
"Reshaped array:"
, newarr)
# Flatten the array
arr
=
np.array ([ [
1
,
2
,
3
], [
4
,
5
, 6
]])
flarr
=
arr.flatten ()
print
(
"Original array:"
, arr)
print
(
"Fattened array:"
, flarr)
Exit:
Array created using passed list: [[1. 2. 4.] [5. 8. 7.]] Array created using passed tuple: [1 3 2] An array initialized with all zeros: [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] An array ini tialized with all 6s. Array type is complex: [[6. + 0.j 6. + 0.j 6. + 0.j] [6. + 0.j 6. + 0.j 6. + 0.j] [6. + 0.j 6. + 0.j 6. + 0.j]] A random array: [[0.46829566 0.67079389] [0.09079849 0.95410464]] A sequential array with steps of 5: [0 5 10 15 20 25] A sequential array with 10 values between 0 and 5: [0. 0.55555556 1.11111111 1.66666667 2.22222222 2.77777778 3.33333333 3.88888889 4.44444444 5.] Original array: [[1 2 3 4] [5 2 4 2] [1 2 0 1]] Reshaped array: [[ [1 2 3] [4 5 2]] [[4 2 1] [2 0 1]]] Original array: [[1 2 3] [4 5 6]] Fattened array: [1 2 3 4 5 6]
3. Array Indexing. Knowing the basics of array indexing is important for parsing and manipulating an array object. NumPy offers many ways to index arrays.
# Python program for demonstration
# numpy indexing
import
numpy as np
# Sample array
arr
=
np.array ([[

1
,
2
,
0
,
4
],
[
4
,

0.5
,
6
,
0
],
[
2.6
,
0
,
7
,
8
],
[
3
,

7
,
4
,
2.0
]])
# Slicing an array
temp
=
arr [:
2
, ::
2
]
print
(
" Array with first 2 rows and alternate "
" columns (0 and 2 ): "
, temp)
# An example of indexing an integer array
temp
=
arr [[
0
,
1
,
2
,
3
], [
3
,
2
,
1
,
0
]]
print
(
"Elements at indices (0, 3), (1, 2 ), (2, 1), "
" (3, 0): "
, temp)
# example of boolean array indexing
cond
=
arr & gt;
0
# cond is a boolean array
temp
=
arr [cond]
print
(
" Elements greater than 0: "
, temp)
Output:
Array with first 2 rows and alternatecolumns (0 and 2): [[1. 0.] [4. 6.]] Elements at indices (0, 3), (1, 2), (2, 1), (3, 0): [4. 6. 0. 3.] Elements greater than 0: [2. 4. 4. 6. 2.6 7. 8. 3. 4. 2.]
4. Basic Operations: NumPy provides many builtin arithmetic functions.

Output:
Adding 1 to every element: [2 3 6 4] Subtracting 3 from each element: [2 1 2 0] Multiplying each element by 10: [ 10 20 50 30] Squaring each element: [1 4 25 9] Doubled each element of original array: [2 4 10 6] Original array: [[1 2 3] [3 4 5] [9 6 0]] Transpose of array: [[1 3 9] [2 4 6] [3 5 0]]

Output:
Largest element is: 9 Rowwise maximum elements: [6 7 9] Columnwise minimum elements: [1 1 2] Sum of all array elements: 38 Cumulative sum along each row: [[1 6 12] [4 11 13] [3 4 13 ]]


