normalize a matrix along one specific dimension [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a matrix of shape [1000,500], and I would like to normalize the matrix along the second dimension. Is the following implementation right?
def norm(x):
return (x - np.mean(x)) / (np.std(x) + 1e-7)
for row_id in range(datamatrix.shape[0]):
datamatrix[row_id,:] = norm(datamatrix[row_id,:])

Your implementation would indeed normalize along the row-axis (I'm not sure what you mean by second dimension as rows are usually the first dimension of matrices, and numpy starts with dimension 0). You don't need to include the colon as it's implicit that you want all the rows.
Do remember to use the float32 dtype in your datamatrix as opposed to a integer dtype as it doesn't do automatic typecasting.
A more efficient, or clean implementation might be to use sklearn.preprocessing.normalize.
But be aware that you're using standard score normalization which assumes your dataset is normally distributed.

Related

How to do this particular selective masking in numpy [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have two 2d NumPy arrays. I need to mask array 2 on array 1 in such a way that, whatever non zero value present in array2 should get replaced in array1, but others should remain the same.
I need the function selective mask where I can input this two 2d arrays and it returns output.
eg:
array1=np.array([[1,2,3],[0,0,0],[3,3,3]])
array2=n.array([[0,0,0],[0,0,0],[7,8,9]])
result_array=selective_mask(array1,array2)
result array should be [[1,2,3],[0,0,0],[7,8,9]], as you can see only the elements 7,8,9 got switched as that was only the non zero elements in array2.
I came up with a solution.
I used np.where
np.where(array2!=0,array2,array1)

Python, Numpy - Normalize a matrix/array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
This is most likely a dumb question but being a beginner in Python/Numpy I will ask it anyways. I have come across a lot of posts on how to Normalize an array/matrix in numpy. But I am not sure about the WHY. Why/When does an array/matrix need to be normalized in numpy? When is it used?
Normalize can have multiple meanings in difference context. My question belongs to the field of Data Analytics/Data Science. What does Normalization mean in this context? Or more specifically in what situation should I normalize an array?
The second part to this question is - What are the different methods of Normalization and can they be used interchangeably in all situations?
The third and final part - can Normalization be used for Arrays of any dimensions?
Links to any reference material (for beginners) will be appreciated.
Consider trying to cluster objects with two numerical attributes A and B. Both are equally important. Attribute A can range from 0 to 1000 and attribute B can range from 0 to 5.
If you did not normalize A and B you would end up with attribute A completely overpowering attribute B when applying any standard distance metric.

How to use np.transpose() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am trying to understand how to use np.transpose() properly.
I have a np array of shape (4,28,28,8,8). So this is 4 images of shape (224,224) that I viewed as shown by the previous shape.
I would like to revert back to (4,224,224). I feel the best way to go about is to use np.transpose() and reshape() functions. But I am hitting a roadblock as to how to revert back correctly.
Help. Please and thank you.
EDIT: (4,224,224) is 4 (this variable is subject to change as the number of images I load can change it can be 4, it can be 1000) images of shape (224,224). I used listdir() to load images. While loading I resized to the current shape of (224,224). I am going to perform operations on the shape (4,28,28,8,8) which is technically, 4 images, of shape (224,224) broken into (28,28) blocks, each containing (8,8) blocks. This shape I got by using view_as_blocks provided by scikit-image. Once I perform the operations which require that shape, I must revert back to (4,224,224). Where I am stuck.
What you need is probably :
b=np.swapaxes(a,3,2).reshape(4,28*8,28*8)
a beeing your array.

Fitting arbitrary data from simulation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello dear Python experts:)
From a simulation I got data (course of energy over the time) which I have to fit. When I plot the energy it has a non-periodic oscillating course. There are a bunch of helping function like curve_fit from scipy etc. But you always have to specify a function with which the fit should take place. But I don't know a proper function a priori.
I need something like a Fourier fit to get a function representing the data (like it is possible in MatLab) to later use this function to determine its maxima. Has anyone an idea how to deal with such a problem?
Here is an example course: 2
If you like, you can have a look at the data in a .csv-file: https://1drv.ms/u/s!AuQAmr8-QRJSdzNTzyvWPhUaEnw
I would be very delighted to get some help:-)
Many thanks:-)
Using the Fourier fit in MATLAB you also specify a model (how many sin/cos you want).
For instance "Fourier 2" is:
f(x) = a0 + a1*cos(x*w) + b1*sin(x*w) +
a2*cos(2*x*w) + b2*sin(2*x*w)
Check http://exnumerus.blogspot.nl/2010/04/how-to-fit-sine-wave-example-in-python.html to see how to fit for "Fourier1".
If you really want no model you need to use something like "eureqa", which is free for academic use (http://www.nutonian.com/download/eureqa-desktop-download/).

Draw a random value from a given distribution [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array of values which I have generated starting from an observed value y1, and assuming that this value has a poissonian distribution:
array = np.random.poisson(np.real(y1), 10000)
What if I want to extract a random value from array, which is poissonian distributed, and hence has a "most probable value" that peaks at y1? How can I do that? Does it work by simple random extraction, or does it need something else to be specified?
EDIT: trying to be more specific. I have an array whose elements are Poisson distributed. If I want to randomly extract an element from that array, should I tell to the method about the array distribution, or it is not necessary?
I hope this will clarify a bit.
Just
import random
randval = random.choice(array)
should work fine for you. Once array is generated, in order to pick one at random of its items it does not matter any more by what process or according to what distribution array was populated in the first place.

Categories

Resources