Srisindhu
2 min readSep 14, 2021

--

Numpy and Pandas are the most frequently used during data analysis. In this blog, let's see how NumPy is installed, imported, and is used for arrays creation.

Install the numpy package

NumPy is the fundamental package for scientific computing in Python. It’s a python package used for doing math more advanced than basic additions and subtractions, etc…

Numpy package includes functions like cosine, sqrt. Numpy can be used for vectors, matrices, and tensors, random simulation.

To install numpy package, use either

Conda install numpy

Or

Pip install numpy

Before accessing numpy and its functions, we need to import first in the python code as

Import numpy as np

array function

Numpy is used for 1-Dimensional , 2-Dimensional and 3-Dimensional arrays. Below are the examples of creating arrays by converting lists using array() function.

oneDarray = np.array([1,2,3,4])

twoDarray = np.array([[1,2],[3,4]])

threeDarray = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])

arange function

Another way of creating an 1- D array or vector is by using an arange function.

numpy.arange([start, ]stop, [step, ]dtype=None, *, like=None)

It returns evenly spaced values within a given interval.

array = np.arange(1,10)

This is similar to range() in python.This creates an 1-D array or vector as

array([1, 2, 3, 4, 5, 6, 7, 8, 9])

1 is inclusive and 10 is exclusive and as the 3rd perimeter is not mentioned, it is incremented by 1.

vec = np.arange(1,21,5)

[out]: [ 1 6 11 16]

In this example, it increments the number by 5. 1 is inclusive and 21 is exclusive.

linspace function

numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)

Linspace returns equally spaced values within an interval with both starting and ending point inclusive. linspace command must have 3 arguments.

vec= np.linspace(0,5,10)

array([0. , 0.55555556, 1.11111111, 1.66666667, 2.22222222,

2.77777778, 3.33333333, 3.88888889, 4.44444444, 5. ])

zeros function

numpy.zeros(shape, dtype=float, order=’C’, *, like=None)

zeros() return a new array of given shape and type, filled with zeros.

zeroarray = np.zeros([5,2])

Out:

array([[0., 0.],

[0., 0.],

[0., 0.],

[0., 0.],

[0., 0.]])

ones function

numpy.ones(shape, dtype=None, order=’C’, *, like=None)

ones() returns a new array of given shape and type, filled with ones.

onearray = np.ones((3,5))

out:

array([[1., 1., 1., 1., 1.],

[1., 1., 1., 1., 1.],

[1., 1., 1., 1., 1.]])

reshape function

numpy.reshape(a, newshape, order=’C’)

numpy.reshape() gives a new shape to an array without changing its data.

array = np.array([1,2,3,4,5,6,7,8,9,10])

reshapedarray = array.reshape(5,2)

out:

array([[ 1, 2],

[ 3, 4],

[ 5, 6],

[ 7, 8],

[ 9, 10]])

--

--

Srisindhu

Data science and Machine Learning Enthusiast .Like to blog about what I learn and read blogs to gain more knowledge!