NumPy is a Python library that provides support for large, multi-dimensional arrays and matrices, along with a large collection of mathematical functions to operate on these arrays. NumPy is a popular library for data analysis and scientific computing and is used extensively in finance and trading.
Installing NumPy
You can install NumPy using pip, the Python package manager. Simply open a terminal and enter the following command:
pip install numpy
Importing NumPy
After Numpy installation, you need to import it into your Python script. You can do this using the following line of code:
import numpy as np
The np
alias is commonly used for NumPy, and allows you to refer to the library in a shorter and more convenient way.
Creating Arrays
The core data structure in NumPy is the ndarray
, which stands for “n-dimensional array”. You can create an array by passing a list or tuple to the np.array()
function:
import numpy as np
# Create a one-dimensional array
a = np.array([1, 2, 3, 4, 5])
print('Create a one-dimensional array')
print(a)
# Create a two-dimensional array
b = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print('Create a two-dimensional array')
print(b)
Python Output
Create a one-dimensional array
[1 2 3 4 5]
Create a two-dimensional array
[[1 2 3]
[4 5 6]
[7 8 9]]
You can also create arrays of zeros or ones using the np.zeros()
and np.ones()
functions:
import numpy as np
# Create a one-dimensional array of zeros
a = np.zeros(5)
print('Create a one-dimensional array of zeros')
print(a)
# Create a two-dimensional array of ones
b = np.ones((3, 3))
print('Create a two-dimensional array of ones')
print(b)
Output
Create a one-dimensional array of zeros
[0. 0. 0. 0. 0.]
Create a two-dimensional array of ones
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
Indexing and Slicing Arrays
You can access individual elements of an array using indexing:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
# Get the first element of the array
print(a[0])
# Get the last element of the array
print(a[-1])
Output
1
5
You can also slice arrays to get a subset of the elements:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
# Get the first three elements of the array
print(a[:3])
# Get the last two elements of the array
print(a[-2:])
Output
[1 2 3]
[4 5]
Operations on Arrays
You can perform a wide variety of mathematical operations on arrays in NumPy. Here are a few examples:
import numpy as np
# Create two arrays
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
# Add the arrays together
c = a + b
print(c)
# Subtract the arrays
d = b - a
print(d)
# Multiply the arrays element-wise
e = a * b
print(e)
# Divide the arrays element-wise
f = b / a
print(f)
Output
[5 7 9]
[3 3 3]
[ 4 10 18]
[4. 2.5 2 ]
You can also perform mathematical operations on entire arrays, using functions like `np.sum()`, `np.mean()`, and `np.std()`:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
# Get the sum of the array
print(np.sum(a))
# Get the mean of the array
print(np.mean(a))
# Get the standard deviation of the array
print(np.std(a))
Output
15
3.0
1.4142135623730951
Converting a Numpy array to pandas dataframe
To convert a NumPy array to a Pandas DataFrame, you can use the pd.DataFrame()
function, which takes an array as its input and returns a DataFrame object. Here’s an example:
import numpy as np
import pandas as pd
# Create a NumPy array
data = np.array([[1, 2], [3, 4], [5, 6]])
# Convert the array to a DataFrame
df = pd.DataFrame(data, columns=['Column 1', 'Column 2'])
# Print the DataFrame
print(df)
Output
Column 1 Column 2
0 1 2
1 3 4
2 5 6
In this example, we first create a NumPy array called data
, which contains three rows and two columns of data. We then use the pd.DataFrame()
function to convert the array to a DataFrame, and specify the column names using the columns
parameter. Finally, we print the resulting DataFrame to the console.
Note that when converting a NumPy array to a DataFrame, it’s important to specify column names (either manually or by using the data
parameter with named columns). Otherwise, the resulting DataFrame will have default column names like “0”, “1”, etc., which may not be very informative.
Reshaping Arrays
You can reshape arrays using the np.reshape()
function:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
# Reshape the array into a 2x3 matrix
b = np.reshape(a, (2, 3))
print(b)
Output
[[1 2 3]
[4 5 6]]
You can also use the np.ravel()
function to flatten an array:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
# Flatten the array
b = np.ravel(a)
print(b)
Output
[1 2 3 4 5 6]
Broadcasting
NumPy also supports broadcasting, which allows you to perform mathematical operations on arrays of different shapes and sizes:
import numpy as np
# Create an array
a = np.array([1, 2, 3])
# Multiply the array by 2
b = a * 2
print(b)
Output
[2 4 6]
Generating Random Numbers
NumPy also provides support for generating random numbers. Here’s an example:
import numpy as np
# Generate a random number between 0 and 1
a = np.random.rand()
print(a)
# Generate an array of random numbers
b = np.random.rand(5)
print(b)
Output
0.6232014363018096
[0.74894215 0.80172241 0.83527317 0.186123 0.31004827]
Filtering
import numpy as np
# Create a NumPy array
arr = np.array([1, 2, 3, 4, 5])
# Use np.where to find indices where a condition is true
indices = np.where(arr > 2)
# Print the result
print("Indices where elements are greater than 2:", indices)
print("Elements at those indices:", arr[indices])
Output
Indices where elements are greater than 2: (array([2, 3, 4], dtype=int64),)
Elements at those indices: [3 4 5]
Generate Arrays between Specific Range
import numpy as np
# Generate an array with values within a specified range with a specified step size
start = 0
stop = 11
step = 2
arange_arr = np.arange(start, stop, step)
# Print the result
print("Arange array:", arange_arr)
Output
Arange array: [ 0 2 4 6 8 10]
import numpy as np
# Generate evenly spaced values between start and stop (inclusive) with a specified number of points
start = 0
stop = 1
num_points = 5
linspace_arr = np.linspace(start, stop, num_points)
# Print the result
print("Linspace array:", linspace_arr)
Output
Linspace array: [0.0 0.25 0.5 0.75 1.0 ]
Here’s a list of some popular NumPy functions organized in a table format:
Function Name | Description |
---|---|
numpy.array | Create an array |
numpy.arange | Create an array with regularly spaced values |
numpy.linspace | Create an array with linearly spaced values |
numpy.zeros | Create an array filled with zeros |
numpy.ones | Create an array filled with ones |
numpy.eye | Create a 2-D identity matrix |
numpy.random.rand | Generate random values in a given shape (uniform dist.) |
numpy.random.randn | Generate random values in a given shape (normal dist.) |
numpy.shape | Get the shape of an array |
numpy.reshape | Reshape an array |
numpy.size | Get the number of elements in an array |
numpy.ndim | Get the number of dimensions in an array |
numpy.mean | Compute the mean of an array |
numpy.median | Compute the median of an array |
numpy.sum | Compute the sum of array elements |
numpy.min | Find the minimum value in an array |
numpy.max | Find the maximum value in an array |
numpy.argmin | Find the index of the minimum value in an array |
numpy.argmax | Find the index of the maximum value in an array |
numpy.unique | Find unique elements in an array |
numpy.transpose | Transpose of an array |
numpy.dot | Dot product of two arrays |
numpy.linalg.inv | Compute the inverse of a matrix |
numpy.concatenate | Concatenate arrays along a given axis |
numpy.split | Split an array into multiple sub-arrays |
numpy.vstack | Stack arrays vertically |
numpy.hstack | Stack arrays horizontally |
numpy.sin , numpy.cos , numpy.tan | Trigonometric functions |
numpy.exp | Compute the exponent of each element |
numpy.log , numpy.log10 | Natural logarithm, base-10 logarithm |
NumPy is a powerful library for working with arrays and matrices in Python, and is especially useful for finance and trading applications. In this tutorial, we covered some of the basic features of NumPy, including creating arrays, indexing and slicing, performing mathematical operations, reshaping arrays, broadcasting, and generating random numbers. With these tools, you should be able to start using NumPy in your trading applications.