Create NDArray

Create N-Dimensional Array

We are going to explore what are the ways to create arrays using NumPy library.

Create 1-dimensional array

import numpy as np

def test_run():
    print np.array([1, 2, 3])

if __name__ == "__main__":
    test_run()

Here is the ouput.

[1 2 3]

Create 2-dimensional array

import numpy as np

def test_run():
    print np.array([(1, 2, 3), (2, 3, 4)])

if __name__ == "__main__":
    test_run()

Here is the output.

Create an empty array

Here we create 3 arrays. First one is 1-dimensional, second is 2-dimensional and third is 3-dimensional.

When we run the code it does return arrays with some values. These values represent what ever value which was present in that part of memory (so, kind of random values).

Create array with default values

Here we create array with number 1 present in all cells.

Here is the array with all the ones.

Create typed array

We are going to create array of integers in this section.

Now we have created array full of integers.

Create array with random numbers

We need to use random module from numpy library.

Here is the array with random values.

Random numbers using Gaussian (normal) distribution

We will use 50 as mean and 10 as standard deviation.

Here is the output.

Generate array of random integers in a specific range

Here is the output.

Last updated

Was this helpful?