Using numpy Pkg in Array

Using numpy Pkg in Array

Numpy Package

This package contains various classes, functions, variable which are used to do scientific calculation in Python. The arrays which are created by using numpy package is called as n dimensional Arrays.

In order to work with numpy, we need to import the numpy module, in to python program

Import numpy

Now in order to use any of the numpy object from this package, use format: numpy.Object.

Example:1 Write a program to create an array using numpy module. 

There is another way to import everything from numpy and using this it will import everything because of *.

From numpy import *

Uses of Numpy Packages:

We can use Numpy package to create the array using following function.

  • Using array()
  • Using Linspace()
  • Using logspace()
  • Using arrange()
  • Using Zero(), Ones()

Creating Arrays using LinSpace() Function.

This function is used to create an array, using a starting point, and ending points. The format is

Linspace(start , stop , n)

  • Start: represents starting elements
  • Stop: Represents ending elements

N is integer that represents number of parts of elements should be divided.

Let’s suppose: a = linspace(0 , 10, 5)

Output Array is : a = [0, 2.5, 5,7.5,10]

This range from 0 to 10 is divided to 5 equal parts.

Example2: Write a Python program to create an array using Linspace ()

Creating Arrays using Logspace

Logspace creates evenly spaced points, in this it starts with a value which is 10 to power of start and ends to a value which is 10 to the power of stop.

Syntax: Logspace (start, stop, n)

If n is not specified, then it takes the value of 50.

Let’s understand with this example:

Logspace (1, 4, 5) then the function will starts with (10 power 1 and ends with 10 power 4). These values are then divided in to 5 equal parts.

Example 3: Write a Python program to create array using Logspace.

Creating Arrays using Arange() Function.

This is same as range () function, the below format is used for this:

arange( start , stop, stepsize)

In this array will start from start element and will stop one element before in steps defined step size.

Arange(1 , 10, 3) , this will create array from element 1 to 9 like this : [ 1, 1+3 = 4 , 4+3 = 7] = [1,4,7].

Example 4:  Write a python program to create an array with even number up to 20.


    You are will be the first.


Please login here to comment.