0% found this document useful (0 votes)
13 views16 pages

Data Science - Sec5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views16 pages

Data Science - Sec5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Data

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 :

• To create a ndarray, we can pass a list, tuple or any array-like object


into the array() method, and it will be converted into a ndarray.
Dimensions in Arrays
• A dimension in arrays is one level of array depth (nested arrays).
• 0-D Arrays
✓0-D arrays, or Scalars, are the elements in an array.
✓ Each value in an array is a 0-D array.
• 1-D Arrays
✓An array that has 0-D arrays as its elements is called uni-dimensional or 1-D array.
✓These are the most common and basic arrays.
• 2-D Arrays
✓An array that has 1-D arrays as its elements is called a 2-D array.
✓These are often used to represent matrix .
• 3-D arrays
✓An array that has 2-D arrays (matrices) as its elements is called 3-D array.
✓These are often used to represent a 3rd order tensor.
Dimensions in Arrays
Check Number of Dimensions
• NumPy Arrays provides the ndim attribute that returns an integer that
tells us how many dimensions the array have.
• Higher Dimensional Arrays
✓ An array can have any number of dimensions.
✓ When the array is created, you can define the number of dimensions by using the
ndmin argument.
NumPy Array Indexing
• Access Array Elements
✓ Array indexing is the same as accessing an array element.
✓ You can access an array element by referring to its index number.
✓ The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has
index 1 etc.
NumPy Array Indexing (cont.)
• Negative Indexing
✓ Use negative indexing to access an array from the end.
NumPy Array Slicing
• Slicing arrays
• Slicing in python means taking elements from one given index to another given index.
• We pass slice instead of index like this: [start:end].
• We can also define the step, like this: [start:end:step].
✓ If we don't pass start it considered 0.
✓ If we don't pass end, it considered length of array in that dimension.
✓ If we don't pass step, it considered 1.
✓ Note: The result includes the start index but excludes the end index.

• Slicing 2-D Arrays


NumPy Data Types
• According python data types, NumPy has some extra data types, and refer to data
types with one character, like i for integers, f for float etc.
• NumPy offers a wider range of numerical data types than what is available in
Python. Here's the list of most commonly used numeric data types in NumPy.

• Checking the Data Type of an Array


• The NumPy array object has a property called dtype that returns the data type of the array.
NumPy Data Types
• Here, we have created types of arrays and
checked the default data types of these arrays
using the dtype attribute.
• int_array - contains four integer elements whose
default data type is int32
• float_array - contains three floating-point numbers
whose default data type is float64
• complex_array - contains three complex numbers
whose default data type is complex128
Creating NumPy Arrays With a Defined Data Type
• In NumPy, we can create an array with a defined data type by passing the dtype
parameter while calling the np.array() function.

• 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.

You might also like