Professional Documents
Culture Documents
The numpy library is one of the core packages in Python's scientific software stack. Many other
Python data analysis libraries require numpy as a prerequisite, because they use its ndarray data
structure as a building block. The Anaconda Python distribution we installed in part 1 comes with
numpy.
Numpy implements a data structure called the N-dimensional array or ndarray. ndarrays are
similar to lists in that they contain a collection of items that can be accessed via indexes. On the
other hand, ndarrays are homogeneous, meaning they can only contain objects of the same type
and they can be multi-dimensional, making it easy to store 2-dimensional tables or matrices.
To work with ndarrays, we need to load the numpy library. It is standard practice to load numpy
with the alias "np" like so:
The "as np" after the import statement lets us access the numpy library's functions using the
shorthand "np."
arr1 = np.array(data1)
print(arr1)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
print("Class of data is : ",data1.__class__)
[[1 2 3 4]
[5 6 7 8]]
Dimension of array is : 2
Shape of array is : (2, 4)
Shape of array is: int32
Class of data is : <class 'list'>
Class of array is : <class 'numpy.ndarray'>
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In [5]: my_list = [1, 2, 3, 4] # Define a list
my_array = np.array(my_list) # Pass the list to np.array()
type(my_array) # Check the object's type
Out[5]: numpy.ndarray
To create an array with more than one dimension, pass a nested list to np.array():
[[1 2 3 4]
[5 6 7 8]]
An ndarray is defined by the number of dimensions it has, the size of each dimension and the
type of data it holds. Check the number and size of dimensions of an ndarray with the shape
attribute:
In [7]: two_d_array.shape
Out[7]: (2, 4)
The output above shows that this ndarray is 2-dimensional, since there are two values listed, and
the dimensions have length 2 and 4. Check the total size (total number of items) in an array with
the size attribute:
In [8]: two_d_array.size
Out[8]: 8
Check the type of the data in an ndarray with the dtype attribute:
In [9]: two_d_array.dtype
Out[9]: dtype('int32')
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Numpy has a variety of special array creation functions. Some handy array creation functions
include:
In [10]: # np.identity() to create a square 2d array with 1's across the diagona
l
np.identity(n = 5) # Size of the array
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Array Indexing and Slicing
Numpy ndarrays offer numbered indexing and slicing syntax that mirrors the syntax for Python
lists:
Out[14]: 4
If an ndarray has more than one dimension, separate indexes for each dimension with a comma:
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]]
Out[18]: 11
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Out[19]: array([[11, 12],
[17, 18]])
Reshaping Arrays
Numpy has a variety of built in functions to help you manipulate arrays quickly without having to
use complicated indexing operations.
Reshape an array into a new array with the same data but different structure with np.reshape():
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In [23]: np.ravel(a=two_d_array, order='F') # Use Fortran-style unraveling (by
columns)
In [24]: two_d_array.flatten()
In [25]: two_d_array.T
JOINING ARRAYS/CONCATENATION
Join arrays along an axis with np.concatenate():
In [38]: two_d_array
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
[13, 14, 15, 16, 17, 18]])
-----------------------------------------------------------------------
----
AxisError Traceback (most recent call l
ast)
<ipython-input-27-ea78051e0a29> in <module>()
1 array_to_join = np.array([[10,20,30],[40,50,60],[70,80,90]])
2 np.concatenate( (two_d_array,array_to_join), #
Arrays to join
----> 3 axis=2) #
Axis to join upon
-----------------------------------------------------------------------
----
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
ValueError Traceback (most recent call l
ast)
<ipython-input-39-adc6385139bc> in <module>()
1 array_to_join = np.array([[10,20,30],[40,50,60],[70,80,90]])
2 np.concatenate( (two_d_array,array_to_join), #
Arrays to join
----> 3 axis=0) #
Axis to join upon
ValueError: all the input array dimensions except for the concatenation
axis must match exactly
In [42]: array_to_join1.shape
Out[42]: (3, 6)
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Mathematical Operations using two different
arrays
you can also use the basic math operators on two arrays with the same shape. When operating
on two arrays, the basic math operators function in an element-wise fashion, returning an array
with the same shape as the original:
[[1 2]
[3 4]]
Out[49]: array([[2, 4],
[6, 8]])
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
[ 27, 256]], dtype=int32)
MEAN
In [54]: print(two_d_array)
np.mean(two_d_array) # Get the mean of all the elements in an array
with np.mean()
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]]
Out[54]: 9.5
STANDARD DEVIATION
In [57]: # Get the standard deviation all the elements in an array with np.std()
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In [58]: np.std(two_d_array)
Out[58]: 5.188127472091127
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
1.79175947],
[1.94591015, 2.07944154, 2.19722458, 2.30258509, 2.39789527,
2.48490665],
[2.56494936, 2.63905733, 2.7080502 , 2.77258872, 2.83321334,
2.89037176]])
RANDOM IN NUMPY
In [65]: np.random
Out[66]: array([0.87829669])
In [68]: np.random.randint(1)
Out[68]: 0
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In [69]: np.random.randint(5)
Out[69]: 1
In [71]: lista=[1,2,3,4,5,6,7,8,9,10]
ary1=np.array(lista)
print(ary1.min()) #find out minimum value in an a
rray
print(ary1.max()) #find out maximum value in an a
rray
print(ary1.argmax()) #find out the index of the max
value in an array
print(ary1.argmin()) #find out the index of the min
value in an array
1
10
9
0
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Matrix(2,5) :
[[0.90348546 0.94223808 0.60636848 0.39800106 0.60532874]
[0.82422691 0.95672622 0.93134467 0.00516792 0.21928855]]
Matrix after appending (2,6) :
[[0.90348546 0.94223808 0.60636848 0.39800106 0.60532874 0. ]
[0.82422691 0.95672622 0.93134467 0.00516792 0.21928855 0. ]]
Matrix after appending (3,5) :
[[0.90348546 0.94223808 0.60636848 0.39800106 0.60532874]
[0.82422691 0.95672622 0.93134467 0.00516792 0.21928855]
[0. 0. 0. 0. 0. ]]
In [74]: a1=np.linspace(3,5,3)
print("First array : ",a1)
a2=np.linspace(4,4,3)
print("Second array : ",a2)
print("Using greater_equal function ; ",np.greater_equal(a1,a2))
print("Using equal function :",np.equal(a1,a2))
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD