10 Ways to Create Numpy Arrays

A quick overview of different ways of creating NumPy arrays

Photo by Faris Mohammed on Unsplash

Numpy- Numerical Python

Numpy is multidimensional array library used for scientific computing.

Why use numpy:

Numpy is faster,convenient and takes less memory.

Numpy is homogeneous. Items in the array are of same data type.

Install numpy library:

pip install numpy

Import numpy library

import numpy as np

How to create numpy array:

Array creation
  1. array() — creates array from list/tuple

One dimensional arrays:

https://gist.github.com/IndhumathyChelliah/550fa24f39096ff9717f995d1c0950f2

A frequent error consists in calling array with multiple arguments, rather than providing a single sequence as an argument.

https://gist.github.com/IndhumathyChelliah/91c3054ac173db2c73f04e708faaab19

Two dimensional arrays:

array transforms sequences of sequences into two-dimensional arrays, sequences of sequences of sequences into three-dimensional arrays, and so on.

https://gist.github.com/IndhumathyChelliah/f9788e51621357cde3a1b5cc832f8473

Type of the array can be explicitly specified during creation.

https://gist.github.com/IndhumathyChelliah/59515e77b83c5773f71b8f4a027e617d

2. arange()

arange function is used to create sequence of numbers similar to Python built-in range function,but returns an array.

One dimensional array:

https://gist.github.com/IndhumathyChelliah/2cea996887b139a08ca78503bc57bd13

arange()-accepts floating-point arguments also.

https://gist.github.com/IndhumathyChelliah/2cea996887b139a08ca78503bc57bd13

Creating an array of float numbers.

https://gist.github.com/IndhumathyChelliah/64c148eee205503710fa05f4cb6da2e2

Creating an array of complex numbers

https://gist.github.com/IndhumathyChelliah/a7614b8058822116c9f5f32763e2d249


Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation.

zeros()
ones()
empty()

3. zeros:

Creates an array full of zeros.

https://gist.github.com/IndhumathyChelliah/2d032713c46dc3255bb8f60ab0fe4e08

zeros_like()

Return an array of zeros with the same shape and type as a given array.

https://gist.github.com/IndhumathyChelliah/629cf1b480619934d0b78095258228c7

4.ones:
Creates an array full of ones

https://gist.github.com/IndhumathyChelliah/4b81acb0891afd6bf80aa69932c0d11d

ones_like():

Return an array of ones with the same shape and type as a given array.

https://gist.github.com/IndhumathyChelliah/17b762e007d58688f4772bc49b961b31

5.empty

Function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.

https://gist.github.com/IndhumathyChelliah/5d98316078a36f2a08a3be8a9d1c732d

6.full

creates an array filled with the given value.

https://gist.github.com/IndhumathyChelliah/60ffe1f1cf64ac81417bb4ac113d6c0f

full_like()

Return a full array with the same shape and type as a given array.

https://gist.github.com/IndhumathyChelliah/c24b833504456d3c39b8248d4c7453c4

7.linspace:
creates an array filled with evenly spaces samples

Returns 6 evenly spaced samples, calculated over the interval 1,10.

retstep-returns the step.

https://gist.github.com/IndhumathyChelliah/6b6c76f8a2b1f9bdc2e9ef89d3160edd

The endpoint is excluded:

https://gist.github.com/IndhumathyChelliah/8b9df15661d3a6e445e7eda0324e74ad

8.eye()

Returns array filled with zeros except the kth diagonal, whose values are equal to 1.

https://gist.github.com/IndhumathyChelliah/a1010704e56207a1f91e0caa0381955e

k=-1 means the lower part of the diagonal will be one

https://gist.github.com/IndhumathyChelliah/6996ba533461731be2fbc3b934c7bb84

k=1 means the upper part of the diagonal will be one.

https://gist.github.com/IndhumathyChelliah/9054a3b64c7a4628e825d2514019fc90https://gist.github.com/IndhumathyChelliah/faa6aeb787eb6ce980416f1f9f3456f7

9.identity

Returns square array with ones on the main diagonal.

https://gist.github.com/IndhumathyChelliah/c79f5a6e93a63d6f49df7caf8050494f

10.random
creates an array of given shape 5 with random samples between 0 and 1

https://gist.github.com/IndhumathyChelliah/a6398689fc52fe87f5eece976c40b225

creates an array of shape(2,2) with random samples from 1 to 5

https://gist.github.com/IndhumathyChelliah/3a00df412a7a9e2599d3786593170413

Conclusion:

  • The function zeros creates an array full of zeros, the function ones creates an array full of ones, and the function empty creates an array whose initial content is random and depends on the state of the memory. By default, the dtype of the created array is float64.
  • Difference between range() and arange()

difference between range() and arange()

Resources:

Array Creation:

https://numpy.org/doc/1.19/user/quickstart.html#array-creation

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s