SlideShare a Scribd company logo
Computation on NumPy Arrays:
Aggregations
Summing the Values in an Array
In[1]: import numpy as np
In[2]: np.random.random(100)
sum(L)
Out[2]:
55.61209116604941
In[4]: big_array = np.random.rand(1000000)
%timeit sum(big_array)
%timeit np.sum(big_array)
Out[4]:
443 ms ± 3.54 ms per loop (mean ± std. dev. of 7 runs, 1 loop
each)
2.97 ms ± 19.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops
each)
Minimum and Maximum
In[5]: min(big_array), max(big_array)
Out[5]:
(1.1717128136634614e-06, 0.9999976784968716)
In[6]: np.min(big_array), np.max(big_array)
Out[6]: (1.1717128136634614e-06, 0.9999976784968716)
In[7]:
%timeit min(big_array)
%timeit np.min(big_array)
Out[7]:
251 ms ± 4.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
1.68 ms ± 15.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops
each)
In[8]: print(big_array.min(), big_array.max(), big_array.sum())
Out[8]: 1.17171281366e-06 0.999997678497 499911.628197
Multi dimensional aggregates
In[9]: M = np.random.random((3, 4))
print(M)
Out[9]:
[[ 0.8967576 0.03783739 0.75952519 0.06682827]
[ 0.8354065 0.99196818 0.19544769 0.43447084]
[ 0.66859307 0.15038721 0.37911423 0.6687194 ]]
In[10]:
M.sum()
Out[10]: 6.0850555667307118
In[11]: M.min(axis=0) # Print the minimum value for axis = 0
Out[11]: array([ 0.66859307, 0.03783739, 0.19544769, 0.06682827])
In[12]: M.max(axis=1) # Print the maximum value for axis = 1
Out[12]: array([ 0.8967576 , 0.99196818, 0.6687194 ])
Example: What is the Average Height of US
Presidents?
Example: What is the Average Height of US
Presidents?
In[13]: !head -4 president_heights.csv
order,name,height(cm)
1,George Washington,189 2,
John Adams,170 3,
Thomas Jefferson,189
In[14]: import pandas as pd
data = pd.read_csv('president_heights.csv')
heights = np.array(data['height(cm)'])
print(heights)
Out[14]: [189 170 189 163 183 171 185 168 173 183
173 173 175 178 183 193 178 173 174 183 183 168
170 178 182 180 183 178 182 188 175 179 183 193
182 183 177 185 188 188 182 185]
Example: What is the Average Height of US
Presidents?
In[15]:
print("Mean height: ", heights.mean())
print("Standard deviation:", heights.std())
print("Minimum height: ", heights.min())
print("Maximum height: ", heights.max())
Out[15]:
Mean height: 179.738095238
Standard deviation: 6.93184344275
Minimum height: 163
Maximum height: 193
Example: What is the Average Height of US
Presidents?
In[16]:
print("25th percentile: ", np.percentile(heights, 25))
print("Median: ", np.median(heights))
print("75th percentile: ", np.percentile(heights, 75))
Out[16]:
25th percentile: 174.25
Median: 182.0 75th
percentile: 183.0
percentile()function used to compute the
nth percentile of the given data (array elements) along
the specified axis

More Related Content

Similar to NumPy_Aggregations - Python for Data Science.pptx (20)

PPT
array
teach4uin
 
PDF
How I Built a Power Debugger Out of the Standard Library and Things I Found o...
doughellmann
 
PPTX
NumPy_Broadcasting Data Science - Python.pptx
JohnWilliam111370
 
DOCX
Aastha Shah.docx
AasthaShah41
 
PDF
Writing Faster Python 3
Sebastian Witowski
 
DOC
Experiment 06 psiplclannguage expermient.doc
anuharshpawar22
 
PPTX
Python 03-parameters-graphics.pptx
TseChris
 
DOCX
PRACTICAL COMPUTING
Ramachendran Logarajah
 
PPTX
Python programming workshop session 3
Abdul Haseeb
 
DOCX
Practicle 1.docx
GaneshPawar819187
 
PDF
fds Practicle 1to 6 program.pdf
GaneshPawar819187
 
PPTX
Python basics
NexThoughts Technologies
 
PDF
IMPORTAnt sql ques for data analyst interview.pdf
educationalist1
 
PDF
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
PPTX
Introduction to data analyticals123232.pptx
MalluKomar
 
DOCX
C file
simarsimmygrewal
 
PPT
array.ppt
DeveshDewangan5
 
PDF
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 
array
teach4uin
 
How I Built a Power Debugger Out of the Standard Library and Things I Found o...
doughellmann
 
NumPy_Broadcasting Data Science - Python.pptx
JohnWilliam111370
 
Aastha Shah.docx
AasthaShah41
 
Writing Faster Python 3
Sebastian Witowski
 
Experiment 06 psiplclannguage expermient.doc
anuharshpawar22
 
Python 03-parameters-graphics.pptx
TseChris
 
PRACTICAL COMPUTING
Ramachendran Logarajah
 
Python programming workshop session 3
Abdul Haseeb
 
Practicle 1.docx
GaneshPawar819187
 
fds Practicle 1to 6 program.pdf
GaneshPawar819187
 
IMPORTAnt sql ques for data analyst interview.pdf
educationalist1
 
Programming Fundamentals Arrays and Strings
imtiazalijoono
 
Introduction to data analyticals123232.pptx
MalluKomar
 
array.ppt
DeveshDewangan5
 
Effective Numerical Computation in NumPy and SciPy
Kimikazu Kato
 

More from JohnWilliam111370 (9)

PPTX
AI-Agents-and-Environments AIML unit 1.pptx
JohnWilliam111370
 
PPT
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
JohnWilliam111370
 
