Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

334
Views
¿Cuál es la diferencia entre np.array(int) y np.array([int])?

¿Cuál es la diferencia entre np.array(100) y np.array([100]) ? Entiendo que el último es una matriz 1D que contiene un solo valor ( 100 ), pero ¿cómo se llama el primero?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Esta es una matriz 0d. Se puede utilizar en muchas de las mismas formas que otras matrices, sujeto, por supuesto, a las compatibilidades de forma y tipo.

 In [545]: x=np.array(3) In [546]: x.shape Out[546]: () # empty tuple In [547]: x.ndim Out[547]: 0 In [548]: x.ravel() Out[548]: array([3]) # as with other arrays, ravel makes a 1d array In [549]: x.reshape(1,1,1) # reshape to 3d Out[549]: array([[[3]]]) In [550]: x.item() # extracting that element Out[550]: 3 In [551]: x[()] # another extracting Out[551]: 3 In [552]: type(_) Out[552]: numpy.int64 In [553]: type(x.item()) Out[553]: int

Hay una sutil diferencia entre item() y [()] . Uno devuelve el objeto python, el otro un "escalar numpy".

Más sobre escalares numpy:

https://numpy.org/doc/stable/reference/arrays.scalars.html#methods

Un caso común en el que encontramos una matriz 0d es cuando un objeto se envuelve en una matriz, como a través de np.save .

 In [556]: d = np.array({'foo':'bar'}) In [557]: d Out[557]: array({'foo': 'bar'}, dtype=object) In [558]: d.shape Out[558]: () In [559]: d.item()['foo'] Out[559]: 'bar'

El valor de una matriz 0d se puede cambiar

 In [562]: x[...] = 4 In [563]: x Out[563]: array(4)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!