Skip to Main Content

Python for Basic Data Analysis

Start your data science journey with Python. Learn practical Python programming skills for basic data manipulation and analysis.

Creating arrays with NumPy functions

Creating arrays with NumPy functions

On this page are some other ways to create NumPy arrays. This will be useful for when you need to create large arrays and you don't want to type in each entry one by one as lists. 

Note: The NumPy functions below are all part of the NumPy package.

NumPy functions to create arrays

np.zeros

This function creates a new array filled with zeroes of given shape and type (optional). The codes below will return an array with 2 rows and 2 columns of zeros.
 

np.ones

This function creates a new array filled with ones of given shape and type (optional). The codes below will return an array with 2 rows and 2 columns of ones.
 

np.arange

This function creates an array of evenly spaced values within a defined interval according to the number of specified arguments. It is similar to Python's built-in function range().

Syntax

numpy.arange(start, stop, step) 

 

np.linspace

Similar to np.arange, this function creates an array of evenly spaced values. However, the difference is that you can specify number of elements instead of an interval or step. 

If you do not specify the sample size, it will print the default of 50 values. 
 

Syntax

np.linspace(start(inclusive),stop(inclusive), number of evenly spaced numbers)

 

np.full 

This function creates a full array with the same elements.

np.eye

This function creates an identity matrix, where 1’s fills the diagonals and 0’s elsewhere.
 

np.diag

This function extracts and creates a diagonal array from the given array.

np.random.random

This function creates an array with random floats between the intervals [0,1). You can also specify the size of your array. 
 

np.random.randint

This function creates an array with random integers between a specified range. You can also specify the size of your array. 
 

Video Guide

Exercises

Use the appropriate functions to solve the following questions

1. Create an array with 4 rows and 3 columns of zeros. Your results should look as below:

[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

2. Create an array of ones that has 3 rows and 4 columns. Your results should look as below:

[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]

3. Create an array containing integers 4 to 13 inclusive. Your results should look as below:

[4 5 6 7 8 9 10 11 12 13]

4. Create an array containing

[0., 1.5, 3., 4.5]


5. Create a 2 by 2 array containing '4' in each position. Your results should look like this:

[[4 4]
 [4 4]]

6. Create 2 matrices:

i. Identity matrix of size 4

[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]


ii. Diagonal matrix with [10,12] as the diagonals 

[[10  0]
 [ 0 12]]

7. Create a 3 by 3 array with random floats in [0, 10]. Your answer may be different because it is random but it should look something like this:

[[6.3685612  0.61720883 8.93157783]
 [3.69927617 5.79879583 7.62145626]
 [7.21895112 4.02011535 4.48844787]]

8. Create a 3 by 3 array with random integers in [10, 20]. Your answer may be different because it is random but it should look something like this:

[[19 11 13]
 [10 11 13]
 [11 14 10]]

1. Create an array with 4 rows and 3 columns of zeros


2. Create an array of ones that has 3 rows and 4 columns


3. Create an array containing integers 4 to 13 inclusive


4. Create an array containing [0., 1.5, 3., 4.5]


5. Create a 2 by 2 array containing '4' in each position


6. Create 2 matrices:

i. Identity matrix of size 4


ii. Diagonal matrix with [10,12] as the diagonals 


7. Create a 3 by 3 array with random floats in [0, 10]


8. Create a 3 by 3 array with random integers in [10, 20]