Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
NumPy – Numerical Python is a library for the Python programming language, adding
support for large, multi-dimensional arrays and matrices, along with a large collection
of high-level mathematical functions to operate on these arrays.
How to install?
pip install numpy
1D and 2D Array
1D Array 2D Array
It is a single dimensional array i.e., many It is a two dimensional array
elements.
The first element is stored at index 0, second
at index 1 and so on.
Update Update
A[2] = 100 >>> a[1][1]=100
>>> print(a) >>> print(a)
[ 5 12 100 15 10] [[ 4 12 34]
[ 23 100 10]]
Append data
A1 = np.append(A, 23)
print(A1)
[5 12 100 15 10 23]
It adds only one data.
Create array with any values Create array with any values
>>> p = np.empty(5) >>> a = np.empty([2,2])
>>> print(p) >>> print(a)
[0.00000000e+000 0.00000000e+000 [[0.4375 2. ]
0.00000000e+000 4.94065646e-321 [1.09090909 0.75 ]]
Create array with zero and int data type Create array with zero and int data type
>>> p = np.zeros(5,dtype = int) >>> a = np.zeros([2,2],dtype = int)
>>> print(p) >>> print(a)
[0 0 0 0 0] [[0 0]
[0 0]]
Create array with 5 locations and value 8 Create array 2,2 with value 8
>>> p = np.full(5,8)
>>> print(p) >>> a = np.full([2,2],8)
[8 8 8 8 8] >>> print(a)
[[8 8]
[8 8]]
Create array with random function Create array with random function
>>> p = np.random.random(5) >>> a = np.random.random([2,2])
>>> print(p) >>> print(a)
[0.83377529 0.87501571 0.06585723 [[0.30769708 0.06059771]
0.30474124 0.11060145] [0.5178735 0.1086263 ]]
>>> b = np.reshape(a,(3,2))
>>> print(b)
[[1 2]
[3 4]
[5 6]]
Transpose – rows to columns
>>>C = np.transpose(b)
>>>print(c)
array([[1, 3, 5],
[2, 4, 6]])
Creation of 1D from 2D
>>>D = C.flatten()
>>>print(D)
Concatenate Concatenate
>>> import numpy as np >>> import numpy as np
>>> a = np.array([10,15,20]) >>> a = np.array([[1,2],[3,4]])
>>> b = np.array([12,14]) >>> print(a)
>>> c = np.concatenate([a,b,a]) [[1 2]
>>> print(c) [3 4]]
[10 15 20 12 14 10 15 20]
Concatenate row-wise
>>> b = np.concatenate([a,a])
>>> print(b)
[[1 2]
[3 4]
[1 2]
[3 4]]
By default, it concatenates row-wise.
Concatenate column-wise
>>> b = np.concatenate([a,a],axis = 1)
>>> print(b)
[[1 2 1 2]
[3 4 3 4]]
Vertical Stack
>>> c = np.array([100,100])
>>> print(np.vstack([a,c]))
[[ 1 2]
[ 3 4]
[100 100]]
Horizontal Stack
>>> d = np.array([[50],[50]])
>>> print(np.hstack([a,d]))
[[ 1 2 50]
[ 3 4 50]]
Slicing Slicing
>>> import numpy as np
>>> a = np.array([12,10,20,15,8])
>>> print(a)
[12 10 20 15 8]
>>> print(a[:])
[12 10 20 15 8] >>> import numpy as np
>>> print(a[1:3]) >>> a = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
[10 20]
>>> print(a[:2]) >>> print(a)
[12 10] [[ 1 2 3 4]
>>> print(a[-2:]) [ 5 6 7 8]
[15 8] [ 9 10 11 12]]
>>> print(a[:2,:3])
[[1 2 3]
[5 6 7]]
>>> print(a[:3,:2])
[[ 1 2]
[ 5 6]
[ 9 10]]
>>> print(a[:,0])
[1 5 9]
>>> print(a[0,:])
[1 2 3 4]
>>> print(a[:-1,:-1])
[[1 2 3]
[5 6 7]]
>>> print(a[::-1,::-1])
[[12 11 10 9]
[ 8 7 6 5]
[ 4 3 2 1]]
>>> print(a[::-1,:])
[[ 9 10 11 12]
[ 5 6 7 8]
[ 1 2 3 4]]
>>> print(a[:,::-1])
[[ 4 3 2 1]
[ 8 7 6 5]
[12 11 10 9]]
NumPy – Broadcasting
The term broadcasting refers to the ability of NumPy to treat arrays of different shapes
during arithmetic operations.
If two arrays are of exactly the same shape, arithmetic operations are usually
done on corresponding elements.
If two arrays are of different dimensions, the smaller array is broadcast to the
size of the larger array so that they have compatible shapes and arithmetic
operations are done on corresponding elements.
Numpy operations are usually done element-by-element which requires two arrays to have
exactly the same shape:
Example 1
The simplest broadcasting example occurs when an array and a scalar value are combined
in an operation:
The scalar b being stretched during the arithmetic operation into an array with the same
shape as a.
Example 2
Example 3
When the trailing dimensions of the arrays are unequal, broadcasting fails because it is
impossible to align the values in the rows of the 1st array with the elements of the 2nd arrays
for element-by-element addition.
Example 4
In some cases, broadcasting stretches both arrays to form an output array larger than either
of the initial arrays.
>>> import numpy as np
>>> a = np.array([[0],[10],[20],[3]])
>>> b = np.array([0,1,2])
>>> a
array([[ 0],
[10],
[20],
[ 3]])
>>> b
array([0, 1, 2])
>>> a+b
array([[ 0, 1, 2],
[10, 11, 12],
[20, 21, 22],
[ 3, 4, 5]])
The figure illustrates the stretching of both arrays to produce the desired 4x3 output array.
Array subset
import pandas as pd
import numpy as np
def sub_lists(list1):
# store all the sublists
sublist = [[]]
# first loop
for i in range(len(list1) + 1):
# second loop
for j in range(i + 1, len(list1) + 1):
# slice the subarray
sub = list1[i:j]
sublist.append(sub)
return sublist
x = np.array([1, 2, 3])
# driver code
print(sub_lists(x))