srs2010
Platform Insights Analyst
2
MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1
300 XP
In this tutorial you will learn:
NumPy Array
Array in NumPy is a data structure that is similar to Python lists but it's a lot more powerful since it allows us to manage N number of dimensions which helps us in making different mathematical calculations. We can define same type of elements in a NumPy array. The elements of a NumPy array are actually dtype objects ( data-type objects) and the array object in NumPy itself is called ndarray. We can use simple array function to create an a NumPy ndarray object. Let’s take a look at an example that creates NumPy array object and print the contents.
Dimensions of NumPy Array
One of the biggest advantages of NumPy array is that it allows us to create arrays that can have N-Dimensions. It means that we can have a very deeply nested array. Let’s have a look at examples in which we will create arrays that are 2 level, 3 level and 4 levels nested.
Example
In this example we are creating an array which is 2-Dimensional. For that we write both arrays in a NumPy array function separated by a comma.
In this example we are creating 2-D array by creating an array within an array.
Here we are creating 3-D array by creating an array within an array up to three levels.
Common Functions of NumPy Array
Now let’s have a look at the usage of some common functions of NumPy. NumPy gives us functions that can fill the array with ones, zeros and random numbers. These functions are simple yet very powerful while developing Machine Learning algorithms in Python.
Book traversal links for NumPy Array Creation
Download
- NumPy Array
- Dimensions of NumPy Array
- Common Functions of NumPy Array
NumPy Array
Array in NumPy is a data structure that is similar to Python lists but it's a lot more powerful since it allows us to manage N number of dimensions which helps us in making different mathematical calculations. We can define same type of elements in a NumPy array. The elements of a NumPy array are actually dtype objects ( data-type objects) and the array object in NumPy itself is called ndarray. We can use simple array function to create an a NumPy ndarray object. Let’s take a look at an example that creates NumPy array object and print the contents.
- import
numpy as
np
- nparr =
np.array
(
[
"Python"
,
"Test"
,
"Programming"
]
)
- print
(
"** Numpy Array Creation **"
)
- print
(
"Content of NumPy array: "
,
nparr)
Dimensions of NumPy Array
One of the biggest advantages of NumPy array is that it allows us to create arrays that can have N-Dimensions. It means that we can have a very deeply nested array. Let’s have a look at examples in which we will create arrays that are 2 level, 3 level and 4 levels nested.
Example
In this example we are creating an array which is 2-Dimensional. For that we write both arrays in a NumPy array function separated by a comma.
- nparr2 =
np.array
(
[
[
8
,
9
,
5
]
,
[
5
,
4
,
7
]
,
[
7
,
7
,
9
]
,
[
4
,
4
,
4
]
]
)
- print
(
"\n
\n
Contents of 2-D NumPy array: \n
"
,
nparr2)
In this example we are creating 2-D array by creating an array within an array.
- nparr2 =
np.array
(
[
[
8
,
9
,
5
]
,
[
5
,
4
,
7
]
,
[
7
,
7
,
9
]
,
[
4
,
4
,
4
]
]
)
- print
(
"\n
\n
Contents of 3-D NumPy array: \n
"
,
nparr2)
Here we are creating 3-D array by creating an array within an array up to three levels.
- nparr3 =
np.array
(
[
[
[
8
,
9
,
5
,
5
]
,
[
4
,
4
,
4
,
4
]
]
,
[
[
7
,
6
,
6
,
3
]
,
[
7
,
7
,
9
,
1
]
]
]
)
- print
(
"\n
\n
Contents of 4-D NumPy array: \n
"
,
nparr3)
Common Functions of NumPy Array
Now let’s have a look at the usage of some common functions of NumPy. NumPy gives us functions that can fill the array with ones, zeros and random numbers. These functions are simple yet very powerful while developing Machine Learning algorithms in Python.
- print
(
"\n
\n
Array full of 4"
,
np.full
(
(
3
,
3
)
,
4
)
)
- print
(
"\n
Random number array 3 rows 4cols"
,
np.random
.random
(
(
3
,
4
)
)
)
- print
(
"\n
Create Array of ones"
,
np.ones
(
(
2
,
4
)
)
)
- print
(
"\n
Create Array of zeros"
,
np.zeros
(
(
3
,
3
)
,
dtype=
np.int16
)
)
Book traversal links for NumPy Array Creation
- ‹ NumPy Accessing Arrays
- Up
- NumPy Iterator ›
Download
You must upgrade your account or reply in the thread to view hidden text.