PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

[FIXED] what does myarray[0][:,0] mean

 November 04, 2022     lambda, numpy, numpy-ndarray     No comments   

Issue

This is an excerpt from a documentation.

lambda ind, r: 1.0 + any(np.array(points_2d)[ind][:,0] == 0.0)

But I don't understand np.array(points_2d)[ind][:,0].

It seems equivalent to myarray[0][:,0], which doesn't make sense to me.

Can anyone help to explain?


Solution

With points_2d from earlier in the doc:

In [38]: points_2d = [(0., 0.), (0., 1.), (1., 1.), (1., 0.),
    ...:           (0.5, 0.25), (0.5, 0.75), (0.25, 0.5), (0.75, 0.5)]

In [39]: np.array(points_2d)
Out[39]: 
array([[0.  , 0.  ],
       [0.  , 1.  ],
       [1.  , 1.  ],
       [1.  , 0.  ],
       [0.5 , 0.25],
       [0.5 , 0.75],
       [0.25, 0.5 ],
       [0.75, 0.5 ]])

Indexing with a scalar gives a 1d array, which can't be further indexed with [:,0].

In [40]: np.array(points_2d)[0]
Out[40]: array([0., 0.])

But with a list or slice:

In [41]: np.array(points_2d)[[0,1,2]]
Out[41]: 
array([[0., 0.],
       [0., 1.],
       [1., 1.]])

In [42]: np.array(points_2d)[[0,1,2]][:,0]
Out[42]: array([0., 0., 1.])

So this selects the first column of a subset of rows.

In [43]: np.array(points_2d)[[0,1,2]][:,0]==0.0
Out[43]: array([ True,  True, False])

In [44]: any(np.array(points_2d)[[0,1,2]][:,0]==0.0)
Out[44]: True

I think they could have used:

In [45]: np.array(points_2d)[[0,1,2],0]
Out[45]: array([0., 0., 1.])


Answered By - hpaulj
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing