0% found this document useful (0 votes)
31 views

Exploring Numpy!: Function Code

NumPy provides n-dimensional arrays and functions to perform arithmetic and logical operations on array elements. NumPy arrays can be created in various ways, such as using numpy.array() to create a 1D array from a list, numpy.zeros() to create a 2D zero array, and numpy.random.random() to generate random values. Arithmetic operations can be performed directly on whole arrays, for example adding 5 to each element of an array using ARR + 5.

Uploaded by

CaPital
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Exploring Numpy!: Function Code

NumPy provides n-dimensional arrays and functions to perform arithmetic and logical operations on array elements. NumPy arrays can be created in various ways, such as using numpy.array() to create a 1D array from a list, numpy.zeros() to create a 2D zero array, and numpy.random.random() to generate random values. Arithmetic operations can be performed directly on whole arrays, for example adding 5 to each element of an array using ARR + 5.

Uploaded by

CaPital
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Exploring NumPy!

NumPy package provides us with various features and functions which helps us in arithmetic
and logical operations. Let us look at some of them:

1. NumPy Arrays: As discussed before, arrays are homogenous collection of datatypes.


With NumPy, we can create n-dimensional arrays (where n can be any integer) and operate
on them using other mathematical functions.
Here are some ways by which you can create arrays using NumPy package assuming the
NumPy packages is imported already.

Function Code
Creating a Numpy Array numpy.array([1,2,3,4,5])
Creating a 2-Dimensional zero array (4X3 – numpy.zeros((4,3))
4 rows and 3 columns)
Creating an array with 5 random values numpy.random.random(5)
Creating a 2-Dimensional constant value numpy.full((3,4),6)
array (3X4 – 3 rows and 4 columns) having
all 6s
Creating a sequential array from 0 to 30 with numpy.arrange(0,30,5)
gaps of 5

One of the features of array is that we can perform arithmetic functions on the elements of
the array directly by performing them on the whole array.

Let us assume the array is “ARR” and it has been initialized as:
ARR = numpy.array([1,2,3,4,5])

Now, let us look at various operations that could be implemented on this array:

Function Code
Adding 5 to each element ARR + 5
Divide each element by 5 ARR / 5
Squaring each element ARR ** 2
Accessing 2nd element of the array (element
ARR[1]
count starts from 0)
Multiplying 2 arrays
ARR * BRR
{consider BRR = numpy.array([6,7,8,9,0]) }

As you can see, direct arithmetical operations can be implemented on individual array
elements just by manipulating the whole array variable.

93

You might also like