Python Forum
Trying to understand Numpy/array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to understand Numpy/array
#1
Hi guys, it's on my first exercise list with Numpy.
That problem:
Consider the 3 arrays below
Return the value of the xarr array if the value is True in the cond array. Otherwise, return the value of the yarr array.

import numpy as np

xarr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
yarr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])
cond = np.array([True, False, True, True, False])

#my answer to the problem

result1 = np.arange(cond.size)
a=0
for i in cond:
    if i == True:
        #print(xarr[a])
        result1[a] = xarr[a]
        a=a+1
    else:
        #print(yarr[a])
        result1[a] = yarr[a]
        a=a+1
print(result1)
In response I get this:
[1 2 1 1 2]
But I would like to get this:
 [1.1 2.2 1.3 1.4 2.5]
I'm starting study with Data Science, this is the first list of exercises I've done with NumPy. I was confused because when I use the "prints" that are inside the "if/else", they return the value I want, but when I assign them to the new "array" they don't go as I imagine. What logic am I wrong or missing? from now on, grateful
Reply
#2
Numpy array is not like list. All elements in the array are of same type and the type is fixed, unless you cast it to different dtype.
You can specify the dtype when you create the array with arange, but you do not do so and numpy infer the dtype from other arguments and in this case the dtype of result1 is int32. You can do result1 = np.arange(cond.size, dtype=float) and to get dtype float64. You can use other types
nabuchadresar likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Very thanks

a lesson learned!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,927 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  IPython errors for numpy array min/max methods muelaner 1 592 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Expand the range of a NumPy array? PythonNPC 0 767 Jan-31-2023, 02:41 AM
Last Post: PythonNPC
  Change a numpy array to a dataframe Led_Zeppelin 3 1,149 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  from numpy array to csv - rounding SchroedingersLion 6 2,251 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  numpy.array has no attribute head Led_Zeppelin 1 1,274 Jul-13-2022, 12:56 AM
Last Post: Led_Zeppelin
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,166 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  go over and search in numpy array faster caro 7 1,828 Jun-20-2022, 04:54 PM
Last Post: deanhystad
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,148 Apr-19-2022, 07:36 AM
Last Post: JulianZ
  Need a little help with numpy array magic. pmf71 0 1,163 Dec-01-2021, 02:51 AM
Last Post: pmf71

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020