Python Forum
2 Dimensional NumPy for beginners
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2 Dimensional NumPy for beginners
#2
if you have an array like arr = np.array([1,2,3,4,5]) and you would say something like arr[1:3] you would select everything starting at index 1 to index 3 (but excluding index 3). so you would get [2,3]. so the : always tells the array from where to where you want to select stuff. additionally you could say something like arr[0:4:2] now you pick everything from index 0 to index 4 (excluding index 4) and only every two values (so you set a step width there). what you would get is: [1,3].
In your example you have a 2 Dimensional array, so saying something like ridership[1,3] would select the item at row 1 and column 3. so you divide your x,y,z,... indices by using "," comma. so in your case you would pick following rows [1:3] (so each row from index 1 to 3, excluding index 3) and columns [3:5] (so each column from 3 to 5, excluding index 5) since you selece ridership[1:3, 3:5]. ridership[1,3] == 2328, ridership[1,4] == 2539, ridership[2,3] == 6461 and ridership[2,4] == 2691. What you are doing is slicing your array and it is extreamly efficient in comparison to the usage of loops :)
Reply


Messages In This Thread
2 Dimensional NumPy for beginners - by Jack_Sparrow - May-08-2018, 05:01 PM
RE: 2 Dimensional NumPy for beginners - by ThiefOfTime - May-08-2018, 05:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Numpy] How to store different data type in one numpy array? water 7 716 Mar-26-2024, 02:18 PM
Last Post: snippsat
  Numpy returns "TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'int'" kalle 2 2,675 Jul-19-2022, 06:31 AM
Last Post: paul18fr
  PyTorch for beginners OmegaRed94 1 1,119 Jun-09-2022, 09:20 PM
Last Post: Larz60+
  "erlarge" a numpy-matrix to numpy-array PhysChem 2 3,030 Apr-09-2019, 04:54 PM
Last Post: PhysChem
  How to add an element such as an average to a multi-dimensional array? xhughesey 6 4,054 Jan-06-2019, 10:47 PM
Last Post: xhughesey
  Three-dimensional Contour Plots minifizikus 1 3,298 Sep-13-2018, 10:56 PM
Last Post: Larz60+
  Recurse through n-dimensional array ColdDeath 2 3,000 Apr-05-2018, 11:20 AM
Last Post: KenniT
  Two Dimensional Chart from CSV File srini1995 0 2,253 Nov-27-2017, 07:10 AM
Last Post: srini1995
  How to sum elements of same position in n dimensional array Felipe 2 4,161 May-11-2017, 10:33 AM
Last Post: Felipe

Forum Jump:

User Panel Messages

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