Skip to content

Latest commit

History

History
128 lines (108 loc) · 2.64 KB

File metadata and controls

128 lines (108 loc) · 2.64 KB

Numpy

Numpy

Introduction

  • Numpy is an abbreviated form of Numerical Python or Numeric Python.
  • It is the most basic yet powerful package for scientific computing and data manipulation in Python.
  • It supports large, multidimensional arrays and matrices.
  • It is generally used for Data Analysis and is a part of scientific python.
  • It is an extension module for Python, mostly written in C.

Official Website:https://numpy.org/

Why Numpy?

  • Numpy support N-dimensional array.
  • It consumes less memory.
  • NumPy array is faster than Python List
  • We can create an n-dimensional array in python using numpy.array().
  • Numpy array provide multidimensional slicing on an array
  • Easy for matrix computation
  • In Python Array is not present where list store different types of elements but numpy helps to behave like a normal array to store similar data types.
  • the list need looping to perform scalar operations

Normal List

lst=[1,2,3,4]
#Add 5 to every element
res=[elem+5 for elem in lst]
print(res)
#Result:

Output:

[6, 7, 8, 9]

Using Numpy Array

importnumpyasnplst=np.array([1,2,3,4])
res=lst+5print(res)

Output:

[6 7 8 9]
  • Consumes Less Memory as Compared to Python List Python List Memory Consumes
importsyslst=list(range(0,100))
#Add 5 to every elementres=sys.getsizeof(lst)
print(res)

Output:

1008

Numpy Array Memory Consumes

importsysimportnumpyasnplst=np.array(range(0,100))
res=sys.getsizeof(res)
print(res)

Output:

28

Install Numpy Array

  • Open command prompt in windows machine and type following command
pipinstallnumpy
  • After Completion of Installation open Jupyter Notebook or python shell to test whether numpy is properly install or not.
  • Type
importnumpy

and hit Enter if it does not give any error then your numpy is installed properly(Do not try on Normal command Prompt use python shell or any idle).

import Numpy in Your Program

write down following line for importing Numpy

importnumpy

or Rename Numpy in Import as follows

importnumpyasnp

Create Simple Numpy Array

  • To Create a simple numpy array use array() function.

Syntax:

importnumpyasnpNumpy_array=np.array(<Elementsiterative>)

Example:

importnumpyasnpnp_array=np.array([1,2,3,4,5])
#print Type of Arrayprint(type(np_array))
#print numpy arrayprint(np_array)

Output:

<class 'numpy.ndarray'>
[1 2 3 4 5]

numpy