473,386 Members | 1,804 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,386 software developers and data experts.

Why does numpy.array(a[0],b[0]) have this meaning?

Hello All,

Case 1
This generates an error, which makes sense because the argument should
be a list of numbers:
numpy.array(10,10)

Case 2
This does not generate an error and the result is an array with a
single element:
a = numpy.array([10])
b = numpy.array([10])
numpy.array(a[0],b[0])

The only different I see here between the numpy.array call in the
cases is that
a[0] is a numpy int32
10 is an int

Why would this minor difference in integer types cause a totally
different result for the two cases - or is something else causing the
difference in results?
-rick

P.S.
I am aware that numpy.array([10,10]) will work, but I'm trying to
understand what is going on syntactically/semantically in the two
cases above.
Nov 3 '08 #1
2 1675
On Sun, 02 Nov 2008 23:26:24 -0800, Rick Giuly wrote:
Case 1
This generates an error, which makes sense because the argument should
be a list of numbers:
numpy.array(10,10)

Case 2
This does not generate an error and the result is an array with a single
element:
a = numpy.array([10])
b = numpy.array([10])
numpy.array(a[0],b[0])

The only different I see here between the numpy.array call in the cases
is that
a[0] is a numpy int32
10 is an int

Why would this minor difference in integer types cause a totally
different result for the two cases - or is something else causing the
difference in results?
From the `numpy.array` docstring:

Inputs:
object - an array, any object exposing the array interface, any
object whose __array__ method returns an array, or any
(nested) sequence.

And `numpy.int32` instances have an `__array__()` method:

In [225]: ten = numpy.int32(10)

In [226]: ten.__array__()
Out[226]: array(10)

Ciao,
Marc 'BlackJack' Rintsch
Nov 3 '08 #2
Rick Giuly wrote:
Hello All,

Case 1
This generates an error, which makes sense because the argument should
be a list of numbers:
numpy.array(10,10)

Case 2
This does not generate an error and the result is an array with a
single element:
a = numpy.array([10])
b = numpy.array([10])
numpy.array(a[0],b[0])

The only different I see here between the numpy.array call in the
cases is that
a[0] is a numpy int32
10 is an int

Why would this minor difference in integer types cause a totally
different result for the two cases - or is something else causing the
difference in results?
The second argument is for a dtype. Basically, we'll accept anything there that
can be coerced to a dtype using numpy.dtype(). For some reason, we have an
undocumented feature where dtype(some_array_or_numpy_scalar) will return the
dtype of that value. Plain Python ints and floats don't have a dtype attached to
them, so we raise an exception.

If you have more numpy questions, please join us on the numpy-discussion mailing
list.

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

Nov 3 '08 #3

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

Similar topics

2
by: mcdurr | last post by:
I recently installed Python 2.5 on Windows and also installed numpy 1.0. I'd like to compute an FFT on an array of numbers but I can't seem to access the FFT function. I'm fairly new to Python...
1
by: Jianzhong Liu | last post by:
Hello, Guys, I have a question about the linear_least_squares in Numpy. My linear_least_squares cannot give me the results. I use Numpy1.0. The newest version. So I checked online and get...
3
by: Duncan Smith | last post by:
Hello, Since moving to numpy I've had a few problems with my existing code. It basically revolves around the numpy scalar types. e.g. ------------------------------------------------ array(,...
4
by: jimgardener | last post by:
hi, (i posted this to numpy discussion grp couple of days back ..but it fails to appear..)since it is needed for my work i would appreciate if anyone can help me with this question i have two...
0
by: Gary Herron | last post by:
Marlin Rowley wrote: Numpy can do this for you. First, do you really mean the array to contain lists of one string each? If so: kludge here array(, dtype='|S1') array(, ,
5
by: Marc Oldenhof | last post by:
Hello all, I'm pretty new to Python, but use it a lot lately. I'm getting a crazy error trying to do operations on a string list after importing numpy. Minimal example: Python 2.5.1...
2
by: sapsi | last post by:
Hello, I have a numpy array (2 rows 3 colums) import numpy a=numpy.array( , ]) I wish to add a row, this is how i do it s=a.shape numpy.resize(a,s+1,s)
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...
3
by: Rüdiger Werner | last post by:
Hello! Out of curiosity and to learn a little bit about the numpy package i've tryed to implement a vectorised version of the 'Sieve of Zakiya'. While the code itself works fine it is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.