You can learn about NumPy in Python Programs with Outputs helped you to understand the language better.

Python Programming – Array Creation

There are several ways to create an array in NumPy like np. array, np. zeros, np. ones, etc. Each of them provides some flexibility. Syntax to create an array with important attributes of a Numpy object is
Syntax: NumPy.array (object, dtype=None,
copy=True, order=’K’, subok=False, ndmin=0)
Parameters:
Object: An array, any object exposing the array interface.
DType: Desired data type of array.
Copy: If true(default), then the object is copied.
Order ‘K’, ‘A’/C’/F’: Specify the memory layout of the array. If the object is not an array, the newly created array will be in C order (row-major) unless ‘F’ is specified, in which case it will be in Fortran order.
Subok: If True, then sub-classes will be passed- through, otherwise the returned array will be forced to be a base-class array,
ndmin: Specifies minimum dimensions of resultant
array.
Take a look at the following examples to under-stand better.
For example, to convert a list of numeric value into one-dimensional Numpy arrav as shown in Figure 13.8.
import numpy as np
List = [1,2,3,4]
al = np.array(List)
print(al)
The above code written in Python is shown in Figure 13.9:

Python Programming - Array Creation chapter 13 img 1

The simplest way to create an array in Numpy is to use Python List:
myPythonList = [1,9,8,3]
To convert Python list to a numpy array by using the object np.array:
numpy_array_from_list = np.array(myPythonList)

Methods used to create Numpy Array

Different methods used to create a Numpy array are discussed in the following sections.
To display the contents of the list:
numpy_array_from_list
>>>
array( [1, 9, 8, 3] )
>>>

empty ( )

the empty ( ) function is used to create empty arrays or an uninitialized array of specified shape and dtype, in which you can store actual data as and when required. (See Figure 13.10). Note that empty ( ) created array with random garbage values.

Example

Demo of empty ( ) function.

import numpy as np
arr1 = np.empty([3,2], dtype=int) #Empty
print(arr1)RUN
>>>
[ [ 6357069   6815843 ]
[7209065   4653157]
[6881397                100 ] ]
>>>

zeros ( )

The function zeros ( ) takes the same attributes as empty ( ) and creates an array with specifies size and type but filled with zeros. Default dtype is float. (See Figure 13.11).

Example
Demo of zero ( ) function.

# Creating a 2X3 array with all zeros
import numpy as np
c = np.zeros((2, 3))
print (“\nAn array initialized with all zeros:\n”, c)
print(“No. of dimensions: “, c.ndim)RUN
>>>
An array initialized with all zeros:
[ [0. 0. 0.]
[0.0.0.]]
No. of dimensions: 2
>>>

Let’s Try

import numpy as np
x = np.zeros((5,), dtype = int)
print ( x )

ones ( )

The function ones ( ) takes the same attributes as empty( ), and creates an array with specified size and type but filled with ones. (See Figure 13.12).

Example
Demo of one ( ) function.

import numpy as np
arr1 = np.ones([3,2],dtype=np.int64)
print(arr1)RUN
>>>
[[1 1]
[1 1]
[1 1 ] ]
>>>

Let’s Try

import numpy as np
x = np.ones ( 5 )
print ( x )

copy ( )

If you just assign a portion of an array to another array, the new array you just created actually refers to the parent array in memory. That means, if you make any changes to the new array, it will reflect in the parent array as well. So to avoid disturbing the parent array, you need to make a copy of it using All NumPy arrays come with the copy ( ) method. (See Figure 13.13).

Example
Demo of copy ( ) function.

import numpy as np
x=np.array([11,12,13])
y=x
z=np.copy(x)
x[0]=22
print(x)
print(y)
print(z)RUN
>>>
[22 12 13]
[22 12 13]
[11 12 13]
>>>

Leave a Reply

Your email address will not be published. Required fields are marked *