Advertisement
zeros(shape, datatype, order)
Parameters | Description |
---|---|
shape | shape is the shape of an array and could be given either of the values - - An integer value i.e. the total number of elements in a 1D array. - A list of integer values i.e. the dimensions of a multi-dimensional array. This is a non-optional attribute. |
datatype | datatype is the data type of the elements in the array, could be given either of the values like int, float, str, double.
This is an optional attribute and its default value is a float. |
order | order is the order which is used to specify whether to store multi-dimensional array in row-major (C-style)
or column-major (Fortran-style) order in memory. This is an optional attribute and usually, we don't specify it. |
from numpy import zeros
# Calling zeros() to create an int array of 2 rows and 2 columns
a = zeros([2,2], int)
print('A 2D array of int values : ')
print(a)
# Calling zeros() to create a float array of 2 rows and 3 columns
a = zeros([2,3], float)
print('A 2D array of float values : ')
print(a)
# Calling zeros() to create a float array of 2 rows and 2 columns i.e datatype is float by default.
a = zeros([2,2])
print('A 2D array of default float values : ')
print(a)
# Calling zeros() to create a double array of 2rows and 2 columns
a = zeros([2,2], double)
print('A 2D array of of double values : ')
print(a)
# Calling zeros() to create a string array of 2 rows and 4 columns
a = zeros([2,4], str)
print('A 2D array of string values : ')
print(a)
# Calling zeros() to create a bool array of 2 rows and 2 column bool
a = zeros([2,1], bool)
print('A 2D array of bool values : ')
print(a)
A 2D array of int values :
[[0 0]
[0 0]]
A 2D array of float values :
[[0. 0. 0.]
[0. 0. 0.]]
A 2D array of default float values :
[[0. 0.]
[0. 0.]]
A 2D array of of double values :
[[0. 0.]
[0. 0.]]
A 2D array of string values :
[['' '' '' '']
['' '' '' '']]
A 2D array of bool values :
[[False]
[False]]
Advertisement
from numpy import ones
# Calling ones() to create an int array of 2 rows and 2 columns
a = ones([2,2], int)
print('A 2D array of int values : ')
print(a)
# Calling ones() to create a float array of 2 rows and 3 columns
a = ones([2,3], float)
print('A 2D array of float values : ')
print(a)
# Calling ones() to create a float array of 2 rows and 2 columns i.e datatype is float by default.
a = ones([2,2])
print('A 2D array of default float values : ')
print(a)
# Calling ones() to create a double array of 2rows and 2 columns
a = ones([2,2], double)
print('A 2D array of of double values : ')
print(a)
# Calling ones() to create a string array of 2 rows and 4 columns
a = ones([2,4], str)
print('A 2D array of string values : ')
print(a)
# Calling ones() to create a bool array of 2 rows and 2 column bool
a = ones([2,1], bool)
print('A 2D array of bool values : ')
print(a)
A 2D array of int values :
[[1 1]
[1 1]]
A 2D array of float values :
[[1. 1. 1.]
[1. 1. 1.]]
A 2D array of default float values :
[[1. 1.]
[1. 1.]]
A 2D array of of double values :
[[1. 1.]
[1. 1.]]
A 2D array of string values :
[['' '' '' '']
['' '' '' '']]
A 2D array of bool values :
[[True]
[True]]
zeros(list, datatype)
Parameters | Description |
---|---|
list | A list is a list containing multiple lists, where each list corresponds to an array of values.
A list of values is always contained in square brackets i.e. [ ]
This is a non-optional attribute. |
datatype | datatype is the datatype of the elements in the array,
could be given either of the values like int, float, str, double.
If the values passed in a list are not exactly of the datatype mentioned in the array function, they
will be converted to the mentioned data type.
This is an optional attribute and its default value is a float. |
from numpy import array
# Calling array() to create an int array of 2 rows and 3 columns
a = array([[5,4,3], [4,2,1]], int)
print('A 2D array of int values : ')
print(a)
# Calling array() to create an float array of 2 rows and 2 columns
a = array([[4.3,5.2], [1.1,2.3]], float)
print('A 2D array of float values : ')
print(a)
# Calling array() to create an array(2 rows and 3 columns) of type based on the type of values
a = array([[4.3,5.2,1.2], [1.1,2.3,3.4]])
print('A 2D array of float values : ')
print(a)
# Calling array() to create a double array of 3 rows and 2 columns
a = array([[2.2, 3.4], [4.5,6.7], [3.4,7.8]], double)
print('A 2D array of of double values : ')
print(a)
# Calling array() to create a string array of 2 rows and 3 columns
a = array([['hi','ABC', '8'], ['A', 'hello', 'Z']], str)
print('A 2D array of string values : ')
print(a)
# Calling array() to create a bool array of 3 rows and 2 columns
a = array([[True,False], [False,True], [True,True]], bool)
print('A 2D array of bool values : ')
print(a)
A 2D array of int values :
[[5 4 3]
[4 2 1]]
A 2D array of float values :
[[4.3 5.2]
[1.1 2.3]]
A 2D array of float values :
[[4.3 5.2 1.2]
[1.1 2.3 3.4]]
A 2D array of of double values :
[[2.2 3.4]
[4.5 6.7]
[3.4 7.8]]
A 2D array of string values :
[['hi' 'ABC' '8']
['A' 'hello' 'Z']]
A 2D array of bool values :
[[ True False]
[False True]
[ True True]]
from numpy import *
# Calling array() to create an int array of 2 rows and 3 columns
a = array([[5,4,3], [4,2,1]], int)
print('A 2D array of int values : ')
print(a)
# Extracting the first element of an array using its index [0][0]
print('Value at 0th row and 0th column : ', a[0][0])
# Extracting the second element of an array using its index [0][1]
print('Value at 0th row and 1st column : ', a[0][1])
# Extracting the third element of an array using its index [0][2]
print('Value at 0th row and 2nd column : ', a[0][2])
# Extracting the four element of an array using its index [1][0]
print('Value at 1st row and 0th column : ', a[1][0])
# Extracting the four element of an array using its index [1][1]
print('Value at 1st row and 1st column : ', a[1][1])
# Extracting the four element of an array using its index [1][2]
print('Value at 1st row and 2nd column : ', a[1][2])
A 2D array of int values :
[[5 4 3]
[4 2 1]]
Value at 0th row and 0th column : 5
Value at 0th row and 1st column : 4
Value at 0th row and 2nd column : 3
Value at 1st row and 0th column : 4
Value at 1st row and 1st column : 2
Value at 1st row and 2nd column : 1
from numpy import array
# Calling array() to create an int array of 2 rows and 3 columns
a = array([[5,4,3], [4,2,1]], int)
print('A 2D array of int values : ')
print(a)
print('Accessing each array element using for loop :')
# Traversing through the 2D array and printing its each element using a nested for loop
# Outer for loop is used to access each list i.e. each array of values, within a list
# the inner for loop is used to access values of individual list i.e. array.
for x in a:
for y in x:
print(y)
A 2D array of int values :
[[5 4 3]
[4 2 1]]
Accessing each array element using for loop :
5
4
3
4
2
1
import array as arr
# Calling array() to create an int array of 2 rows and 3 columns
a = array([[5,4,3], [4,2,1]], int)
# The index of first element of an array i.e. [0][0]
x = 0
y = 0
print('Accessing each array element using while loop :')
# The first len() function gives us the total number of lists in a list
# The second len() function gives us total number of values in the first list i.e. a[0] and all the lists have same number of values
# Outer for loop is used to access each list i.e. each array of values, within a list
# the inner whule loop is used to access values of an individual list i.e. array.
while(x<len(a)):
while(y<len(a[0])):
print(a[x][y])
y = y + 1
x = x + 1
y = 0
A 2D array of int values :
[[5 4 3]
[4 2 1]]
Accessing each array element using while loop :
5
4
3
4
2
1
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 | This 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. |
from numpy import *
# Calling array() to create an int array of 2 rows and 3 columns
a = array([[5,4,3], [4,2,1]], int)
print('Array of int values : ', a)
# Calling the insert() function to insert integer value 7 at index 2 in axis = 1 i.e. in the 2nd row
a = insert(a, 2, 7, axis=0)
print('Modified array of int values : ')
print(a)
# Calling the insert() function to insert integer value 9 at index 1 in axis = 1 i.e. in the 1st column
a = insert(a, 1, 9, axis=1)
print('Modified array of int values : ')
print(a)
Array of int values :
[[5 4 3]
[4 2 1]]
Modified array of int values :
[[5 4 3]
[4 2 1]
[7 7 7]]
Modified array of int values :
[[5 9 4 3]
[4 9 2 1]
[7 9 7 7]]
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 the axis is 1, the value is deleted from the column. - if the axis is 0, the value is deleted from the row. This is an optional attribute. If the value of axis is not provided then the axis is assigned None and the obj index is applied to the flattened array and we get a flattened array in return. |
from numpy import *
# Calling array() to create an int array of 2 rows and 3 columns
a = array([[5,4,3], [4,2,1]], int)
# Calling the delete() function to delete from index 1 and axis = 0 i.e. 0th row
a = delete(a, 0, 0)
print('Modified array of int values : ')
print(a)
# Calling the delete() function to delete from index 1 and axis = 1 i.e. 1st column
a = delete(a, 1, axis=1)
print('Modified array of int values : ')
print(a)
Array of int values :
[[5 4 3]
[4 2 1]]
Modified array of int values :
[[4 2 1]]
Modified array of int values :
[[4 1]]
append(arr, values, axis)
Parameters | Description |
---|---|
arr | The name of the array on which we want to perform the append operation.
This is a non-optional attribute. |
values | The values that we want to append in an array.
This is a non-optional attribute. |
axis | This is the axis along we are going to append the values-
- if the 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 axis is assigned None and the append operation is is applied to the flattened array and we get a flattened array in return. |
from numpy import *
print('Array of int values : ')
print(a)
# Calling the append() function to append a list of elements to its end # Calling the delete() function to delete from index 0 and axis = 0 i.e. 0th row
a = append(a, [[66,33,44]], 0)
# Calling the append() function to append a list of elements to its end
a = append(a, [[66,33,44]], 0)
print('Modified array of int values : ')
print(a)
Array of int values :
[[5 4 3]
[4 2 1]]
Modified array of int values :
[[ 5 4 3]
[ 4 2 1]
[66 33 44]]
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement