Advertisement
zeros(shape, datatype, order)
Parameters | Description |
---|---|
shape | shape is shape of the array and could be given either of the values - - An integer value i.e. total number of elements in a 1D array. - A list of integer values i.e. dimensions of a multi-dimensional array. This is a non-optional attribute. |
datatype | datatype is the data type of the elements in the array, it could be given either of the values like int, float, str, double..
This is an optional attribute and its default value is float. |
order | order is the order to store multi-dimensional array i.e. row-major (C-style)
or column-major (Fortran-style) order in memory. This is an optional attribute and usually we don't specify it. |
#Python - Array from Numpy Module
from numpy import zeros
# Calling zeros() to create an array of 2 int values
a = zeros([2], int)
print('Array of two int values : ', a)
# Calling zeros() to create an array of 2 float values
a = zeros([2], float)
print('Array of two float values : ', a)
# Calling zeros() to create an array of 4 float values i.e datatype is float by default.
a = zeros(4)
print('Array of four float values : ', a)
# Calling zeros() to create an array of 2 double values
a = zeros([2], double)
print('Array of two double values : ', a)
# Calling zeros() to create an array of 3 str values i.e. string values
a = zeros([3], str)
print('Array of three str values : ', a)
# Calling zeros() to create an array of 2 bool i.e. boolean values
a = zeros([2], bool)
print('Array of two bool values : ', a)
Array of two int values : [0 0]
Array of two float values : [0. 0.]
Array of four float values : [0. 0. 0. 0.]
Array of two double values : [0. 0.]
Array of three str values : ['' '' '']
Array of two bool values : [False False]]
Advertisement
#Python - Calling ones() function of Numpy Module
from numpy import ones
# Calling ones() to create an array of 2 int values
a = ones([2], int)
print('Array of two int values : ', a)
# Calling ones() to create an array of 2 float values
a = ones([2], float)
print('Array of two float values : ', a)
# Calling ones() to create an array of 4 float values i.e datatype is float by default.
a = ones(4)
print('Array of four float values : ', a)
# Calling ones() to create an array of 2 double values
a = ones([2], double)
print('Array of two double values : ', a)
# Calling ones() to create an array of 3 str values i.e. string values
a = ones([3], str)
print('Array of three str values : ', a)
# Calling ones() to create an array of 2 bool i.e. boolean values
a = ones([2], bool)
print('Array of two bool values : ', a)
Array of two int values : [1 1]
Array of two float values : [1. 1.]
Array of four float values : [1. 1. 1. 1.]
Array of two double values : [1. 1.]
Array of three str values : ['1' '1' '1']
Array of two bool values : [ True True]
#Python - Calling array() function of Numpy Module
from numpy import array
# Calling array() to create an array of int values
a = array([5, 2, 7, 8], int)
print('Array of int values : ', a)
# Calling array() to create an array of float values
a = array([2.4, 7, 8.5], float)
print('Array of float values : ', a)
# Calling array() to create an array of type based on the type of values
a = array([5, 2, 7, 8])
print('Array of int values : ', a)
# Calling array() to create an array of double values
a = array([5.94, 2.34, 7, 8], double)
print('Array of double values : ', a)
# Calling array() to create an array of str i.e. string values
a = array(['Hi', 'XYZ', 8], str)
print('Array of str values : ', a)
# Calling array() to create an array of bool i.e. boolean values
a = array([False, True, True, 8], bool)
print('Array of bool values : ', a)
Array of int values : [5 2 7 8]
Array of float values : [2.4 7. 8.5]
Array of values : [5 2 7 8]
Array of double values : [5.94 2.34 7. 8. ]
Array of str values : ['Hi' 'XYZ' '8']
Array of bool values : [False True True True]
#Python - Extracting an element from an array using Numpy Module
from numpy import array
# Calling array() to create an array of int values
a = array([5, 2, 7, 8], int)
print('Array of two int values : ', a)
# Extracting the first element of an array using its index i.e 0
print(a[0])
# Extracting the second element of an array using its index i.e 1
print(a[1])
# Extracting the third element of an array using its index i.e 2
print(a[2])
# Extracting the four element of an array using its index i.e 3
print(a[3])
5
2
7
8
#Python - Traversing through an array using Numpy Module
from numpy import array
# Calling array() to create an array of int values
a = array([1, 5, 4, 3, 2], int)
# Traversing through the array and printing its each element using a for loop
for x in a:
print(x)
1
5
3
4
2
#Python - Traversing through an array using Numpy Module
from numpy import array
# Calling array() to create an array of int values
a = array([1, 5, 4, 3, 2], int)
# The index of first element of an array
i = 0
# Using while loop to extract each of its element using while loop
# And len() function which takes an iterable type in its parameters i.e. list, tuple, array etc
while(i<len(a)):
print(a[i])
i = i + 1
1
5
3
4
2
insert(arr, obj, values, axis)
Parameters | Description |
---|---|
arr | The name of array on which we want to perform the insert operation.
This is a non-optional attribute. |
obj | This is an object that defines the index position(where we want to insert the new element).
.
This is an non-optional attribute. |
values | The values that we want to insert in an array.
This is an non-optional attribute. |
axis | This is the axis along we are going to insert the values-
- if axis is 1, the value is inserted in the column. - if axis is 0, the value is inserted in the row. This is an optional attribute. If the value of axis is not provided then we get a flattened array. |
#Python - Inserting an element into an array using Numpy Module
from numpy import *
# Calling array() to create an array of 2 int values
a = array([5, 2, 7, 8], int)
print('Array of int values : ', a)
# Calling the insert() function to insert integer value 99 at index 2 i.e. starting index in array is 0
a = insert(a, 2, 99)
print('Modified array of int values : ', a)
Array of int values : [5 2 7 8]
Modified array of int values : [5 2 99 7 8]
delete(arr, obj, axis)
Parameters | Description |
---|---|
arr | The name of array on which we want to perform the delete operation.
This is a non-optional attribute. |
obj | This is an object that defines the index position(where we want to delete the element).
This is an non-optional attribute. |
axis | This is the axis along we are going to insert the values -
- if axis is 1, the value is deleted from the column. - if axis is 0, the value is deleted from the row. This is an optional attribute. If the value of axis is not provided then axis is assigned None and the obj index is applied to the flattened array and we get a flattened array in return. |
#Python - Deleting an element from an array using Numpy Module
from numpy import *
# Calling array() to create an array of 2 int values
a = array([5, 2, 7, 8], int)
print('Array of int values : ', a)
# Calling the delete() function to delete the value at index 2 i.e. starting index in array is 0
a = delete(a, 2)
print('Modified array of int values : ', a)
Array of int values : [5 2 7 8]
Modified array of int values : [5 2 8]
append(arr, values, axis)
Parameters | Description |
---|---|
arr | The name of array on which we want to perform the append operation.
This is a non-optional attribute. |
values | The values that we want to append to an array.
This is an non-optional attribute. |
axis | This is the axis along we are going to append the values-
- if axis is 0, the value is appended to the last row i.e. at the end of a 2D array. This is an optional attribute. If the value of axis is not provided then we get a flattened array. |
#Python - Appending elements tp an array using Numpy Module
from numpy import *
# Calling array() to create an array of int values
a = array([5, 2, 7, 8], int)
print('Array of int values : ', a)
# Calling the append() function to append a list of elements to its end
a = append(a, [66,33,44])
print('Modified array of int values : ', a)
Array of int values : [5 2 7 8]
Modified array of int values : [5 2 7 8 66 33 44]
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement