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 axis for the calculation of the mean should be the same as used in the call to this std function. New in version 2.0.0. ... Array API compatible name for the ddof parameter. Only one of them can be provided at the same time. New in version 2.0.0. ... If out is None, return a new array containing the standard deviation, otherwise return a reference to the output array. ... There are several common variants of the array standard deviation calculation. Assuming the input a is a one-dimensional NumPy array and mean is either provided as an argument or computed as a.mean(), NumPy computes the standard deviation of an array as:
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.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.std.html
numpy.std — NumPy v2.6.dev0 Manual
The axis for the calculation of the mean should be the same as used in the call to this std function. Added in version 2.0.0. ... Array API compatible name for the ddof parameter. Only one of them can be provided at the same time. Added in version 2.0.0. ... If out is None, return a new array containing the standard deviation, otherwise return a reference to the output array. ... There are several common variants of the array standard deviation calculation. Assuming the input a is a one-dimensional NumPy array and mean is either provided as an argument or computed as a.mean(), NumPy computes the standard deviation of an array as:
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › 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
August 29, 2020 - In pandas, the std() function is used to find the standard Deviation of the series. The mean can be simply defined as the average of numbers. In pandas, the mean() fu ... Sometimes we need to find the sum of the Upper right, Upper left, Lower ...
🌐
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
The Mean, Variance and Standard Deviation of values of a numpy.ndarray object along with the given axis can be found using the mean(), var() and std() functions.
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 ...
🌐
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...
🌐
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.