SlideShare a Scribd company logo
Data Science - Introduction to Numpy
# In[1]:
pip install numpy
# In[105]:
import numpy ## Import entire Numpy Package
# In[2]:
import numpy as np ## rename as np
# In[4]:
# Creating an array using Numpy
#import numpy
marks = numpy.array([10,20,30])
print(marks)
# In[106]:
import numpy as n
#The arange() method creates an array with evenly spaced elements as per the
intervals.
a= n.arange(10,100,2) #------------- arange(start,stop,step)
print(a)
# In[107]:
b= np.random.randint(4)
print(b)
# In[109]:
b=np.random.random(3) #--------------- size of random number required between 0 to 1
print(b)
# In[19]:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]]) #Create a 2-D array containing two arrays with the
values 1,2,3 and 4,5,6:
print(arr)
# In[110]:
# 2D Array
#Random value less than 5, so that 0,1,2,3,4 with 3 rows and 3 columns
a1= np.random.randint(3,size=(4,3))
print(a1)
# In[17]:
print(np.__version__) # np version
# In[9]:
# All the elements filled with 1
a1=np.ones((2,3))
print(a1)
# In[111]:
# All the elements filled with 0
a1=np.zeros((2,2))
print(a1)
# In[112]:
# All values to be same
c=np.full((2,3),'@')
print(c)
# In[ ]:
##### Arithemetic Operations on Array
# In[114]:
# Adding two Array
t1=np.array([50,60,70])
t2=np.array([50,50,-30])
t3=t1+t2
print("Addition of two term marks",t3)
# In[16]:
# difference two Array
t1=np.array([50,60,70])
t2=np.array([50,90,30])
t3=t1-t2
print("Difference between two term marks",t3)
# In[20]:
# Subtracting the array value with numeric value
total_stock=np.array([10,15,30,40,15])
item_sold=5
balance=total_stock-item_sold
print("Available Balance", balance)
# In[22]:
#multiplication
qty_sold=np.array([10,20,30])
price_per_unit=np.array([3,5,10])
total_cost=qty_sold*price_per_unit
print("Total Cost",total_cost)
# In[25]:
#Division
dist_traveled=np.array([300,100,200])
time_taken=np.array([2,3,5])
speed=dist_traveled/time_taken
print(speed)
# In[27]:
#exponent
arr=np.array([10,20,3])
print("exponent are",arr**2)
# In[ ]:
#### Array Functions
# In[118]:
# dtype Returns the data type of the array
#arr=np.array([[1,3,4],[2,3,4]]) #----------------Two dimension
arr=np.array([1,2,3])
data_type=arr.dtype
print(data_type)
# In[119]:
#ndim -----Dimension to indicate whether the array is 1D,2D..
dimension=arr.ndim
print(dimension)
# In[ ]:
The shape of an array is the number of elements in each dimension.
Shape that returns a tuple with each index having the number of corresponding
elements.
# In[95]:
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)
arr1=np.array([[1,2,1],[3,4,4],[5,6,4]])
print(arr1.shape)
# In[46]:
## Size- Total number of element in the array
size=arr.size
print("total number of element",size)
s=np.array([[1,2,3],[3,4,4]])
size=s.size
print(size)
# In[120]:
## To find the maximum and minimum value of the array
a=np.array([-10,-3,-100])
max_value=np.max(a)
print("Maximum Value in the array",max_value)
min_value=np.min(a)
print("Minimum Value in the array",min_value)
# In[53]:
# To find max value in each row and column
x=np.array([[1,2,3],[-4,3,10]])
row=np.max(x,axis=1)
col=np.max(x,axis=0)
print("Row max= ", row)
print("column max= ",col)
# In[54]:
# To find min value in each row and column
x=np.array([[1,2,3],[-4,3,10]])
row=np.min(x,axis=1)
col=np.min(x,axis=0)
print("Row min= ", row)
print("column min= ",col)
# In[55]:
# To sort in Ascending Order
arr=np.array([10,4,2,40,23])
sort=np.sort(arr)
print("Sorted array",sort)
# In[56]:
# To sort in Descending Order
arr=np.array([10,4,2,40,23])
sort=np.sort(arr)[::-1]
print("Sorted array",sort)
# In[57]:
# Sum of array elements
arr=np.array([1,2,3])
sum_value=np.sum(arr)
print("Sum of array elements= ", sum_value)
# In[58]:
# Creating two arrays
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
# Joining along rows (vertical)
concatenated_array = np.concatenate((arr1, arr2))
print("Concatenated Array:")
print(concatenated_array)
# In[104]:
import numpy as np
# Creating two 2D arrays
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6]])
# Joining along rows (vertical stacking, axis=0)
concatenated_axis0 = np.concatenate((arr1, arr2), axis=0)
# Joining along columns (horizontal stacking, axis=1)
concatenated_axis1 = np.concatenate((arr1, arr2.T), axis=1)
print("Concatenated Along Rows (Axis 0):")
print(concatenated_axis0)
print("Concatenated Along Columns (Axis 1):")
print(concatenated_axis1)
# In[122]:
#Splitting Arrays -
#numpy.array_split() divides an array into multiple sub-arrays.
arr = np.array([1, 2, 3, 4, 5, 6,7])
# Splitting into three parts
split_array = np.array_split(arr, 3)
print(split_array)
# Accessing split arrays
print(split_array[1])
# In[72]:
#searching
#numpy.where:() It returns the indices of elements in an input array where the given
condition is satisfied
# creating the array
arr = np.array([10, 32, 30, 50, 20, 82, 91, 45])
print("arr = {}".format(arr))
# looking for value 30 in arr and storing its index in i
i = np.where(arr == 30)
print("i = {}".format(i[0]))
# In[ ]:
Slicing arrays
Slicing in python means taking elements from one given index to another given index.
We pass slice instead of index like this: [start:end].
We can also define the step, like this: [start:end:step].
If we don't pass start its considered 0
If we don't pass end its considered length of array in that dimension
If we don't pass step its considered 1
# In[81]:
#Slice elements from index 1 to index 5 from the following array
arr = np.array([10, 20, 30, 40, 50, 60, 70])
print(arr[1:5]) #The result includes the start index, but excludes the end index
# In[ ]:
Iterating Arrays
Iterating means going through elements one by one.
As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop
of python.
If we iterate on a 1-D array it will go through each element one by one.
# In[82]:
arr = np.array([1, 2, 3])
for x in arr:
print(x)
# In[84]:
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
for y in x:
print(y)
# In[ ]:
copying an array refers to the process of creating a new array that contains all the
elements of the original array.
This operation can be done using assignment operator (=)
# In[89]:
a = np.array([110, 220, 330, 440, 550])
b = a
print("Actual Array",a)
print("Copied array:",b)
# In[ ]:
identity array of dimension n x n, with its main diagonal set to one, and all other
elements 0.
# In[98]:
a = np.identity(3)
print("nMatrix a : n", a)
# In[ ]:

