The document provides an overview of NumPy, specifically focusing on ndarrays, their properties, and operations. It covers topics such as array storage methods, slicing, joining, and functions for statistical calculations like covariance and correlation. Additionally, it highlights the differences between ndarrays and Python lists, as well as the concept of vectorized operations.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
2 views2 pages
CH 1 Working With NumPy
The document provides an overview of NumPy, specifically focusing on ndarrays, their properties, and operations. It covers topics such as array storage methods, slicing, joining, and functions for statistical calculations like covariance and correlation. Additionally, it highlights the differences between ndarrays and Python lists, as well as the concept of vectorized operations.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2
CHECK POINT ANSWERS
CH – 1 WORKING WITH NUMPY
1. What is ndarray? An ndarray or a NumPy array is simply a grid that contains values of the same/ homogeneous type, indexed by a tuple of integers. The ndarrays can be 1-dimensional/ 2-dimensional or multi-dimensional. 2. What are the ways in which 2D arrays are stored in memory? Elements of arrays are stored in contiguous memory locations. Therefore, 2D arrays are linearised for storage purposes in one of these two alternatives: a. Row-major or Row-wise: This linearisation technique stores firstly the first row of the array, then the second row, and so forth. b. Column-major or Column-wise: This linearisation technique stores firstly the first column, then the second column and so forth. 3. Define: a. Axis: It refers to a particular dimension of a numpy arrays and multiple axis’ are called axes. b. Rank: It refers to the number of axes in an ndarray. c. Shape: It refers to the number of elements along each axis of an ndarray. d. Itemsize: It refers to the size of each element of an ndarray in bytes. 4. List one similarity of ndarrays and lists. Indexing in NumPy Arrays is similar to Lists i.e. forward indexing and backward indexing. 5. List one difference between ndarrays and lists. Size: Once a NumPy Array is created, you cannot change its size. In Python Lists, size is mutable. 6. Name two NumPy datatypes for text. Two NumPy datatypes for text are string_ and unicode_ 7. Name some functions for creating ndarrays. (Number of points depending upon the marks) Some of the functions for creating ndarrays are: a. np.array(<object>, [<dtype>]) b. np.fromiter(<iterable sequence name>, <target data type>, [<count>]) c. np.arange(start, stop, step, [<dtype>]) d. np.linspace(start, stop, number of values) e. np.empty(shape, [<dtype>], [order = ]) f. np.zeros(shape, [<dtype>], [order = ]) g. np.one(shape, [<dtype>], [order = ]) 8. What are array slices? Array Slicing refers to the process of extracting a subset of elements from an existing array and returning the result as another array, possibly in a different dimension from the original. Syntax for array slicing: a. For 1D array, <arrayname>[<start> : <stop> : <step>] b. For 2D array, <arrayname>[<start> : <stop> : <step>, <start> : <stop> : <step>] 9. Name some functions for joining ndarrays. Functions for joining arrays are: a. np.hstack(<tuple containing names of 1D arrays to be stacked>) b. np.vstack(<tuple containing names of 1D arrays to be stacked>) c. np.concatenate((<tuple of arrays to be joined>), [axis = <n>]), by default axis is 0 if skipped 10. Name the functions to obtain subsets from ndarrays. The functions to obtain subsets from ndarrays are: a. np.hsplit(<array>, <n>): It is used to extract the subsets of a NumPy array after splitting it horizontally or column wise. b. np.vsplit(<array>, <n>): It is used to extract the subsets of a NumPy array after splitting it vertically or row wise. c. np.split(<array>, <n>, [axis = ]): It is used to split a NumPy array both horizontally and vertically by providing axis arguments. 11. Name a function to obtain a non-contiguous subset from an ndarray. To obtain non-contiguous subsets of a NumPy array the following function is used: numpy.extract(<condition>, <array>) 12. What are vectorised operations? Vectorised operations means operation is performed on every element of the array. 13. How can you perform arithmetic operations on ndarrays? Arithmetic Operations can be performed on ndarrays in two ways: a. Using Operators: The conventional arithmetic operations are used such as +, -, *, /, %, etc. 1. Syntax: <ndarray1> <operator> <n>|<ndarray2> 2. E.g.: array1 + 2 or array1 * array2 b. Using Numpy Functions: Perform arithmetic operations on ndarrays with the various arithmetic functions. 14. Name some application of ndarrays. Some applications of ndarrays are – Covariance, Correlation and Linear Regression. 15. Write NumPy functions for calculating: a. Covariance b. Correlation c. Linear Regression a. Covariance: It is used to find how similar or dissimilar the data is. Syntax: np.cov(<arr1>, <arr2>) Where, ‘<arr1>’ and ‘<arr2>’ are two sets of observations. Output/ Result: 1. A high positive covariance means the data is strongly similar. 2. A high negative covariance means the data is very dissimilar. b. Correlation: It is used to tell if it is positive or a negative covariance. Syntax: np.corrcoef(<arr1>, <arr2>) Where, ‘<arr1>’ is a 1D or 2D array and ‘<arr2>’ is an array having same shape as <arr1>. Output/ Result: 1. It gives ‘1’ if the datasets have positive covariance. 2. It gives ‘-1’ if the datasets have negative covariance. c. Linear Regression: It is used to find on how one dataset is dependent on the other. Syntax: np.polyfit(x, y, deg) Where, 1. ‘x’ and ‘y’: They are arrays having the same shape. 2. ‘deg’: It specifies the degree of the fitting polynomial.