Quantinsti Quantitative Learning, Compiler
Installing Numpy NumPy is not a part of the Python Standard Library and hence, as with any other such library or module, it needs to be installed on a workstation before it can be used. Based on the Python distribution one uses, it can be installed via a command prompt, conda prompt, or terminal using the following command. pip install numpy One point to note is that if we use the Anaconda distribution to install Python, most of the libraries (like NumPy, pandas, scikit-learn, matplotlib, etc.) used in the scientific Python ecosystem come pre-installed. Note: If we use the Python or iPython console to install the NumPy library, the command to install it would be preceded by the character! Once installed we can use it by importing into our program by using the import statement. The de facto way of importing is shown below: import numpy as np Here, the NumPy library is imported with an alias of np so that any functionality within it can be used with convenience. We will be using this form of alias for all the examples in this section. NumPy Arrays A Python list is a pretty powerful sequential data structure with some nifty features like index sub-setting and traversal. But lists lack an important feature, carrying out operations over an entire collection of elements in an efficient manner. For example, consider a case where we calculate PCR (Put Call Ratio) for the previous 5 days. Say, we have put and call options volume (in Lacs) stored in lists call_vol and put_vol respectively. We then compute the PCR by dividing put volume by call volume as illustrated in the below script: put_vol = [52.89, 45.14, 63.84, 77.1, 74.6] # Put volume in lacs call_vol = [49.51, 50.45, 59.11, 80.49, 65.11] # Call volume in lacs # Computing Put Call Ratio (PCR) put_vol / call_vol Traceback (most recent call last): File "", line 1, in put_vol / call_vol TypeError: unsupported operand type(s) for /: 'list' and 'list' Unfortunately, Python threw an error while calculating PCR values as it h
Read this work at the World Public Library (library card needed).