Don't make ragged arrays. Just don't. Numpy can't do much with them, and any code you might make for them will always be unreliable and slow because numpy doesn't work that way. It turns them into object dtypes:

Sample
array([[1, 2, 3], [1, 2]], dtype=object)

Which almost no numpy functions work on. In this case those objects are list objects, which makes your code even more confusing as you either have to switch between list and ndarray methods, or stick to list-safe numpy methods. This a recipe for disaster as anyone noodling around with the code later (even yourself if you forget) will be dancing in a minefield.

There's two things you can do with your data to make things work better:

First method is to index and flatten.

i = np.cumsum(np.array([len(x) for x in Sample]))
flat_sample = np.hstack(Sample)

This preserves the index of the end of each sample in i, while keeping the sample as a 1D array

The other method is to pad one dimension with np.nan and use nan-safe functions

m = np.array([len(x) for x in Sample]).max()
nan_sample = np.array([x + [np.nan] * (m - len(x)) for x in Sample])

So to do your calculations, you can use flat_sample and do similar to above:

new_flat_sample = (flat_sample - np.mean(flat_sample)) / np.std(flat_sample) 

and use i to recreate your original array (or list of arrays, which I recommend:, see np.split).

new_list_sample = np.split(new_flat_sample, i[:-1])

[array([-1.06904497,  0.26726124,  1.60356745]),
 array([-1.06904497,  0.26726124])]

Or use nan_sample, but you will need to replace np.mean and np.std with np.nanmean and np.nanstd

new_nan_sample = (nan_sample - np.nanmean(nan_sample)) / np.nanstd(nan_sample)

array([[-1.06904497,  0.26726124,  1.60356745],
       [-1.06904497,  0.26726124,         nan]])
Answer from Daniel F on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.std.html
numpy.std — NumPy v2.5 Manual
The use of \(N-1\) in the denominator is often called “Bessel’s correction” because it corrects for bias (toward lower values) in the variance estimate introduced when the sample mean of a is used in place of the true mean of the population. The resulting estimate of the standard deviation is still biased, but less than it would have been without the correction. For this quantity, use ddof=1. Note that, for complex numbers, std takes the absolute value before squaring, so that the result is always real and nonnegative.
Top answer
1 of 2
5

Don't make ragged arrays. Just don't. Numpy can't do much with them, and any code you might make for them will always be unreliable and slow because numpy doesn't work that way. It turns them into object dtypes:

Sample
array([[1, 2, 3], [1, 2]], dtype=object)

Which almost no numpy functions work on. In this case those objects are list objects, which makes your code even more confusing as you either have to switch between list and ndarray methods, or stick to list-safe numpy methods. This a recipe for disaster as anyone noodling around with the code later (even yourself if you forget) will be dancing in a minefield.

There's two things you can do with your data to make things work better:

First method is to index and flatten.

i = np.cumsum(np.array([len(x) for x in Sample]))
flat_sample = np.hstack(Sample)

This preserves the index of the end of each sample in i, while keeping the sample as a 1D array

The other method is to pad one dimension with np.nan and use nan-safe functions

m = np.array([len(x) for x in Sample]).max()
nan_sample = np.array([x + [np.nan] * (m - len(x)) for x in Sample])

So to do your calculations, you can use flat_sample and do similar to above:

new_flat_sample = (flat_sample - np.mean(flat_sample)) / np.std(flat_sample) 

and use i to recreate your original array (or list of arrays, which I recommend:, see np.split).

new_list_sample = np.split(new_flat_sample, i[:-1])

[array([-1.06904497,  0.26726124,  1.60356745]),
 array([-1.06904497,  0.26726124])]

Or use nan_sample, but you will need to replace np.mean and np.std with np.nanmean and np.nanstd

new_nan_sample = (nan_sample - np.nanmean(nan_sample)) / np.nanstd(nan_sample)

array([[-1.06904497,  0.26726124,  1.60356745],
       [-1.06904497,  0.26726124,         nan]])
2 of 2
3

@MichaelHackman (following the comment remark). That's weird because when I compute the overall std and mean then apply it, I obtain different result (see code below).

import numpy as np

