{"id":27356,"date":"2022-02-23T05:01:48","date_gmt":"2022-02-23T05:01:48","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=27356"},"modified":"2022-12-23T06:48:50","modified_gmt":"2022-12-23T06:48:50","slug":"top-50-numpy-interview-questions-and-answers","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/top-50-numpy-interview-questions-and-answers\/","title":{"rendered":"Top 50 Numpy interview questions and answers"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/465-4651848_numpy-python-logo-hd-png-download-copy.png\" alt=\"\" class=\"wp-image-27475\" width=\"754\" height=\"333\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/465-4651848_numpy-python-logo-hd-png-download-copy.png 860w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/465-4651848_numpy-python-logo-hd-png-download-copy-300x133.png 300w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2022\/02\/465-4651848_numpy-python-logo-hd-png-download-copy-768x339.png 768w\" sizes=\"auto, (max-width: 754px) 100vw, 754px\" \/><figcaption><em><strong>Numpy interview questions and answers <\/strong><\/em><\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-what-is-numpy\">1) What is Numpy?<\/h2>\n\n\n\n<p>Ans: NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package for scientific computing with Python. \u2026 A powerful N-dimensional array object. Sophisticated (broadcasting) functions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-why-numpy-is-used-in-python\">2) Why NumPy is used in Python?<\/h2>\n\n\n\n<p>Ans: NumPy is a package in Python used for Scientific Computing. NumPy package is used to perform different operations. The ndarray (NumPy Array) is a multidimensional array used to store values of same datatype. These arrays are indexed just like Sequences, starts with zero.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-what-does-numpy-mean-in-python\">3) What does NumPy mean in Python?<\/h2>\n\n\n\n<p>Ans: NumPy (pronounced \/\u02c8n\u028cmpa\u026a\/ (NUM-py) or sometimes \/\u02c8n\u028cmpi\/ (NUM-pee)) is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-where-is-numpy-used\">4) Where is NumPy used?<\/h2>\n\n\n\n<p>Ans: NumPy is an open source numerical Python library. NumPy contains a multi-dimentional array and matrix data structures. It can be utilised to perform a number of mathematical operations on arrays such as trigonometric, statistical and algebraic routines. NumPy is an extension of Numeric and Numarray.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-how-to-install-numpy-in-windows\">5) How to Install Numpy in Windows?<\/h2>\n\n\n\n<p>Ans: Step 1: Download Python for Windows 10\/8\/7. First, download the Python executable binaries on your Windows system from the official download the page of the Python. \u2026<br>Step 2: Run the Python executable installer. \u2026<br>Step 3: Install pip on Windows 10\/8\/7. \u2026<br>Step 4: Install Numpy in Python using pip on Windows 10\/8\/7.<\/p>\n\n\n\n<p>Installation Process of Numpy..<\/p>\n\n\n\n<p>step1: Open the terminal<\/p>\n\n\n\n<p>step2: type pip install numpy<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"6-how-to-import-numpy-in-python\">6) How to import numpy in python?<\/h2>\n\n\n\n<p>Ans: import numpy as np<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-how-to-create-1d-array\">7) how to create 1D Array ?<\/h2>\n\n\n\n<p>Ans: num=[1,2,3]<br>num = np.array(num)<br>print(\u201c1d array : \u201c,num)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"8-how-to-create-2d-array\">8) How to create 2D Array ?<\/h2>\n\n\n\n<p>Ans: num2=[[1,2,3],[4,5,6]]<br>num2 = np.array(num2)<br>print(\u201c\\n2d array : \u201c,num2)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"9-how-to-create-3d-array-or-nd-array\">9) How to create 3D Array or ND Array ?<\/h2>\n\n\n\n<p>Ans: num3=[[[1,2,3],[4,5,6],[7,8,9]]]<br>num3 = np.array(num3)<br>print(\u201c\\n3d array : \u201c,num3)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"10-how-to-use-shape-for-1d-array\">10) How to use shape for 1D Array ?<\/h2>\n\n\n\n<p>Ans: num=[1,2,3] if not defined<br>print(\u2018\\nshpae of 1d \u2018,num.shape)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"11-how-to-use-shape-for-2d-array\">11) How to use shape for 2D Array ?<\/h2>\n\n\n\n<p>Ans: num2=[[1,2,3],[4,5,6]] if not added<br>print(\u2018\\nshpae of 2d \u2018,num2.shape)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"12-how-to-use-shape-for-3d-or-nd-array\">12) How to use shape for 3d or Nd Array ?<\/h2>\n\n\n\n<p>Ans: num3=[[[1,2,3],[4,5,6],[7,8,9]]] if not added<br>print(\u2018\\nshpae of 3d \u2018,num3.shape)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"13-how-to-identified-datatyp-for-numpy-array\">13) How to identified datatyp for numpy array?<\/h2>\n\n\n\n<p>Ans: print(\u2018\\n data type num 1 \u2018,num.dtype)<br>print(\u2018\\n data type num 2 \u2018,num2.dtype)<br>print(\u2018\\n data type num 3 \u2018,num3.dtype)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"14-print-5-zeros\">14) Print 5 zeros ?<\/h2>\n\n\n\n<p>Ans: arr = np.zeros(5)<br>print(\u2018single arrya\u2019,arr)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"15-print-zeros-with-2-rows-and-3-columns\">15) print zeros with 2 rows and 3 columns ?<\/h2>\n\n\n\n<p>Ans: arr2 = np.zeros((2,3))<br>print(\u2018\\nprint 2 rows and 3 cols : \u2018,arr2)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"16-use-of-eye-diagonal-values\">16) use of eye() diagonal values ?<\/h2>\n\n\n\n<p>Ans: arr3 = np.eye(4)<br>print(\u2018\\ndiaglonal values : \u2018,arr3)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17-use-of-diag-square-matrix\">17) use of diag() square matrix ?<\/h2>\n\n\n\n<p>Ans: arr3 = np.diag([1,2,3,4])<br>print(\u2018\\n square matrix\u2019,arr3)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"18-print-range-between-1-to-15-and-show-4-integers-random-numbers\">18) Print Range Between 1 To 15 and show 4 integers random numbers<\/h2>\n\n\n\n<p>Ans: rand_arr = np.random.randint(1,15,4)<br>print(\u2018\\n random number from 1 to 15 \u2018,rand_arr)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"19-print-range-between-1-to-100-and-show-4-integers-random-numbers\">19) Print Range Between 1 To 100 and show 4 integers random numbers<\/h2>\n\n\n\n<p>Ans: rand_arr3 = np.random.randint(1,100,20)<br>print(\u2018\\n random number from 1 to 100 \u2018,rand_arr3)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"20-print-range-between-random-number-2-row-and-3-cols-integers-random-numbers\">20) Print Range Between random number 2 row and 3 cols integers random numbers<\/h2>\n\n\n\n<p>Ans: rand_arr2 = np.random.randint([2,3])<br>print(\u2018\\n random number 2 row and 3 cols \u2018,rand_arr2)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"21-i-ve-found-a-bug-what-do-i-do\">21) I\u2019ve Found A Bug. What Do I Do?<\/h2>\n\n\n\n<p>The SciPy development team works hard to make SciPy as reliable as possible, but, as in any software product, bugs do occur. If you find bugs that affect your software, please tell us by entering a ticket in the SciPy bug tracker, or NumPy bug tracker, as appropriate.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"22-how-can-i-get-involved-in-scipy\">22) How Can I Get Involved In Scipy?<\/h2>\n\n\n\n<p>Drop us a mail on the mailing lists. We are keen for more people to help out writing code, unit tests, documentation (including translations into other languages), and helping out with the website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"23-is-there-commercial-support-available\">23) Is There Commercial Support Available?<\/h2>\n\n\n\n<p>Yes, commercial support is offered for SciPy by Enthought.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"24-what-is-the-difference-between-numpy-and-scipy\">24) What Is The Difference Between Numpy And Scipy?<\/h2>\n\n\n\n<p>In an ideal world, NumPy would contain nothing but the array data type and the most basic operations: indexing, sorting, reshaping, basic elementwise functions, et cetera. All numerical code would reside in SciPy. However, one of NumPy\u2019s important goals is compatibility, so NumPy tries to retain all features supported by either of its predecessors. Thus NumPy contains some linear algebra functions, even though these more properly belong in SciPy. In any case, SciPy contains more fully-featured versions of the linear algebra modules, as well as many other numerical algorithms. If you are doing scientific computing with python, you should probably install both NumPy and SciPy. Most new features belong in SciPy rather than NumPy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"25-how-do-i-make-plots-using-numpy-scipy\">25) How Do I Make Plots Using Numpy\/scipy?<\/h2>\n\n\n\n<p>Plotting functionality is beyond the scope of NumPy and SciPy, which focus on numerical objects and algorithms. Several packages exist that integrate closely with NumPy to produce high quality plots, such as the immensely popular Matplotlib and the extensible, modular toolkit Chaco.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"26-how-do-i-make-3d-plots-visualizations-using-numpy-scipy\">26) How Do I Make 3d Plots\/visualizations Using Numpy\/scipy?<\/h2>\n\n\n\n<p>Like 2D plotting, 3D graphics is beyond the scope of NumPy and SciPy, but just as in the 2D case, packages exist that integrate with NumPy. Matplotlib provides basic 3D plotting in the mplot3d subpackage, whereas Mayavi provides a wide range of high-quality 3D visualization features, utilizing the powerful VTK engine.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"27-why-both-numpy-linalg-and-scipy-linalg-what-s-the-difference\">27) Why Both Numpy.linalg And Scipy.linalg? What\u2019s The Difference?<\/h2>\n\n\n\n<p>One of the design goals of NumPy was to make it buildable without a Fortran compiler, and if you don\u2019t have LAPACK available NumPy will use its own implementation. SciPy requires a Fortran compiler to be built, and heavily depends on wrapped Fortran code.<\/p>\n\n\n\n<p>The linalg modules in NumPy and SciPy have some common functions but with different docstrings, and scipy.linalgcontains functions not found in numpy.linalg, such as functions related to LU decomposition and the Schur decomposition, multiple ways of calculating the pseudoinverse, and matrix transcendentals like the matrix logarithm. Some functions that exist in both have augmented functionality in scipy.linalg; for example scipy.linalg.eig() can take a second matrix argument for solving generalized eigenvalue problems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"28-do-numpy-and-scipy-support-python-3-x\">28) Do Numpy And Scipy Support Python 3.x?<\/h2>\n\n\n\n<p>NumPy and SciPy support the Python 2.x series, (versions 2.6 and 2.7), as well as Python 3.2 and newer. The first release of NumPy to support Python 3 was NumPy 1.5.0. Python 3 support in SciPy starts with version 0.9.0.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"29-does-numpy-scipy-work-with-jython\">29) Does Numpy\/scipy Work With Jython?<\/h2>\n\n\n\n<p>No. Simply put, Jython runs on top of the Java Virtual Machine and has no way to interface with extensions written in C for the standard Python (CPython) interpreter.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"30-does-numpy-scipy-work-with-ironpython-net\">30) Does Numpy\/scipy Work With Ironpython (.net)?<\/h2>\n\n\n\n<p>Some users have reported success in using NumPy with Ironclad on 32-bit Windows. The current status of Ironclad support for SciPy is unknown, but there are several complicating factors (namely the Fortran compiler situation on Windows) that make it less feasible than NumPy.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"31-what-is-the-preferred-way-to-check-for-an-empty-zero-element-array\">31) What Is The Preferred Way To Check For An Empty (zero Element) Array?<\/h2>\n\n\n\n<p>If you are certain a variable is an array, then use the size attribute. If the variable may be a list or other sequence type, use len().<\/p>\n\n\n\n<p>The size attribute is preferable to len because:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>a = numpy.zeros((1,0))<br>a.size<br>0<br>whereas<br>len(a)<br>1<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"32-i-want-to-load-data-from-a-text-file-how-do-i-make-this-code-more-efficient\">32) I Want To Load Data From A Text File. How Do I Make This Code More Efficient?<\/h2>\n\n\n\n<p>Use numpy.loadtxt(). Even if your text file has header and footer lines or comments, loadtxt can almost certainly read it; it is convenient and efficient.<\/p>\n\n\n\n<p>If you find this still too slow, you should consider changing to a more efficient file format than plain text. There are a large number of alternatives, depending on your needs (and on which version of NumPy\/SciPy you are using):<\/p>\n\n\n\n<p>Text files: slow, huge, portable, human-readable; built into <\/p>\n\n\n\n<p>NumPy Raw binary: no metadata, totally unportable, fast; built into <\/p>\n\n\n\n<p>NumPy pickle: somewhat slow, somewhat portable (may be incompatible with different NumPy versions); built into NumPy<\/p>\n\n\n\n<p>MATLAB format: portable; built into SciPy (scipy.io.loadmat()) <\/p>\n\n\n\n<p>HDF5: high-powered kitchen-sink format; both PyTables and h5py provide a NumPy friendly interface on top of the core HDF5 library written in C. <\/p>\n\n\n\n<p>FITS: standard kitchen-sink format in astronomy; the astropy library provides a convenient Python interface through its io.fits package. .npy: NumPy native binary data format, simple, efficient, portable; built into NumPy as of 1.0.5.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"33-why-not-just-have-a-separate-operator-for-matrix-multiplication\">33) Why Not Just Have A Separate Operator For Matrix Multiplication?<\/h2>\n\n\n\n<p>From Python 3.5, the @ symbol will be defined as a matrix multiplication operator, and Numpy and Scipy will make use of this. This addition was the subject of PEP 465. The separate matrix and array types exist to work around the lack of this operator in earlier versions of Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"34-how-do-i-find-the-indices-of-an-array-where-some-condition-is-true\">34) How Do I Find The Indices Of An Array Where Some Condition Is True?<\/h2>\n\n\n\n<p>Answer :The prefered idiom for doing this is to use the function numpy.nonzero() , or the nonzero() method of an array. Given an array a, the condition a &gt; 3 returns a boolean array and since False is interpreted as 0 in Python and NumPy, np.nonzero(a &gt; 3)yields the indices of a where the condition is true.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>import numpy as np<br>a = np.array([[1,2,3],[4,5,6],[7,8,9]])<br>a &gt; 3<br>array([[False, False, False],<br>[ True, True, True],<br>[ True, True, True]], dtype=bool)<br>np.nonzero(a &gt; 3)<br>(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))<br>The nonzero() method of the boolean array can also be called.<br>(a &gt; 3).nonzero()<br>(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"35-how-do-i-count-the-number-of-times-each-value-appears-in-an-array-of-integers\">35) How Do I Count The Number Of Times Each Value Appears In An Array Of Integers?<\/h2>\n\n\n\n<p>Use numpy.bincount(). The resulting array is<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>arr = numpy.array([0, 5, 4, 0, 4, 4, 3, 0, 0, 5, 2, 1, 1, 9])<br>numpy.bincount(arr)<\/p><\/blockquote>\n\n\n\n<p>The argument to bincount() must consist of positive integers or booleans. Negative integers are not supported.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"36-list-the-advantages-numpy-arrays-have-over-nested-python-lists\">36) List the advantages NumPy Arrays have over (nested) Python lists?<\/h2>\n\n\n\n<p>Python\u2019s lists, even though hugely efficient containers capable of a number of functions, have several limitations when compared to NumPy arrays. It is not possible to perform vectorised operations which includes element-wise addition and multiplication.<\/p>\n\n\n\n<p>They also require that Python store the type information of every element since they support objects of different types. This means a type dispatching code must be executed each time an operation on an element is done. Also, each iteration would have to undergo type checks and require Python API bookkeeping resulting in very few operations being carried by C loops.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"37-list-the-steps-to-create-a-1d-array-and-2d-array\">37) List the steps to create a 1D array and 2D array<\/h2>\n\n\n\n<p>A one-dimensional array is created as follows:<\/p>\n\n\n\n<p>num=[1,2,3]<\/p>\n\n\n\n<p>num = np.array(num)<\/p>\n\n\n\n<p>print(\u201c1d array : \u201c,num)<\/p>\n\n\n\n<p>A two-dimensional array is created as follows:<\/p>\n\n\n\n<p>num2=[[1,2,3],[4,5,6]]<\/p>\n\n\n\n<p>num2 = np.array(num2)<\/p>\n\n\n\n<p>print(\u201c\\n2d array : \u201c,num2)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"38-how-do-you-create-a-3d-array\">38) How do you create a 3D array?<\/h2>\n\n\n\n<p>A three-dimensional array is created as follows:<\/p>\n\n\n\n<p>num3=[[[1,2,3],[4,5,6],[7,8,9]]]<\/p>\n\n\n\n<p>num3 = np.array(num3)<\/p>\n\n\n\n<p>print(\u201c\\n3d array : \u201c,num3)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"39-what-are-the-steps-to-use-shape-for-a-1d-array-2d-array-and-3d-nd-array-respectively\">39) What are the steps to use shape for a 1D array, 2D array and 3D\/ND array respectively?<\/h2>\n\n\n\n<p>1D Array:<\/p>\n\n\n\n<p>num=[1,2,3] if not added<\/p>\n\n\n\n<p>print(\u2018\\nshpae of 1d \u2018,num.shape)<\/p>\n\n\n\n<p>2D Array:<\/p>\n\n\n\n<p>num2=[[1,2,3],[4,5,6]] if not added<\/p>\n\n\n\n<p>print(\u2018\\nshpae of 2d \u2018,num2.shape)<\/p>\n\n\n\n<p>3D or ND Array:<\/p>\n\n\n\n<p>num3=[[[1,2,3],[4,5,6],[7,8,9]]] if not added<\/p>\n\n\n\n<p>print(\u2018\\nshpae of 3d \u2018,num3.shape)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"40-how-can-you-identify-the-datatype-of-a-given-numpy-array\">40) How can you identify the datatype of a given NumPy array?<\/h2>\n\n\n\n<p>Use the following sequence of codes to identify the datatype of a NumPy array.<\/p>\n\n\n\n<p>print(\u2018\\n data type num 1 \u2018,num.dtype)<\/p>\n\n\n\n<p>print(\u2018\\n data type num 2 \u2018,num2.dtype)<\/p>\n\n\n\n<p>print(\u2018\\n data type num 3 \u2018,num3.dtype)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"41-what-is-the-procedure-to-count-the-number-of-times-a-given-value-appears-in-an-array-of-integers\">41) What is the procedure to count the number of times a given value appears in an array of integers?<\/h2>\n\n\n\n<p>You can count the number of the times a given value appears using the bincount() function. It should be noted that the bincount() function accepts positive integers or boolean expressions as its argument. Negative integers cannot be used.<\/p>\n\n\n\n<p>Use NumPy.bincount(). The resulting array is<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>arr = NumPy.array([0, 5, 4, 0, 4, 4, 3, 0, 0, 5, 2, 1, 1, 9])<\/p><p>NumPy.bincount(arr)<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"42-what-is-the-procedure-to-find-the-indices-of-an-array-on-numpy-where-some-condition-is-true\">42) What is the procedure to find the indices of an array on NumPy where some condition is true?<\/h2>\n\n\n\n<p>You may use the function numpy.nonzero() to find the indices or an array. You can also use the nonzero() method to do so.<\/p>\n\n\n\n<p>In the following program, we will take an array a, where the condition is a &gt; 3. It returns a boolean array. We know False on Python and NumPy is denoted as 0. Therefore, np.nonzero(a &gt; 3) will return the indices of the array a where the condition is True.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>import numpy as np<\/p><p>a = np.array([[1,2,3],[4,5,6],[7,8,9]])<\/p><p>a &gt; 3<\/p><\/blockquote>\n\n\n\n<p>array([[False, False, False],<\/p>\n\n\n\n<p>   [ True,  True,  True],    <\/p>\n\n\n\n<p>[ True,  True,  True]], dtype=bool)<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>np.nonzero(a &gt; 3)<\/p><\/blockquote>\n\n\n\n<p>(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))<\/p>\n\n\n\n<p>You can also call the nonzero() method of the boolean array.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>(a &gt; 3).nonzero()<\/p><\/blockquote>\n\n\n\n<p>(array([1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"43-shown-below-is-the-input-numpy-array-delete-column-two-and-replace-it-with-the-new-column-given-below\">43) Shown below is the input NumPy array. Delete column two and replace it with the new column given below.<\/h2>\n\n\n\n<p>import NumPy<\/p>\n\n\n\n<p>sampleArray = NumPy.array([[34,43,73],[82,22,12],[53,94,66]])<\/p>\n\n\n\n<p>newColumn = NumPy.array([[10,10,10]])<\/p>\n\n\n\n<p>Expected Output:<\/p>\n\n\n\n<p>Printing Original array<\/p>\n\n\n\n<p>[[34 43 73]<\/p>\n\n\n\n<p>[82 22 12]<\/p>\n\n\n\n<p>[53 94 66]]<\/p>\n\n\n\n<p>Array after deleting column 2 on axis 1<\/p>\n\n\n\n<p>[[34 73]<\/p>\n\n\n\n<p>[82 12]<\/p>\n\n\n\n<p>[53 66]]<\/p>\n\n\n\n<p>Array after inserting column 2 on axis 1<\/p>\n\n\n\n<p>[[34 10 73]<\/p>\n\n\n\n<p>[82 10 12]<\/p>\n\n\n\n<p>[53 10 66]]<\/p>\n\n\n\n<p>Solution:<\/p>\n\n\n\n<p>import NumPy<\/p>\n\n\n\n<p>print(\u201cPrinting Original array\u201d)<\/p>\n\n\n\n<p>sampleArray = NumPy.array([[34,43,73],[82,22,12],[53,94,66]])<\/p>\n\n\n\n<p>print (sampleArray)<\/p>\n\n\n\n<p>print(\u201cArray after deleting column 2 on axis 1\u201d)<\/p>\n\n\n\n<p>sampleArray = NumPy.delete(sampleArray , 1, axis = 1)<\/p>\n\n\n\n<p>print (sampleArray)<\/p>\n\n\n\n<p>arr = NumPy.array([[10,10,10]])<\/p>\n\n\n\n<p>print(\u201cArray after inserting column 2 on axis 1\u201d)<\/p>\n\n\n\n<p>sampleArray = NumPy.insert(sampleArray , 1, arr, axis = 1)<\/p>\n\n\n\n<p>print (sampleArray)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"44-create-a-two-2-d-array-plot-it-using-matplotlib\">44) Create a two 2-D array. Plot it using matplotlib<\/h2>\n\n\n\n<p>Solution:<\/p>\n\n\n\n<p>import NumPy<\/p>\n\n\n\n<p>print(\u201cPrinting Original array\u201d)<\/p>\n\n\n\n<p>sampleArray = NumPy.array([[34,43,73],[82,22,12],[53,94,66]])<\/p>\n\n\n\n<p>print (sampleArray)<\/p>\n\n\n\n<p>print(\u201cArray after deleting column 2 on axis 1\u201d)<\/p>\n\n\n\n<p>sampleArray = NumPy.delete(sampleArray , 1, axis = 1)<\/p>\n\n\n\n<p>print (sampleArray)<\/p>\n\n\n\n<p>arr = NumPy.array([[10,10,10]])<\/p>\n\n\n\n<p>print(\u201cArray after inserting column 2 on axis 1\u201d)<\/p>\n\n\n\n<p>sampleArray = NumPy.insert(sampleArray , 1, arr, axis = 1)<\/p>\n\n\n\n<p>print (sampleArray)<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"45-why-numpy-is-faster-than-list\">45) Why NumPy is faster than list?<\/h2>\n\n\n\n<p>NumPy Arrays are faster than Python Lists because of the following reasons: An array is a collection of homogeneous data-types that are stored in contiguous memory locations. On the other hand, a list in Python is a collection of heterogeneous data types stored in non-contiguous memory locations<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"46-what-is-the-main-use-of-numpy\">46) What is the main use of NumPy?<\/h2>\n\n\n\n<p>NumPy can be used to perform a wide variety of mathematical operations on arrays. It adds powerful data structures to Python that guarantee efficient calculations with arrays and matrices and it supplies an enormous library of high-level mathematical functions that operate on these arrays and matrices.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"47-does-numpy-use-multiple-cores\">47) Does NumPy use multiple cores?<\/h2>\n\n\n\n<p>I know that numpy is configured for multiple cores, since I can see tests using numpy. dot use all my cores, so I just reimplemented mean as a dot product, and it runs way faster.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"48-what-is-difference-between-numpy-and-pandas\">48) What is difference between NumPy and Pandas?<\/h2>\n\n\n\n<p>Difference between Pandas and NumPy: \u2026 NumPy library provides objects for multi-dimensional arrays, whereas Pandas is capable of offering an in-memory 2d table object called DataFrame. NumPy consumes less memory as compared to Pandas. Indexing of the Series objects is quite slow as compared to NumPy arrays.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"49-can-you-import-libraries-in-coding-interviews\">49) Can you import libraries in coding interviews?<\/h2>\n\n\n\n<p>During a coding interview, you can always ask if you can use such-and-such a function from the library, but generally you can as long as that doesn&#8217;t save you from actually having to solve the problem. If you&#8217;re asked to implement a sort, for example, you shouldn&#8217;t do it by calling the library sort.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"50-does-numpy-use-simd\">50) Does NumPy use SIMD?<\/h2>\n\n\n\n<p>NumPy comes with a flexible working mechanism that allows it to harness the SIMD features that CPUs own, in order to provide faster and more stable performance on all popular platforms.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"related-video\">Related video:<\/h4>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\"  id=\"_ytid_31863\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/QUT1VHiLmmI?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  epyt-is-override  no-lazyload\" title=\"YouTube player\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>1) What is Numpy? Ans: NumPy is a general-purpose array-processing package. It provides a high-performance multidimensional array object, and tools for working with these arrays. It is the fundamental package&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[52],"tags":[1729,5432,6163,5377,3120,6161,766,3347,173,5611,6198,7184,666,5738,686,176,179,637,182],"class_list":["post-27356","post","type-post","status-publish","format-standard","hentry","category-interview-questions-answers","tag-advantages","tag-arrays","tag-booleans","tag-coding","tag-download","tag-integers","tag-interview","tag-interview-questions-answers","tag-java","tag-javascript","tag-multidimensional-arrays","tag-numpy","tag-open-source","tag-programmers","tag-programming-language","tag-python","tag-software","tag-top","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/27356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=27356"}],"version-history":[{"count":2,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/27356\/revisions"}],"predecessor-version":[{"id":27476,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/27356\/revisions\/27476"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=27356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=27356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=27356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}