How_to_Create_Numpy_Arrays
How_to_Create_Numpy_Arrays
a. numpy.array function:
The basic ndarray is created using array function in NumPy as follows –
numpy.array
1|Page
Let us make Python programs to understand the ndarray better.
import numpy as np
a = np.array([4,6,8])
print(a)
Output
[4 6 8]
Output
[[3 4]
[9 6]]
3. Creating Array with minimum dimension: You can create an array with a
minimum dimension using the ndmin parameter when creating the array.
This parameter specifies the minimum number of dimensions that the
resulting array should have.
# minimum dimensions
import numpy as np
a = np.array([1, 2, 3,4,5], ndmin = 2)
print(a)
Output
[[1 2 3 4 5]]
2|Page
has a minimum of two dimensions, even though the input is a 1-
dimensional list. The output is a 2D array with a single row and five
columns.
# dtype parameter
import numpy as np
a = np.array([1, 2, 3], dtype = complex)
print(a)
Output
[1.+0.j 2.+0.j 3.+0.j]
The dtype parameter specifies the data type of array elements. Here,
dtype=complex is used to create an array of complex numbers from
the integer values 1, 2, and 3.
1. numpy.empty
2. numpy.zeros
3. numpy.ones
4. numpy.asarray
5. numpy.arange
6. numpy.linspace
7. numpy.logspace
3|Page
Let us discuss these routines one by one.
Syntax:
Example Code:
import numpy as np
x = np.empty([3,2], dtype = int)
print(x)
Output
[[0 0]
[0 0]
[0 0]]
4|Page
The constructor takes the following parameters:
3 Order - 'C' for C-style row-major array, 'F' for FORTRAN style
column-major array
Example Code 1:
Output
[0. 0. 0.]
This code creates a NumPy array named x with three elements, all
initialized to zero, and the default data type is float.
Example Code 2:
# custom type
import numpy as np
arr = np.zeros((2,2), dtype = [('a', 'i4'), ('b', 'i4')])
print(arr)
Output
[[(0, 0) (0, 0)]
[(0, 0) (0, 0)]]
The code creates a 2x2 NumPy array with a custom data type, where
each element is a tuple with two integer fields ('a' and 'b'). The array
is initialized with zeros, and the output shows the array with the
specified data type.
3. numpy.ones: It makes a new array with the specified size and data
type, where all elements are set to one.
5|Page
Syntax:
Example Code 1:
Output
[1. 1. 1. 1. 1.]
Example Code 2:
import numpy as np
arr = np.ones([2,2], dtype = int)
print(arr)
Output
[[1 1]
[1 1]]
6|Page
The code is creating a variable arr. It is a NumPy array with
dimensions 2x2, and all the elements in the array are set to the value
1 using np.ones() function.
Syntax:
Parameter
S.No. Parameter Description
Name
Input data in various forms, including lists,
1. a lists of tuples, tuples, tuple of tuples, or tuple
of lists.
By default, the data type of the input data is
2. dtype
assigned to the resulting ndarray.
Example Code 1:
x = [4,5,6]
arr = np.asarray(x)
print(arr)
Output
[4 5 6]
The Python list named x is converted into a NumPy array named arr.
The np.asarray() function is employed for this conversion.
7|Page
Example Code 2:
x = (1,2,3)
arr = np.asarray(x)
print(arr)
Output
[1 2 3]
Now that you know how to create a ndarray from existing data. Let us
learn how we can create an array from numerical ranges.
Syntax:
Example Code 1:
import numpy as np
arr = np.arange(3)
print(arr)
Output
[0 1 2]
8|Page
The code creates an array (arr) with elements ranging from 0 to 2
using the arange function.
Example Code 2:
import numpy as np
# dtype set
arr= np.arange(3, dtype = float)
print(arr)
Output
[0. 1. 2.]
A NumPy array 'arr' is created with values 0.0, 1.0, 2.0, having a data
type of float. It's generated using arrange function.
Example Code 3:
Output
[30 32 34 36 38]
Syntax:
9|Page
The constructor accepts the following parameters.
Example Code:
import numpy as np
arr= np.linspace(20,40,5)
print(arr)
Output
[20. 25. 30. 35. 40.]
Syntax:
10 | P a g e
The output of the logspace function is influenced by the following
parameters.
Example Code:
# set base of log space to 2
import numpy as np
arr = np.logspace(1,10,num = 10, base = 2)
print(arr)
Output
[ 2. 4. 8. 16. 32. 64. 128. 256. 512.
1024.]
11 | P a g e
ndArray Attributes
1. ndarray.shape
2. ndarray.ndim
3. numpy.itemsize
4. numpy.flags
Example Code 1:
import numpy as np
a = np.array([[4,5,6],[7,8,9]])
print(a.shape)
Output
(2,3)
The code creates a 2x3 array named a, and prints its shape, which
is (2, 3), indicating 2 rows and 3 columns.
Example Code 2:
a = np.array([[4,5,6],[7,8,9]])
a.shape = (3,2)
print(a)
Output
[[4 5]
[6 7]
[8 9]]
12 | P a g e
This code snippet creates a 2x3 array a, then resizes it to a 3x2 array
using the shape property. Finally, it prints the resized array.
import numpy as np
a = np.array([[4,5,6],[7,8,9]])
b = a.reshape(3,2)
print(b)
Output
[[4 5]
[6 7]
[8 9]]
The code creates a NumPy array a with shape (2,3), then reshapes it to
a new array b with shape (3,2), and finally prints the reshaped array b.
Example Code:
import numpy as np
Output
1
3
13 | P a g e
3. numpy.itemsize: This array attributes provides the size of each
element in the array, measured in bytes.
Output
4
14 | P a g e
Example Code:
import numpy as np
x = np.array([3,4,5,6,7])
print(x.flags)
Output
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
The code creates a NumPy array 'x' with elements [3,4,5,6,7] and
prints its flags indicating memory layout and properties.
15 | P a g e