More Related Content

PDF
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
DineshThallapelly
 
PDF
Numpy - Array.pdf
AnkitaArjunDevkate
 
PPTX
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
PDF
Essential numpy before you start your Machine Learning journey in python.pdf
Smrati Kumar Katiyar
 
PPTX
NUMPY [Autosaved] .pptx
coolmanbalu123
 
PDF
Numpy questions with answers and practice
basicinfohub67
 
PPTX
NUMPY-2.pptx
MahendraVusa
 
PPTX
arraycreation.pptx
sathya930629
 
ACFrOgAabSLW3ZCRLJ0i-To_2fPk_pA9QThyDKNNlA3VK282MnXaLGJa7APKD15-TW9zT_QI98dAH...
DineshThallapelly
 
Numpy - Array.pdf
AnkitaArjunDevkate
 
NumPy-python-27-9-24-we.pptxNumPy-python-27-9-24-we.pptx
tahirnaquash2
 
Essential numpy before you start your Machine Learning journey in python.pdf
Smrati Kumar Katiyar
 
NUMPY [Autosaved] .pptx
coolmanbalu123
 
Numpy questions with answers and practice
basicinfohub67
 
NUMPY-2.pptx
MahendraVusa
 
arraycreation.pptx
sathya930629
 

Similar to Concept of Data science and Numpy concept (20)

PPTX
Data Preprocessing Introduction for Machine Learning
sonali sonavane
 
PPTX
Numpy in python, Array operations using numpy and so on
SherinRappai
 
PPTX
ARRAY OPERATIONS.pptx
DarellMuchoko
 
PDF
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
DOCX
ECE-PYTHON.docx
Chaithanya89350
 
PDF
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
PPTX
object oriented programing in python and pip
LakshmiMarineni
 
PPTX
NUMPY LIBRARY study materials PPT 2.pptx
CHETHANKUMAR274045
 
PDF
Introduction to NumPy
Huy Nguyen
 
PDF
Introduction to NumPy (PyData SV 2013)
PyData
 
