Standardization/preprocessing for 4-dimensional array

2024/10/14 15:26:25

I'd like to standardize my data to zero mean and std = 1. The shape of my data is 28783x4x24x7, and it can thought of as 28783 images with 4 channels and dimensions 24x7. The channels need to be standardized. How do I standardize while specifying that the 2nd dimension holds the features?

Answer

I found a way to do it. It's perhaps not the most efficient, but it also allows me to use this approach for cross-validation, where I only want to obtain the mean and std from my training data, but apply the operation to both training and test data. This can be used across any number of dimensions if you only want the mean of one dimension. See example code below:

n_user = 3
n_channel = 2
n_pixels = 3
A = np.zeros(shape=(n_user, n_channel, n_pixels))for i in range(n_user):A[i, 0, :] = np.arange(n_pixels)A[i, 1, :] = np.arange(n_pixels) + n_pixels
print Amu_f    = np.zeros(shape=n_channel)
sigma_f = np.zeros(shape=n_channel)for i in range(n_channel):mu_f[i]    = np.mean(A[:,i,:])sigma_f[i] = np.std(A[:,i,:])print mu_f
print sigma_ffor i in range(n_channel):A[:, i, :] -= mu_f[i]A[:, i, :] /= sigma_f[i]print Aprint np.mean(A[:,0,:])
print np.std(A[:,0,:])
https://en.xdnf.cn/q/117943.html

Related Q&A

My Python number guessing game

I have been trying to make a number guessing game for Python and so far it has altogether gone quite well. But what keeps bugging me is that it resets the number on every guess so that it is different,…

Class that takes another class as argument, copies behavior

Id like to create a class in Python that takes a single argument in the constructor, another Python class. The instance of the Copy class should have all the attributes and methods of the original clas…

Simple python script to get a libreoffice base field and play on vlc

Ive banged my head for hours on this one, and I dont understand the LibreOffice macro api well enough to know how to make this work:1) This script works in python:#!/usr/bin/env python3 import subproce…

Print month using the month and day

I need to print month using the month and day. But I cannot seem to move the numbers after 1 to the next line using Python.# This program shows example of "November" as month and "Sunday…

Maya: Defer a script until after VRay is registered?

Im trying to delay a part of my pipeline tool (which runs during the startup of Maya) to run after VRay has been registered. Im currently delaying the initialization of the tool in a userSetup.py like…

Optimization on Python list comprehension

[getattr(x, contact_field_map[communication_type])for x in curr_role_group.contacts ifgetattr(x, contact_field_map[communication_type])]The above is my list comprehension. The initial function and the …

tensorflow Word2Vec error

I downloaded source code of word2vec in github below. https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec.py I am using tensorflow on pycharm. Im using windows 10. I installed…

Mysterious characters at the end of E-Mail, received with socket in python

I am not sure if this is the right forum to ask, but I give it a try. A device is sending an E-Mail to my code in which I am trying to receive the email via a socket in python, and to decode the E-Mail…

Error when importingPython-vlc

I have installed python-vlc D:\Programing\Python\Python 3.6>python -m pip install python-vlc Requirement already satisfied: python-vlc in d:\programing\python\python 3.6\lib\site-packages\python_vlc…

How to use FEniCS in Jupyter Notebook or Spyder?

I have recently installed FEniCS using Docker with following commandTerminal> curl -s https://get.fenicsproject.org | bashEvery thing works well when I am in fenicsproject session. In this session w…