Samples = np.array([[1, 2, 3],
                   [1, 2]])
c = np.hstack(Samples)  # Will gives [1,2,3,1,2]
mean, std = np.mean(c), np.std(c)
newSamples = np.asarray([(np.array(xi)-mean)/std for xi in Samples])
print newSamples
# [array([-1.06904497,  0.26726124,  1.60356745]), array([-1.06904497,  0.26726124])]

edit: Add np.asarray(), put mean,std computation outside loop following Imanol Luengo's excellent comments (Thanks!)

🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-std-in-python
numpy.std() in Python - GeeksforGeeks
April 26, 2025 - numpy.std() is a function provided by the NumPy library that calculates the standard deviation of an array or a set of values. Standard deviation is a measure of the amount of variation or dispersion of a set of values.
🌐
GeeksforGeeks
geeksforgeeks.org › python › compute-the-mean-standard-deviation-and-variance-of-a-given-numpy-array
Compute the mean, standard deviation, and variance of a given NumPy array - GeeksforGeeks
July 15, 2025 - import numpy as np # Original array array = np.arange(10) print(array) r1 = np.average(array) print("\nMean: ", r1) r2 = np.sqrt(np.mean((array - np.mean(array)) ** 2)) print("\nstd: ", r2) r3 = np.mean((array - np.mean(array)) ** 2) print("\nvariance: ", r3) Output: [0 1 2 3 4 5 6 7 8 9] Mean: 4.5 std: 2.8722813232690143 variance: 8.25 · Example: Comparing both inbuilt methods and formulas ·
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.std.html
numpy.std — NumPy v2.6.dev0 Manual
The use of \(N-1\) in the denominator is often called “Bessel’s correction” because it corrects for bias (toward lower values) in the variance estimate introduced when the sample mean of a is used in place of the true mean of the population. The resulting estimate of the standard deviation is still biased, but less than it would have been without the correction. For this quantity, use ddof=1. Note that, for complex numbers, std takes the absolute value before squaring, so that the result is always real and nonnegative.
🌐
DataCamp
datacamp.com › doc › numpy › std
NumPy std()
NumPy's standard deviation function, `numpy.std()`, is used for computing the standard deviation of elements in an array.
🌐
TutorialsPoint
tutorialspoint.com › article › compute-the-mean-standard-deviation-and-variance-of-a-given-numpy-array
Compute the mean, standard deviation, and variance of a given NumPy array
August 7, 2023 - Standard deviation defines the measure of how the data is spread from the mean and tells us how much the data deviates from the mean. The mathematical formula for this method is as follows.
🌐
Pythontic
pythontic.com › numpy › ndarray › mean_std_var
Mean, Variance and Standard Deviation of values NumPy ndarray elements with example | Pythontic.com
If no axis is specified, all the ... section. The numpy.ndarray also provides methods var(), std() methods that calculates the variance and standard deviation along any given axis of a ndarray object....
Find elsewhere
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-stat-exercise-7.php
NumPy: Compute the mean, standard deviation, and variance of a given array along the second axis - w3resource
# Importing the NumPy library import numpy as np # Creating an array 'x' using arange with 6 elements x = np.arange(6) # Displaying the original array 'x' print("\nOriginal array:") print(x) # Calculating the mean of the array 'x' using np.mean() r1 = np.mean(x) # Calculating the average of the array 'x' using np.average() r2 = np.average(x) # Asserting if the results from np.mean() and np.average() are close assert np.allclose(r1, r2) # Displaying the calculated mean of the array 'x' print("\nMean: ", r1) # Calculating the standard deviation of the array 'x' using np.std() r1 = np.std(x) # Ca
🌐
ProjectPro
projectpro.io › recipes › calculate-mean-variance-and-std-of-matrix-or-ndarray
How to Calculate NumPy Variance and Std of a Matrix in Python? -
February 6, 2024 - Check out this recipe to understand how to calculate the variance and standard deviation of a matrix using NumPy. Additionally, we'll create a function named calculate() in a file named mean_var_std.py that leverages NumPy to compute the mean, variance, standard deviation, max, min, and sum of the rows, columns, and elements in a 3x3 matrix.
🌐
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.std.html
numpy.std — NumPy v1.25 Manual
The standard deviation is the square root of the average of the squared deviations from the mean, i.e., std = sqrt(mean(x)), where x = abs(a - a.mean())**2.
🌐
DEV Community
dev.to › shlok2740 › implementation-of-mean-variance-and-standard-deviation-3nob
Implementation of Mean, Variance, and Standard Deviation - DEV Community
February 28, 2025 - import numpy as np # Dataset data = [2, 4, 4, 4, 5, 5, 7, 9] # Calculating mean mean = np.average(data) print("Mean:", mean) # Output: 5.0 # Calculating variance variance = np.var(data) print("Variance:", variance) # Output: 4.0 # Calculating standard deviation std_deviation = np.std(data) print("Standard Deviation:", std_deviation) # Output: 2.0 · This Python implementation demonstrates how easily you can compute mean, variance, and standard deviation using NumPy, making it a valuable tool for data analysis in machine learning and other scientific applications.
🌐
Medium
medium.com › @shlokkumar2303 › implementation-of-mean-variance-and-standard-deviation-ce4d02d30229
Implementation of Mean, Variance, and Standard Deviation | by Shlok Kumar | Medium
February 28, 2025 - import numpy as np # Dataset data = [2, 4, 4, 4, 5, 5, 7, 9] # Calculating mean mean = np.average(data) print("Mean:", mean) # Output: 5.0 # Calculating variance variance = np.var(data) print("Variance:", variance) # Output: 4.0 # Calculating standard deviation std_deviation = np.std(data) print("Standard Deviation:", std_deviation) # Output: 2.0 · This Python implementation demonstrates how easily you can compute mean, variance, and standard deviation using NumPy, making it a valuable tool for data analysis in machine learning and other scientific applications.
🌐
Vultr Docs
docs.vultr.com › python › third party › numpy › std()
Python Numpy std() - Calculate Standard Deviation
December 25, 2024 - A high standard deviation indicates that the data points are spread out over a larger range of values. Numpy's std() function calculates the standard deviation of an array-like data structure. This section covers the basics and dives deeper ...
🌐
Programiz
programiz.com › python-programming › numpy › methods › std
NumPy std()
The std() method returns the standard deviation of the array. import numpy as np # create an array array1 = np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) # find the standard deviation of entire array deviation1 = np.std(array1) # find the standard deviation across axis 0 (slice wise mean) ...
🌐
The Neural Base
theneuralbase.com › home › pytorch & ml › how to compute mean and std in numpy
How to compute mean and std in numpy for PyTorch users
import numpy as np # Weighted standard deviation (not directly supported by np.std) arr = np.array([1, 2, 3, 4, 5], dtype=float) weights = np.array([1, 2, 3, 4, 5], dtype=float) # Correct weighted mean weighted_mean = np.average(arr, weights=weights) # Correct weighted standard deviation calculation weighted_std = np.sqrt(np.average((arr - weighted_mean)**2, weights=weights)) print(f"Weighted mean: {weighted_mean}") print(f"Weighted std: {weighted_std}") # Sample vs population standard deviation sample_std = np.std(arr, ddof=1) # ddof=1 for sample pop_std = np.std(arr, ddof=0) # ddof=0 for pop
🌐
Celantur
celantur.com › blog › numpy-improvement-std-mean
20x Faster Than NumPy: Mean & Std for uint8 Arrays
How to calculate mean and standard deviation 20 times faster than NumPy for uint8 arrays. ... In this article, I want to explore one method that allowed Celantur to decrease developer downtime by achieving 20 times the performance of built-in numpy.std() and numpy.mean() algorithms for uint8 images.
🌐
SciPy
docs.scipy.org › doc › numpy-1.15.1 › reference › generated › numpy.std.html
numpy.std — NumPy v1.15 Manual
numpy.doc.ufuncs · Section “Output arguments” · Notes · The standard deviation is the square root of the average of the squared deviations from the mean, i.e., std = sqrt(mean(abs(x - x.mean())**2)). The average squared deviation is normally calculated as x.sum() / N, where N = len(x).