library(reticulate)
py_install("numpy")
Using virtual environment "~/.virtualenvs/r-reticulate" ...
py_install("matplotlib")
Using virtual environment "~/.virtualenvs/r-reticulate" ...
NumPy is a Python library used for working with arrays, linear algebra, fourier transform, and matrices. NumPy stands for Numerical Python and it is an open source project. The array object in NumPy is called ndarray, it provides a lot of supporting functions that make working with ndarray very easy.
Arrays are very frequently used in data science, where speed and resources are very important.
Using virtual environment "~/.virtualenvs/r-reticulate" ...
Using virtual environment "~/.virtualenvs/r-reticulate" ...
Each element in an array is of the same type:
Note: ([ ]) the parenthesis and brackets
Just like a list, and similar to df, and dictionaries we can access each element by their location
You can use arithmetic operators on numpy arrays:
You can visualize the operation like this
import time
import sys
def Plotvec1(u, z, v):
ax = plt.axes() # to generate the full window axes
ax.arrow(0, 0, *u, head_width=0.05, color='r', head_length=0.1)
# Add an arrow to the U Axes with arrow head width 0.05, color red and arrow head length 0.1
plt.text(*(u + 0.1), 'u') # Adds the text u to the Axes
ax.arrow(0, 0, *v, head_width=0.05, color='b', head_length=0.1)
# Add an arrow to the v Axes with arrow head width 0.05, color red and arrow head length 0.1
plt.text(*(v + 0.1), 'v') #Adds the text v to the Axes
ax.arrow(0, 0, *z, head_width=0.05, head_length=0.1)
plt.text(*(z + 0.1), 'z') #Adds the text z to the Axes
plt.ylim(-2, 2) #set the ylim to bottom(-2), top(2)
plt.xlim(-2, 2) #set the xlim to left(-2), right(2)
Example:
numpy.linspace(start, stop, num = int value)
array([-2. , -1.5, -1. , -0.5, 0. , 0.5, 1. , 1.5, 2. ])
A 1D array is often termed a vector. Depending on the orientation of the data, it could be a row vector or a column vector
You can add or subtract two vectors by going across:
The same apply, just multiply across.
Note all these operations require both to have the same size
Here the same applies, the constant is added, subtracted, or multiplied to or from each element
A 2D array is called a matrix. They are called rectangular arrays. The operations above apply to these as well in the same fashion.
Dot product follows a different rule:
As before we can use [] to access different elements of the array.
# Access element on the second row and 3rd column
a = [[11, 12, 13], [21, 22, 23], [31, 32, 33]]
A = np.array(a)
A[1,2]
23
23
Multiplication of two arrays corresponds to an element-wise product Hadamard product. Therefore you multiply each of the elements in the same position.
# Create two matrices
A = np.array([[0, 1, 1], [1, 0, 1]])
B = np.array([[1, 1], [1, 1], [-1, 1]])
A
array([[0, 1, 1],
[1, 0, 1]])
array([[ 1, 1],
[ 1, 1],
[-1, 1]])
Dot Product
Here’s a list of operations that can be performed using Numpy
Operation | Description | Example |
---|---|---|
Array Creation | Creating a NumPy array. | arr = np.array([1, 2, 3, 4, 5]) |
Element-Wise Arithmetic | Element-wise addition, subtraction, and so on. | result = arr1 + arr2 |
Scalar Arithmetic | Scalar addition, subtraction, and so on. | result = arr * 2 |
Element-Wise Functions | Applying functions to each element. | result = np.sqrt(arr) |
Sum and Mean | Calculating the sum and mean of an array.Calculating the sum and mean of an array. | total = np.sum(arr)<br>average = np.mean(arr) |
Maximum and Minimum Values | Finding the maximum and minimum values. | max_val = np.max(arr)<br>min_val = np.min(arr) |
Reshaping | Changing the shape of an array. | reshaped_arr = arr.reshape(2, 3) |
Transposition | Transposing a multi-dimensional array. | transposed_arr = arr.T |
Matrix Multiplication | Performing matrix multiplication. | result = np.dot(matrix1, matrix2) |