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

Functions To Create Arrays

Uploaded by

Kang Chul
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Functions To Create Arrays

Uploaded by

Kang Chul
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Functions to Create Arrays

There are more convenience functions for creating fixed-sized arrays that you may encounter or
be required to use. Let’s look at just a few.

Empty
The empty() function will create a new array of the specified shape. The argument to the
function is an array or tuple that specifies the length of each dimension of the array to create.
The values or content of the created array will be random and will need to be assigned before
use. The example below creates an empty 3 × 3 two-dimensional array.
# create empty array
from numpy import empty
a = empty([3,3])
print(a)
Listing 4.3: Example of creating an array with the empty() function.
Running the example prints the content of the empty array. Your specific array contents
will vary.
[[ 0.00000000e+000 0.00000000e+000 2.20802703e-314] [ 2.20803350e-314 2.20803353e-314 2.20803356e-314]
[ 2.20803359e-314 2.20803362e-314 2.20803366e-314]]

Listing 4.4: Sample output of creating an array with the empty() function.
Zeros
The zeros() function will create a new array of the specified size with the
contents filled with zero values. The argument to the function is an array or
tuple that specifies the length of each dimension of the array to create. The
example below creates a 3 × 5 zero two-dimensional array.
# create
zero
array
from
numpy
import
zeros a
=
zeros([
3,5])
print(a)
Listing 4.5: Example of creating an array with the zeros() function.
Running the example prints the contents of the created zero array.
[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]

Listing 4.6: Sample output of creating an array with the zeros() function.

Ones
The ones() function will create a new array of the specified size with the
contents filled with one values. The argument to the function is an array or
tuple that specifies the length of each dimension of the array to create. The
# create one array from numpy import ones a = ones([5])
print(a)

example below creates a 5-element one-dimensional array.


Listing 4.7: Example of creating an array with the ones() function.
Running the example prints the contents of the created ones array.
[ 1. 1. 1. 1. 1.]

Listing 4.8: Sample output of creating an array with the ones() function.

Combining Arrays
NumPy provides many functions to create new arrays from existing arrays.
Let’s look at two of the most popular functions you may need or encounter.

Vertical Stack
Given two or more existing arrays, you can stack them vertically using the
vstack() function. For example, given two one-dimensional arrays, you can
create a new two-dimensional array with two rows by vertically stacking
them. This is demonstrated in the example below.

You might also like