0% found this document useful (0 votes)
1 views

-2

The document provides an overview of working with Numpy arrays, highlighting their use for mathematical calculations and data organization in structured formats. It covers various types of arrays, including 1-dimensional and 2-dimensional arrays, and discusses methods for creating and selecting data using indexing and slicing. Additionally, it introduces Matplotlib Pyplot for data visualization, offering various plotting options for exploratory data analysis.

Uploaded by

吳恩
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

-2

The document provides an overview of working with Numpy arrays, highlighting their use for mathematical calculations and data organization in structured formats. It covers various types of arrays, including 1-dimensional and 2-dimensional arrays, and discusses methods for creating and selecting data using indexing and slicing. Additionally, it introduces Matplotlib Pyplot for data visualization, offering various plotting options for exploratory data analysis.

Uploaded by

吳恩
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Working with Numpy Arrays

• Numpy: • Matplotlib Pyplot:


• Allows for effective mathematical • Pyplot works well together with data
calculations with large data sets stored in numpy arrays
• Numpy arrays are used to organize • It gives you a number of plotting options
data in a structured format for Exploratory Data Analysis:
• 1-dimensional arrays are like • Plots in a x-y coordinate system
vectors in mathematics • Histogram plots
• 2-dimensional arrays are like • Barplots
matrices in mathematics • Contour plots
• 3-dimensional plots
Higher-dimensional arrays are
• Animations
possible:
• Interactive plots
For example: Storing 4-dimensional
data from a weather forecast model • See the example page of Matplotlib for
with longitude, latitude, height, and the myriads of possibilities you have to
become creative!
time dimension.
Index system and selecting data from
numpy arrays using index operations
1-dimensional arrays examples:

Variable a is assigned the array:


We create it here from a list
object, and specifically say that
we want the numbers as float
numbers.
Index system and selecting data from
numpy arrays using index operations
1-dimensional arrays examples:

In data analysis we often need to select


A single element from the array or work
with a subset of the data. This is what we
call ‘slicing’ the data arrays.

1-dimensional arrays work with a single


index axis, similar to the list indexing.

Note: a[:] takes all elements. This can be useful


to help reading the code, because you see
immediately it is not just a scalar but an array.
The ‘:’ becomes very useful when we work with
2-dimensional data!
Index system and selecting data from
numpy arrays using index operations
1-dimensional arrays examples:

More details you can find in the numpy


tutorial online:
https://numpy.org/doc/stable/user/quick
start.html

Tip: Start with simple slicing methods


first!
Once you have a feeling for it, go on and
explore the advanced options, when you
feel that you need more advanced slicing
methods)
Creating numpy arrays with pre-filled values

1-dimensional arrays examples:


Typical methods used to create arrays: This fills the array with
np.empty() ‘random’ values! You
must assign proper
values to it afterwards

np.zeros()

Note: The main purpose of using these functions is to initialize variables with
the right-sized Numpy array. Then, you can fill the arrays in your code with
valuesduring your program execution.
Creating numpy arrays with pre-filled values

1-dimensional arrays examples:


Typical methods used to create arrays:

np.arange(start,end, increment)

np.linspace(start,end,number)

Often we need sequential equal-distanced numbers.


These two functions allow you to create numpy arrays this way.
2-dimensional arrays
2-dimensional arrays examples:

Most often, you will either read data from a spreadsheet table (or another 2-dim
data set) that creates 2-dimensional (or higher) dimensional arrays for you.

However, one often needs to also create variables that we can assign data and
store them in 2-dimensional arrays.
2-dimensional arrays examples:
Examples for 2-dimensional data arrays:

• Delta Airlines offers a price matrix for your flights when you choose flexible dates.
2-dimensional arrays examples:
We need two indices:
np.empty(shape=[2,3]) first index is the number
of rows, second index
the number of columns

np.zeros(shape[5,1])
It is a simplified version of a[2:len(a):2,0:len(a):2]

You might also like