473,320 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Re: numpy.frombuffer != unpack() ??

Marlin Rowley wrote:
All:

Say I have an array:

a = (['rrrrggggbbbbaaaa'],['rrrrggggbbbbaaaa'])

How do I make it so that I now have:

starting with first element (a[0])
new_arr[0] = 'r'
new_arr[1] = 'g'
new_arr[2] = 'b'
new_arr[3] = 'a'
new_arr[4] = 'r'
.....

continuing "through" a[1] with the same new_arr
new_arr[N] = 'r'
new_arr[N+1] = 'g'
....

-M
Numpy can do this for you. First, do you really mean the array to
contain lists of one string each? If so:
>>import numpy
a = (['rrrrggggbbbbaaaa'],['rrrrggggbbbbaaaa'])
b = numpy.frombuffer(''.join(sum(a,[])),dtype='S1') # Kind of a
kludge here
>>b
array(['r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'a',
'a', 'a', 'a', 'r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b',
'b', 'b', 'a', 'a', 'a', 'a'],
dtype='|S1')
>>b.shape=(2,4,4)
b
array([[['r', 'r', 'r', 'r'],
['g', 'g', 'g', 'g'],
['b', 'b', 'b', 'b'],
['a', 'a', 'a', 'a']],

[['r', 'r', 'r', 'r'],
['g', 'g', 'g', 'g'],
['b', 'b', 'b', 'b'],
['a', 'a', 'a', 'a']]],
dtype='|S1')
>>c = b.transpose((2,0,1))
c
array([[['r', 'g', 'b', 'a'],
['r', 'g', 'b', 'a']],

[['r', 'g', 'b', 'a'],
['r', 'g', 'b', 'a']],

[['r', 'g', 'b', 'a'],
['r', 'g', 'b', 'a']],

[['r', 'g', 'b', 'a'],
['r', 'g', 'b', 'a']]],
dtype='|S1')
>>d=c.copy() # To make it contiguous
d.shape = (32,)
d
array(['r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r',
'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g',
'b', 'a', 'r', 'g', 'b', 'a'],
dtype='|S1')

Done. Cool no?

Gary Herron
>

------------------------------------------------------------------------
From: ma***********@hotmail.com
To: ro*********@gmail.com; py*********@python.org
Subject: RE: numpy.frombuffer != unpack() ??
Date: Fri, 16 May 2008 17:31:30 -0500

Thank you! That solved it!

-M
------------------------------------------------------------------------
To: py*********@python.org
From: ro*********@gmail.com
Subject: Re: numpy.frombuffer != unpack() ??
Date: Fri, 16 May 2008 17:25:00 -0500
>
Marlin Rowley wrote:
All:

I'm getting different floating point values when I use numpy
vs. unpack().

frgba = numpy.frombuffer(<string of bytes>, dtype=float32)
buffer = unpack("!f", byte)

frgba[0] != buffer[0]

why? This is forcing me use the unpack() function since it's
giving me
the correct values. What am I doing wrong?
>
Endianness, perhaps? '!' specifies big-endian data (an alias for
'>'). Most
likely, you are on a little-endian platform. All of the dtypes
in numpy default
to the native-endianness unless specified. If you want to read
big-endian data
using numpy, do this:
>
frgba = numpy.frombuffer(<string of bytes>, dtype='>f')
>
If you have any more problems with numpy, please join us on the
numpy mailing
list. When reporting problems, please try to provide a small but
complete
snippet of self-contained code, the output that you got, and
explain the output
that you expected to get. Thank you.
>
http://www.scipy.org/Mailing_Lists
>
--
Robert Kern
>
"I have come to believe that the whole world is an enigma, a
harmless enigma
that is made terrible by our own mad attempt to interpret it as
though it had
an underlying truth."
-- Umberto Eco
>
--
http://mail.python.org/mailman/listinfo/python-list


------------------------------------------------------------------------
E-mail for the greater good. Join the i’m Initiative from
Microsoft.
<http://im.live.com/Messenger/IM/Join/Default.aspx?source=EML_WL_%20GreaterGood>

------------------------------------------------------------------------
E-mail for the greater good. Join the i’m Initiative from Microsoft.
<http://im.live.com/Messenger/IM/Join/Default.aspx?source=EML_WL_%20GreaterGood>

------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '08 #1
0 1682

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Gary Wessle | last post by:
Hi I am trying to install NumPy in my debian/testing linux 2.6.15-1-686. with no numpy for debian/testing, I am left alone, since the experimental version available by debian will result in a...
3
by: Iljya | last post by:
Hello, I need to pickle the type numpy.float32 but encounter an error when I try to do so. I am able to pickle the array itself, it is specifically the type that I cannot pickle. I am using:...
2
by: Chris Smith | last post by:
Howdy, I'm a college student and for one of we are writing programs to numerically compute the parameters of antenna arrays. I decided to use Python to code up my programs. Up to now I haven't...
5
by: robert | last post by:
Turning algs for old NumPy modules into numpy code I suffer from this: Upon further processing of returns of numpy calculations, lots of data in an apps object tree will become elementary numpy...
1
by: Gerdus van Zyl | last post by:
I have the following, that is used to convert pixel data and thus should be as fast as possible: b = numpy.ndarray (shape=(w,h,4), dtype=numpy.uint8) a = numpy.frombuffer(buf, numpy.uint8)...
0
by: Robert Kern | last post by:
Marlin Rowley wrote: Endianness, perhaps? '!' specifies big-endian data (an alias for '>'). Most likely, you are on a little-endian platform. All of the dtypes in numpy default to the...
3
by: Sean Davis | last post by:
I have a set of numpy arrays which I would like to save to a gzip file. Here is an example without gzip: b=numpy.ones(1000000,dtype=numpy.uint8) a=numpy.zeros(1000000,dtype=numpy.uint8) fd =...
6
by: John [H2O] | last post by:
I have a script: from numpy import float OutD= v= OutD.append(]) On linux: Python 2.5.1 (r251:54863, Mar 7 2008, 04:10:12)
2
by: Travis Oliphant | last post by:
I wanted to point anybody interested to a blog post that describes a useful pattern for having a NumPy array that points to the memory created by a different memory manager than the standard one...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.