PPTX
Ethics in AI and Its imoact on Society etc
JohnWilliam111370
 
PPTX
Machine Learning for AIML course UG.pptx
JohnWilliam111370
 
PPTX
Feature Engineering for data science.pptx
JohnWilliam111370
 
PPTX
R Factor.pptx
JohnWilliam111370
 
PPT
13256181.ppt
JohnWilliam111370
 
PPT
Research Techniques.ppt
JohnWilliam111370
 
PPT
Research.ppt
JohnWilliam111370
 
AI-Agents-and-Environments AIML unit 1.pptx
JohnWilliam111370
 
ROBOETHICS-CCS345 ETHICS AND ARTIFICIAL INTELLIGENCE.ppt
JohnWilliam111370
 
Ethics in AI and Its imoact on Society etc
JohnWilliam111370
 
Machine Learning for AIML course UG.pptx
JohnWilliam111370
 
Feature Engineering for data science.pptx
JohnWilliam111370
 
R Factor.pptx
JohnWilliam111370
 
13256181.ppt
JohnWilliam111370
 
Research Techniques.ppt
JohnWilliam111370
 
Research.ppt
JohnWilliam111370
 
Ad

Recently uploaded (20)

PDF
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
PPTX
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
PDF
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
PPT
deep dive data management sharepoint apps.ppt
novaprofk
 
PDF
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
PDF
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
PPTX
recruitment Presentation.pptxhdhshhshshhehh
devraj40467
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PDF
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
PDF
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PPTX
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
PDF
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
PDF
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
PPTX
Dr djdjjdsjsjsjsjsjsjjsjdjdjdjdjjd1.pptx
Nandy31
 
PPTX
The _Operations_on_Functions_Addition subtruction Multiplication and Division...
mdregaspi24
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PPTX
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
Building Production-Ready AI Agents with LangGraph.pdf
Tamanna
 
apidays Helsinki & North 2025 - Running a Successful API Program: Best Practi...
apidays
 
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
deep dive data management sharepoint apps.ppt
novaprofk
 
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
Data Chunking Strategies for RAG in 2025.pdf
Tamanna
 
recruitment Presentation.pptxhdhshhshshhehh
devraj40467
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
ER_Model_Relationship_in_DBMS_Presentation.pptx
dharaadhvaryu1992
 
apidays Helsinki & North 2025 - REST in Peace? Hunting the Dominant Design fo...
apidays
 
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
Dr djdjjdsjsjsjsjsjsjjsjdjdjdjdjjd1.pptx
Nandy31
 
The _Operations_on_Functions_Addition subtruction Multiplication and Division...
mdregaspi24
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
Ad

NumPy_Aggregations - Python for Data Science.pptx

  • 1. Computation on NumPy Arrays: Aggregations
  • 2. Summing the Values in an Array In[1]: import numpy as np In[2]: np.random.random(100) sum(L) Out[2]: 55.61209116604941 In[4]: big_array = np.random.rand(1000000) %timeit sum(big_array) %timeit np.sum(big_array) Out[4]: 443 ms ± 3.54 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 2.97 ms ± 19.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
  • 3. Minimum and Maximum In[5]: min(big_array), max(big_array) Out[5]: (1.1717128136634614e-06, 0.9999976784968716) In[6]: np.min(big_array), np.max(big_array) Out[6]: (1.1717128136634614e-06, 0.9999976784968716) In[7]: %timeit min(big_array) %timeit np.min(big_array) Out[7]: 251 ms ± 4.14 ms per loop (mean ± std. dev. of 7 runs, 1 loop each) 1.68 ms ± 15.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) In[8]: print(big_array.min(), big_array.max(), big_array.sum()) Out[8]: 1.17171281366e-06 0.999997678497 499911.628197
  • 4. Multi dimensional aggregates In[9]: M = np.random.random((3, 4)) print(M) Out[9]: [[ 0.8967576 0.03783739 0.75952519 0.06682827] [ 0.8354065 0.99196818 0.19544769 0.43447084] [ 0.66859307 0.15038721 0.37911423 0.6687194 ]] In[10]: M.sum() Out[10]: 6.0850555667307118 In[11]: M.min(axis=0) # Print the minimum value for axis = 0 Out[11]: array([ 0.66859307, 0.03783739, 0.19544769, 0.06682827]) In[12]: M.max(axis=1) # Print the maximum value for axis = 1 Out[12]: array([ 0.8967576 , 0.99196818, 0.6687194 ])
  • 5. Example: What is the Average Height of US Presidents?
  • 6. Example: What is the Average Height of US Presidents? In[13]: !head -4 president_heights.csv order,name,height(cm) 1,George Washington,189 2, John Adams,170 3, Thomas Jefferson,189 In[14]: import pandas as pd data = pd.read_csv('president_heights.csv') heights = np.array(data['height(cm)']) print(heights) Out[14]: [189 170 189 163 183 171 185 168 173 183 173 173 175 178 183 193 178 173 174 183 183 168 170 178 182 180 183 178 182 188 175 179 183 193 182 183 177 185 188 188 182 185]
  • 7. Example: What is the Average Height of US Presidents? In[15]: print("Mean height: ", heights.mean()) print("Standard deviation:", heights.std()) print("Minimum height: ", heights.min()) print("Maximum height: ", heights.max()) Out[15]: Mean height: 179.738095238 Standard deviation: 6.93184344275 Minimum height: 163 Maximum height: 193
  • 8. Example: What is the Average Height of US Presidents? In[16]: print("25th percentile: ", np.percentile(heights, 25)) print("Median: ", np.median(heights)) print("75th percentile: ", np.percentile(heights, 75)) Out[16]: 25th percentile: 174.25 Median: 182.0 75th percentile: 183.0 percentile()function used to compute the nth percentile of the given data (array elements) along the specified axis