Python String representation of numpy array - python

Iam trying to get a FULL string representation of an 2D float32 512x512 array. I can either use numpys string2array(array) or repr(array). But the problem is that I always get a shortened output like that:
'...[2.0886018e-04 1.7029114e-04 2.8904244e-05 ... 4.1985390e-06
1.4982919e-06 4.7537060e-06]]'
Is there a possibility of full representation?

The answer is that numpy array2string has a threshold option which can be set to np.inf. Then a full representation is possible! :)

Related

numpy: Print array enforcing scientific notation

I need to print a numpy array with scientific notation regardless of the value of the components of the array.
To my knowledge, numpy.format_float_scientific acts only on scalars and returns error if applied to vectors - therefore, I need an alternative.
Thank you,
Davide
I would do smth like this:
np.set_printoptions(formatter={'all':np.format_float_scientific})
print(np.arange(5))
output:
[0.e+00 1.e+00 2.e+00 3.e+00 4.e+00]

matlab string array to python numpy

I have an array of strings in Matlab and I want to use it with python. I tried to do the same process as for numeric arrays i.e.
np_array = scipy.io.loadmat(mat_array)
which works with numeric data.
But when I try to use a string array like
mat_str_array = [“This”, “is”, “a”, “string”]
I get an error/warning stating
cannot be transformed using the same method
Any suggestions?
Thanks in advance.

Converting a string to a wav file in python

I'm new to Python, and to programming in general, so please don't take it too hard on me
I am currently trying to figure out how to write a new wav file using a string (which was derived from another wave file's data)
I performed a fourier transform on that file's data, so now I'm trying to get the values from the Fourier transform written into a new wav file.
I can only use numpy and the included Python library, not scipy
According to the documentation, I have to use wave_write(), but I have no idea what the code is supposed to look like for this function.
I think I'm supposed to do something pertaining to
wave_write.writeframesraw(data)
Then again, not totally sure of what to do.
Any help is greatly appreciated!
Two functions in NumPy can help you with this: astype and tostring.
If you have an array of sound samples, say X then you can convert it to the right format using astype. This will depend on what data type is used in the wav file, and the library you are using to save it. But let us for this example say you want to store it as 16 bit integer. You'll need to scale X according to the data type selected - so in this case the range will be -32768 to 32767 for a signed 16 bit int. If you sample goes from -1.0 to 1.0 then you can simply multiply with 32767.
The next part is simply to convert it to a string using tostring, it could look something the following:
scaled = X * 32767
scaled.astype('<i2').tostring()
You can find the documentation for the functions here:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.astype.html
https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tostring.html

Numpy shape incorrectly giving L in tuple? [duplicate]

If I construct a numpy matrix like this:
A = array([[1,2,3],[4,5,6]])
and then type A.shape I get the result:
(2L, 3L)
Why am I getting a shape with the format long?
I can restart everything and I still have the same problem. And as far as I can see, it is only when I construct arrays I have this problem, otherwise I get short (regular) integers.
As #CédricJulien puts it on the comment, there is no problem with long numbers in this case - this should be treated as an implementation detail.
The real answer for your question can, of course, only be found inside numpy's source code, but the fact that the dimensions are long in this case should not matter for any use you have for the arrays or these indexes.

Long integer shape of Numpy arrays

If I construct a numpy matrix like this:
A = array([[1,2,3],[4,5,6]])
and then type A.shape I get the result:
(2L, 3L)
Why am I getting a shape with the format long?
I can restart everything and I still have the same problem. And as far as I can see, it is only when I construct arrays I have this problem, otherwise I get short (regular) integers.
As #CédricJulien puts it on the comment, there is no problem with long numbers in this case - this should be treated as an implementation detail.
The real answer for your question can, of course, only be found inside numpy's source code, but the fact that the dimensions are long in this case should not matter for any use you have for the arrays or these indexes.

Categories

Resources