Data Science - Sec5
Data Science - Sec5
Science
Section5
NumPy
NumPy
• NumPy is a Python library used for working with arrays.
• It also has functions for working in domain of linear algebra, fourier
transform, and matrices.
• NumPy stands for Numerical Python.
• NumPy aims to provide an array object that is up to 50x faster than
traditional Python lists.
• The array object in NumPy is called ndarray, it provides a lot of supporting
functions that make working with ndarray very easy.
• Arrays are very frequently used in data science, where speed and resources
are very important.
• Install numpy using this command:
NumPy
• we can create n-dimensional array using array method.
• Create 1-D array example :
• Since we have set the data type of the array to int32, each element of the array is
represented as a 32-bit integer.
• We can convert from one datatype to another Using the same dtype parameter:
Converting Data Type on Existing Arrays
• The astype() function creates a copy of the array, and
allows you to specify the data type as a parameter.
• The data type can be specified using a string, like 'f'
for float, 'i' for integer etc. or you can use the data
type directly like float for float and int for integer.
NumPy Array Copy vs View
• The copy owns the data and any changes made to the copy will not affect original array,
and any changes made to the original array will not affect the copy.
• The view does not own the data and any changes made to the view will affect the original
array, and any changes made to the original array will affect the view.
• The main difference between a copy and a view of an array is that the copy is a new
array, and the view is just a view of the original array.
Check if Array Owns its Data
• As mentioned above, copies owns the data, and views does not own the data, but how
can we check this?
• Every NumPy array has the attribute base that returns None if the array owns the data.
• Otherwise, the base attribute refers to the original object.