Computer Science, asked by Anonymous, 9 months ago

\huge\bold{Question:-}


Explain :-

Array Creation using built-in functions NumPy module.

<marquee>All the best !</marquee>

Answers

Answered by AdorableMe
118

Array creation using built-in functions :-

An explicit input has been provided while creating \tt{n \_call\_vol} and \tt{n\_put\_vol} arrays. In contrast, NumPy provides various built-in functions to create arrays and input to them will be produced by NumPy. Below are some handful of such functions:

zeros \tt{(shape,\ dtype=float)} returns an array of a given shape and type, filled with zeros. If the dtype is not provided as an input, the default type for the array would be float.

# \textsl{ Creating a one-dimensional array}

\tt{In []: np.zeros(5)}

\tt{Out[]: array([0., 0., 0., 0., 0.])}

# \textsl{ Creating a two-dimensional array}

\tt{In []: np.zeros((3, 5))}\\\\\tt{Out[]: }\\\\\tt{array([[0., 0., 0., 0., 0.],}\\\\\tt{\:\:\:\:\:\:\:\:\:\: [0., 0., 0., 0., 0.],}}

\tt{\:\:\:\:\:\:\:\:\:[0., 0., 0., 0., 0.]])}

# \textsl{Creating a one-dimensional array of integer type}

\tt{In []: np.zeros(5, dtype=int)}\\\\\tt{Out[]: array([1, 1, 1, 1, 1])}

_____________________

•  full \tt{(shape,\ fill\_value,\ dtype=None)} returns an array of a given shape and type, fill with fill_value given in input parameters.

# \textsl{Creating a one-dimensional array with value as 12}

\tt{In []: np.full(5, 12)}\\\\\tt{Out[]: array([12, 12, 12, 12, 12])}

# \textsl{Creating a two-dimensional array with value as 9}

\tt{In []: np.full((2, 3), 9)}\\\\\tt{Out[]:}\\\\\tt{array([9, 9, 9],}\\\tt{\:\:\:\:\:\:\:\:\:\:\:\:\:\:[9, 9, 9]])}

_____________________

arrange \tt{([start,\ ]stop,\ [step])} returns an array with evenly spaced values within a given interval. Here the start and step parameters are optional. If they are provided NumPy will consider them while computing the output. Otherwise, range computation starts from 0. For all cases, stop value will be excluded in the output.

# \textsl{Creating an array with only stop argument}

\tt{In []: np.aarange(3, 8, 0.5)}\\\\\tt{Out[]: array([3. , 3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5, 7. ,7.5])}

_____________________

• linspace \tt{(start,\ stop,\ num=50,\ endpoint=True)} returns evenly spaced numbers over a specified interval. The number of samples to be returned is specified by the num parameter. The endpoint of the interval can optionally be excluded.

# \textsl{Creating an evenly spaced array with five numbers within}

# \textsl{interval 2 to 3}

\tt{In []: np.linspace(2.0, 3.0, num=5)}\\\\\tt{Out[]: array([2. , 2.25, 2.5 , 2.75, 3. ])}

# \textsl{ Creating an array excluding end value}

\tt{In []: np.linspace(2.0, 3.0,\ num=5,\ endpoint=False)}\\\\\tt{Out[]: array([2. , 2.2, 2.4, 2.6, 2.8])}

# \textsl{Creating an array with ten values within the specified}

# \textsl{interval}

\tt{In []: np.linspace(11, 20, num=10)}\\\\\tt{Out[]: array([11., 12., 13., 14., 15., 16., 17., 18., 19.,20.])}

✨ Hope this helps ;)

Similar questions