PPTX
NumPy.pptx
EN1036VivekSingh
 
PPTX
1.NumPy is a Python library used for wor
DrAtulZende
 
PPT
Arrays
Aman Agarwal
 
DOCX
CS3401- Algorithmto use for data structure.docx
ywar08112
 
PPTX
NumPy.pptx
DrJasmineBeulahG
 
PPTX
numpydocococ34554367827839271966666.pptx
sankarhariharan2007
 
PPT
14078956.ppt
Sivam Chinna
 
DOCX
Write a NumPy program to find the most frequent value in an array. Take two...
Kritika Chauhan
 
PPTX
NumPy_Broadcasting Data Science - Python.pptx
JohnWilliam111370
 
Data Preprocessing Introduction for Machine Learning
sonali sonavane
 
Numpy in python, Array operations using numpy and so on
SherinRappai
 
ARRAY OPERATIONS.pptx
DarellMuchoko
 
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
ECE-PYTHON.docx
Chaithanya89350
 
Unit-5-Part1 Array in Python programming.pdf
582004rohangautam
 
object oriented programing in python and pip
LakshmiMarineni
 
NUMPY LIBRARY study materials PPT 2.pptx
CHETHANKUMAR274045
 
Introduction to NumPy
Huy Nguyen
 
Introduction to NumPy (PyData SV 2013)
PyData
 
NumPy.pptx
EN1036VivekSingh
 
1.NumPy is a Python library used for wor
DrAtulZende
 
Arrays
Aman Agarwal
 
CS3401- Algorithmto use for data structure.docx
ywar08112
 
NumPy.pptx
DrJasmineBeulahG
 
numpydocococ34554367827839271966666.pptx
sankarhariharan2007
 
14078956.ppt
Sivam Chinna
 
Write a NumPy program to find the most frequent value in an array. Take two...
Kritika Chauhan
 
NumPy_Broadcasting Data Science - Python.pptx
JohnWilliam111370
 
Ad

Recently uploaded (20)

PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PPTX
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
PPTX
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
PPTX
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
Software Testing Tools - names and explanation
shruti533256
 
EE3303-EM-I 25.7.25 electrical machines.pptx
Nagen87
 
22PCOAM21 Data Quality Session 3 Data Quality.pptx
Guru Nanak Technical Institutions
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
unit 3a.pptx material management. Chapter of operational management
atisht0104
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Activated Carbon for Water and Wastewater Treatment_ Integration of Adsorptio...
EmilianoRodriguezTll
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
Ad

