- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
SciPy - weighted() Method
The SciPy weighted() method is not considered as a built-in method because it allows various other functions that the user can perform such as weighted means, weighted sums, and weighted operations.
Furthermore, for better clarification we have survey data which might be more reliable and in time series data, the current observation might be given more weight.
Syntax
Following are the varieties of relatable function in SciPy weighted() Method −
sum() or, sqrt() or, linregress()
Parameters
Below is the explanation of all the above syntaxes −
- sum(): Calculate the sum on given data.
- sqrt(): Find the square root of a number.
- linregress(): It works on linear least-squares regression for the given two sets of measurements.
Return value
It returns the result in form of floay values.
Example 1
Following is the SciPy weighted() Method calculate the average weight of the given data array using specified weights.
import numpy as np
from scipy.stats import hmean
data = np.array([10, 20, 30, 40, 50])
weights = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
avg_weight = np.average(data, weights = weights)
print("Weighted Harmonic Mean:", avg_weight)
Output
The above code produces the following result −
Weighted Harmonic Mean: 31.53846153846154
Example 2
Below the example perform the task of linear regression on the data x and y that considers the specified weights.
from scipy import stats
import numpy as np
x_axis = np.array([11, 12, 13, 14, 15])
y_axis = np.array([26, 37, 58, 79, 100])
weights = np.array([0.5, 0.5, 1, 1, 1])
# Weighted linear regression
slope, intercept, r_value, p_value, std_err = stats.linregress(x_axis, y_axis)
print(f"Slope: {slope}, Intercept: {intercept}")
Output
The above code produces the following result −
Slope: 19.0, Intercept: -187.0
Example 3
This example illustrate the detail description of the data that contain weighted statistics.
import numpy as np
data = np.array([11, 21, 31, 41, 51])
weights = np.array([0.11, 0.12, 0.13, 0.14, 0.15])
# weighted mean
weighted_mean = np.sum(data * weights) / np.sum(weights)
# weighted standard deviation
weighted_std = np.sqrt(np.sum(weights * (data - weighted_mean)**2) / np.sum(weights))
print(f"Weighted Mean: {weighted_mean:.2f}")
print(f"Weighted Standard Deviation: {weighted_std:.2f}")
Output
The above code produces the following result −
Weighted Mean: 3.67 Weighted Standard Deviation: 1.25