Using_Array_Creation_Routines
Using_Array_Creation_Routines
Syntax:
Shape
1
Shape of an empty array in int or tuple of int
Dtype
2
Desired output data type. Optional
Order
3 'C' for C-style row-major array, 'F' for FORTRAN style column-
major array
1|Page
Example Code:
import numpy as np
x = np.empty([3,2], dtype = int)
print(x)
Output
[[0 0]
[0 0]
[0 0]]
Syntax:
Order - 'C' for C-style row-major array, 'F' for FORTRAN style
3
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.
2|Page
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.
Syntax:
Shape
1
Shape of an empty array in int or tuple of int
Dtype
2
Desired output data type. Optional
Order
3 'C' for C-style row-major array, 'F' for FORTRAN style column-
major array
3|Page
Example Code 1:
Output
[1. 1. 1. 1. 1.]
The code uses NumPy to create an array of five floating-point elements, all
set to 1. The np.ones(5) function is used, and the resulting array is printed.
Example Code 2:
import numpy as np
arr = np.ones([2,2], dtype = int)
print(arr)
Output
[[1 1]
[1 1]]
Syntax:
4|Page
The constructor accepts the following parameters:
Parameter
S.No. Parameter Description
Name
Input data in various forms, including lists, lists
1. a
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.
C (row major) or F (column major). The
3. order
default is C.
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.
Example Code 2:
x = (1,2,3)
arr = np.asarray(x)
print(arr)
Output
[1 2 3]
The code is converting a tuple named x into a NumPy array named arr
using the as array() function.
5|Page
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]
Example Code 2:
import numpy as np
# dtype set
arr= np.arange(3, dtype = float)
print(arr)
Output
[0. 1. 2.]
6|Page
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:
7|Page
Example Code:
import numpy as np
arr= np.linspace(20,40,5)
print(arr)
Output
[20. 25. 30. 35. 40.]
Syntax:
8|Page
Example Code:
Output
[ 2. 4. 8. 16. 32. 64. 128. 256. 512.
1024.]
This code creates an array (arr) with ten logarithmically spaced values
between 2 and 1024. The logarithmic scale has a base of 2, achieved by
setting the base parameter in the logspace function.
You now know how to create NumPy arrays. Let us know characteristics
of NumPy arrays, including details such as data type, size, memory
usage, etc.
9|Page