Concept of Data science and Numpy concept

  • 1. Data Science - Introduction to Numpy # In[1]: pip install numpy # In[105]: import numpy ## Import entire Numpy Package # In[2]: import numpy as np ## rename as np # In[4]: # Creating an array using Numpy #import numpy marks = numpy.array([10,20,30]) print(marks)
  • 2. # In[106]: import numpy as n #The arange() method creates an array with evenly spaced elements as per the intervals. a= n.arange(10,100,2) #------------- arange(start,stop,step) print(a) # In[107]: b= np.random.randint(4) print(b) # In[109]: b=np.random.random(3) #--------------- size of random number required between 0 to 1 print(b) # In[19]:
  • 3. import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) #Create a 2-D array containing two arrays with the values 1,2,3 and 4,5,6: print(arr) # In[110]: # 2D Array #Random value less than 5, so that 0,1,2,3,4 with 3 rows and 3 columns a1= np.random.randint(3,size=(4,3)) print(a1) # In[17]: print(np.__version__) # np version # In[9]: # All the elements filled with 1 a1=np.ones((2,3))
  • 4. print(a1) # In[111]: # All the elements filled with 0 a1=np.zeros((2,2)) print(a1) # In[112]: # All values to be same c=np.full((2,3),'@') print(c) # In[ ]: ##### Arithemetic Operations on Array # In[114]:
  • 5. # Adding two Array t1=np.array([50,60,70]) t2=np.array([50,50,-30]) t3=t1+t2 print("Addition of two term marks",t3) # In[16]: # difference two Array t1=np.array([50,60,70]) t2=np.array([50,90,30]) t3=t1-t2 print("Difference between two term marks",t3) # In[20]: # Subtracting the array value with numeric value total_stock=np.array([10,15,30,40,15]) item_sold=5 balance=total_stock-item_sold print("Available Balance", balance) # In[22]:
  • 7. # In[ ]: #### Array Functions # In[118]: # dtype Returns the data type of the array #arr=np.array([[1,3,4],[2,3,4]]) #----------------Two dimension arr=np.array([1,2,3]) data_type=arr.dtype print(data_type) # In[119]: #ndim -----Dimension to indicate whether the array is 1D,2D.. dimension=arr.ndim print(dimension) # In[ ]: The shape of an array is the number of elements in each dimension.
  • 8. Shape that returns a tuple with each index having the number of corresponding elements. # In[95]: arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(arr.shape) arr1=np.array([[1,2,1],[3,4,4],[5,6,4]]) print(arr1.shape) # In[46]: ## Size- Total number of element in the array size=arr.size print("total number of element",size) s=np.array([[1,2,3],[3,4,4]]) size=s.size print(size)
  • 9. # In[120]: ## To find the maximum and minimum value of the array a=np.array([-10,-3,-100]) max_value=np.max(a) print("Maximum Value in the array",max_value) min_value=np.min(a) print("Minimum Value in the array",min_value) # In[53]: # To find max value in each row and column x=np.array([[1,2,3],[-4,3,10]]) row=np.max(x,axis=1) col=np.max(x,axis=0) print("Row max= ", row) print("column max= ",col) # In[54]: # To find min value in each row and column x=np.array([[1,2,3],[-4,3,10]])
  • 10. row=np.min(x,axis=1) col=np.min(x,axis=0) print("Row min= ", row) print("column min= ",col) # In[55]: # To sort in Ascending Order arr=np.array([10,4,2,40,23]) sort=np.sort(arr) print("Sorted array",sort) # In[56]: # To sort in Descending Order arr=np.array([10,4,2,40,23]) sort=np.sort(arr)[::-1] print("Sorted array",sort) # In[57]: # Sum of array elements
  • 11. arr=np.array([1,2,3]) sum_value=np.sum(arr) print("Sum of array elements= ", sum_value) # In[58]: # Creating two arrays arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) # Joining along rows (vertical) concatenated_array = np.concatenate((arr1, arr2)) print("Concatenated Array:") print(concatenated_array) # In[104]: import numpy as np # Creating two 2D arrays arr1 = np.array([[1, 2], [3, 4]]) arr2 = np.array([[5, 6]]) # Joining along rows (vertical stacking, axis=0)
  • 12. concatenated_axis0 = np.concatenate((arr1, arr2), axis=0) # Joining along columns (horizontal stacking, axis=1) concatenated_axis1 = np.concatenate((arr1, arr2.T), axis=1) print("Concatenated Along Rows (Axis 0):") print(concatenated_axis0) print("Concatenated Along Columns (Axis 1):") print(concatenated_axis1) # In[122]: #Splitting Arrays - #numpy.array_split() divides an array into multiple sub-arrays. arr = np.array([1, 2, 3, 4, 5, 6,7]) # Splitting into three parts split_array = np.array_split(arr, 3) print(split_array) # Accessing split arrays print(split_array[1])
  • 13. # In[72]: #searching #numpy.where:() It returns the indices of elements in an input array where the given condition is satisfied # creating the array arr = np.array([10, 32, 30, 50, 20, 82, 91, 45]) print("arr = {}".format(arr)) # looking for value 30 in arr and storing its index in i i = np.where(arr == 30) print("i = {}".format(i[0])) # In[ ]: Slicing arrays Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this: [start:end]. We can also define the step, like this: [start:end:step]. If we don't pass start its considered 0
  • 14. If we don't pass end its considered length of array in that dimension If we don't pass step its considered 1 # In[81]: #Slice elements from index 1 to index 5 from the following array arr = np.array([10, 20, 30, 40, 50, 60, 70]) print(arr[1:5]) #The result includes the start index, but excludes the end index # In[ ]: Iterating Arrays Iterating means going through elements one by one. As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python. If we iterate on a 1-D array it will go through each element one by one. # In[82]:
  • 15. arr = np.array([1, 2, 3]) for x in arr: print(x) # In[84]: arr = np.array([[1, 2, 3], [4, 5, 6]]) for x in arr: for y in x: print(y) # In[ ]: copying an array refers to the process of creating a new array that contains all the elements of the original array. This operation can be done using assignment operator (=) # In[89]:
  • 16. a = np.array([110, 220, 330, 440, 550]) b = a print("Actual Array",a) print("Copied array:",b) # In[ ]: identity array of dimension n x n, with its main diagonal set to one, and all other elements 0. # In[98]: a = np.identity(3) print("nMatrix a : n", a) # In[ ]: