In Numpy we can calculate the Median using two main functions:
numpy.median(): This function returns the median of a given array.
numpy.nanmedian(): This function is similar to numpy.median() but is designed to handle arrays with missing values.
Example: Using "numpy.median()"
As we have already mentioned above, "numpy.median()" is used to calculate the median of an array.
import numpy as np
# Creating an array
data = np.array([10, 2, 5, 8, 7, 3, 12])
# Calculating the median
median_value = np.median(data)
print("Median:", median_value)
In above example, we created an array called data, and then we calculated its median using np.median() function
Output:
Median: 7.0
Example: Using "numpy.nanmedian()"
If our dataset contains missing values (NaN), we can use the numpy.nanmedian() function to calculate median.
import numpy as np
# Creating an array with NaN values
data_with_nan = np.array([10, 2, 5, np.nan, 7, 3, 12])
# Calculate the median, ignoring NaN values
median_nan = np.nanmedian(data_with_nan)
print("Median:", median_nan)
In above example, NumPy automatically ignores the NaN values and computes the median for the remaining values.
Output:
Median: 6.0
Trainings :
Data Science Training in Indore | Python Training in Indore | Data Analytics Training in Indore | Blockchain Training in Indore | React JS Training in Indore | Web Development Training in Indore | Full Stack Development Training in Indore |Free Courses and Resource :
Dart | OpenCV Tutorials | Projects | Interview Questions | Python Data Structures and Algorithms | Aptitude Tests | Verbal Aptitude | Matplotlib Tutorials | Examples | Interview Questions | HackerRank Python | Pandas Tutorials | Projects | Interview Questions | Rust Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | MongoDB Tutorials | Examples | Interview Questions | HackerRank C Program Solutions | Python Tutorials by CodersDaily | React.js Tutorials | Golang Tutorials | Projects | Interview Questions | MS Sql Server Tutorials | Examples | Interview Questions | Verbal Ability Tutorial | Numpy Tutorials | Projects | Interview Questions | Power BI Tutorials | Projects | Interview Questions | Example Dashboards | Tensor Flow | HackerRank C++ Solutions | Django REST Framework Tutorial | CodeChef Python Solutions | Leetcode Python Solutions | Reasoning Ability Tutorial | Quantitative Ability Tutorial | C++ Tutorials | HackerRank Java Solutions | TCS NQT Mock Test Series | Verbal Aptitude 2 | HackerRank DSA Solutions | HackerRank SQL Solutions | Javascript | Node.js Tutorials |Interview Questions :
Pandas Tutorials | Projects | Interview Questions | ExpressJS Tutorials | Projects | Interview Questions | Django | Python Tutorials by CodersDaily | Golang Tutorials | Projects | Interview Questions | Numpy Tutorials | Projects | Interview Questions | Django REST Framework Tutorial |Top Colleges in India :
Indian Institute of Technology Bombay | Jaypee University of Engineering and Technology - Guna |