What is the difference between single and double bracket Numpy array?

2024/10/3 12:27:44
import numpy as np
a=np.random.randn(1, 2)
b=np.zeros((1,2))
print("Data type of A: ",type(a))
print("Data type of A: ",type(b))

Output:

Data type of A:  <class 'numpy.ndarray'>
Data type of A:  <class 'numpy.ndarray'>

In np.zeros(), to declare an array we give the input in 2 brackets whereas in np.random.radn(), we give it in 1 bracket?

Is there any specific reason for the syntax,as both of them are of same data type but follow a different syntax?

Answer

In an effort to ease the transition for Matlab users to NumPy, some convenience functions like randn were built which use the same call signature as their Matlab equivalents.

The more NumPy-centric (as opposed to Matlab-centric) NumPy functions (such as np.zeros) expect the size (or shape) to be a tuple. This allows other parameters like dtype and order to be passed to the function as well. The Matlab-centric functions assume all the arguments are part of the size.

np.random.randn is one of NumPy's Matlab-centric convenience functions, modeled after Matlab's randn. The more NumPy-centric alternative to np.random.randn is np.random.standard_normal.

https://en.xdnf.cn/q/70726.html

Related Q&A

How to make tkinter button widget take up full width of grid

Ive tried this but it didnt help. Im making a calculator program. Ive made this so far: from tkinter import * window = Tk()disp = Entry(window, state=readonly, readonlybackground="white") dis…

Python strip() unicode string?

How can you use string methods like strip() on a unicode string? and cant you access characters of a unicode string like with oridnary strings? (ex: mystring[0:4] )

Python equivalent for MATLABs normplot?

Is there a python equivalent function similar to normplot from MATLAB? Perhaps in matplotlib?MATLAB syntax:x = normrnd(10,1,25,1); normplot(x)Gives:I have tried using matplotlib & numpy module to…

python mask netcdf data using shapefile

I am using the following packages:import pandas as pd import numpy as np import xarray as xr import geopandas as gpdI have the following objects storing data:print(precip_da)Out[]:<xarray.DataArray …

Whats a good general way to look SQLAlchemy transactions, complete with authenticated user, etc?

Im using SQLAlchemys declarative extension. Id like all changes to tables logs, including changes in many-to-many relationships (mapping tables). Each table should have a separate "log" table…

OpenCV - Tilted camera and triangulation landmark for stereo vision

I am using a stereo system and so I am trying to get world coordinates of some points by triangulation.My cameras present an angle, the Z axis direction (direction of the depth) is not normal to my sur…

Node.jss python child script outputting on finish, not real time

I am new to node.js and socket.io and I am trying to write a small server that will update a webpage based on python output. Eventually this will be used for a temperature sensor so for now I have a du…

lambda function returning the key value for use in defaultdict

The function collections.defaultdict returns a default value that can be defined by a lambda function of my own making if the key is absent from my dictionary.Now, I wish my defaultdict to return the u…

Calling Matlab function from python

I have one project in which I have one one matlab code which I have to run tho Django. I tried installing Mlabwrap ..But it gives me following error.Traceback (most recent call last): File "<st…

Suds ignoring proxy setting

Im trying to use the salesforce-python-toolkit to make web services calls to the Salesforce API, however Im having trouble getting the client to go through a proxy. Since the toolkit is based on top of…