# 4.3: Vectors in Python - Mathematics

For those who are new to Python, there are many common mistakes happen in this course. Try to fix the following codes.

## SyntaxError

It means that the code does not make sense in Python. We would like to define a vector with four numbers.

Do This

Fix the following code to creat three vectors with four numbers.

Although you may have been able to get rid of the error messages the answer to you problem may still not be correct. Throughout the semester we will be using a python program calledanswercheckto allow you to check some of your answers. This program doesn’t tell you the right answer but it is intended to be used as a way to get immediate feedback and accelerate learning.

Do This

First we will need to downloadanswercheck.pyto your current working directory. You only really need to do this once. However, if you delete this file by mistake sometime during the semester, you can come back to this notebook and download it again by running the following cell:

Do This

How just run the following command to see if you got (x), (y) and (z) correct when you fixed the code above.

Note

make sure you do not change thecheckanswercommands. The long string with numbers and letters is the secret code that encodes the true answer. This code is also called the HASH. Feel free to look at theanswercheck.pycode and see if you can figure out how it works?

## Numpy

Numpy is a common way to represent vectors, and you are suggested to usenumpyunless otherwise specified. The benefit ofnumpyis that it can perform the linear algebra operations listed in the previous section.

For example, the following code usesnumpy.arrayto define a vector of four elements.

### Scalars versus 1-vectors

In mathematics, 1-vector is considered as a scalar. But in Python, they are not the same.

### Lists of vectors

We have a list of numpy arrays or a list of list. In this case, the vectors can have different dimensions.

Do This

Modify the print statement using indexing to only print the value 3 from thelist_of_vectorsdefined below.

### Indexing

The index of a vector runs from 0 to (n−1) for a (n)-vector.

Do This

The following code tries to get the third element ofx_np, which is the number2.0. Fix this code to provide the correct answer.

Do This

Replace only the third element ofx_npwith the number20.0such that the new values ofx_npis [-1, 0, 20., 3.1]

There is a special index -1, which represents the last element in an array. There are several ways to get more than one consecutive elements.

• x_np[1:3] gives the 2nd and 3rd elements only. It starts with the first index and ends before the second index. So the number of element is just the difference between these two numbers.

• x_np[1:-1] is the same as x_np[1:3] for a 4-vector.

• If you want the last element also, then you do not need to put the index, e.g., x_n[1:] gives all elements except the first one. You can do the same thing as the first one.

Do This

you are given a vector (x_np) of (n) elements, define a new vector (d) of size (n−1) such that ( d_i = x_{i+1} - x_i ) for ( i=1, dots, n-1 ).

Hint try doing this without writing your own loop. You should be able to use simplenumpyindexing as described above.

## Assignment versus copying

Take a look at the following code.

• we create one numpy arrayx_np

• we lety_np = x_np

• we change the third element ofy_np

• The third element ofx_npis also changed

This looks weired and may not make sense for those uses other languages such as MATLAB.

The reason for this is that we are not creating a copy ofx_npand name it asy_np. What we did is that we give a new namey_npto the same arrayx_np. Therefore, if one is changed, and the other one is also changed, because they refer to the same array.

Do This

There is a method namedcopythat can be used to create a new array. You can search how it works and fix the code below. If this is done correctly thex_npvector should stay the same and they_npyou now be[-1 0 2 3.1].

## Vector equality in numpy and list

The relational operator (==,<,>,!=, etc.) can be used to check whether the vectors are same or not. However, they will act differently if the code is comparingnumpy.arrayobjects or alist. Innumpy, Innumpyrelational operators checks the equality for each element in thearray. Forlist, relational operators check all elements.

## Zero vectors and Ones vectors in numpy

• zeros(n) creates a vector with all 0s

• ones(n) creates a vector with all 1s

Do This

Create a zero vector (calledzero_np) with the same dimension as vectorx_np. Create a ones vector (calledones+np) also with the same dimension as vectorx_np.

## Random vectors

• random.random(n) creates a random vector with dimension (n).

In this section, you will understand why we use numpy for linear algebra opeartions. If x and y are numpy arrays of the same size, we can have x + y and x-y for their addition and subtraction, respectively.

For comparison, we also put the addition of two lists below. Recall from the pre-class assignment, we have to define a function to add two lists for linear algebra.

Do This

Modify the following code to properly add and subtract the two lists.

HINT it is perfectly okay NOT to write your own function try you should be able to cast the lists as arrays:

A scalar-vector addition means that the scalar (or a 1-vector) is added to all elements of the vector.

Do This

Add a scalar 20.20 to all elements of the following vectorx_npand store teh result back intox_np

## Scalar-vector multiplication and division

Whenais a scalar andxisnumpyarray. We can express the scalar-vector multiplication asa*xorx*a.

We can also do scalar-vector division forx/aora/x. (note thatx/aanda/xare different)

Do This

Divide all elements of the following vectorx_npby20.20and put it intoy_np

## Element-wise operations

As stated above relational operations onnumpyarrays are performed element-wise. Examples we mentioned before are

• The==operator

• The addition+and subtraction-

Note

for this to work the two vectors have to be the same dimensions.

If they are not have the same dimension, such as a scalar and a vector, we can think about expanding the scalar to have the same dimension as the vector and perform the operations. For example.

• Vector-scalar multiplication and division

Do This

Assume that you invested three assets with initial values stored inp_initial, and after one week, their values are stored inp_final. Then what are the asset return ratio (r) for these three assets (i.e. price change over the initial value).

## Linear combination

We have two vectors (x) and (y) we can get the linear combination of these two vectors as (ax+by) where (a) and (b) are scalar coefficients.

In the following example, we are given two vectors (x_npandy_np), and two scalars (alphaandbeta), we obtain the linear combinationalpha*x_np + beta*y_np.

We can also define a functionlincombto performn the linear combination.

Do This

Finish the following code for lincomb and compare the results we just get.

We can also test the functions ourselves by using values for which we know the answer. For example, the following tests are multiplying and adding by zero we know what these answers should be and can check them.

If you want to check that all values in anumpy.arrayare the same you could convert it to a list or there is a method calledalltruewhich checks if everything is true. It is a good idea to use this method if vectors get big.

Similar to 3D points, 3D vectors are stored as Vector3d structures. They can be thought as a zero-based, one-dimensional list that contain three numbers. These three number represent to the X, Y and Z coordinate direction of the vector.

Here is an easy way to construct a vector:

A Vector3d’s coordinates can be accessed as a list, one element at a time:

The coordinates of a Vector3d may also be accessed through its .X , .Y and .Z properties:

To change the individual coordinate of a Vector3d, simply assign a new value to the coordinate through the index location or coordinate property:

To find the vector between two points, use vector subtraction:

In the above example, the vector goes from point1 to point2. Reversing this direction is a common mistake. It is importatnt to be sure that the starting point is subtraced from the ending point.

Vectors can also be added to points to create new point locations. Here is an example of moving a point location by a vector:

Use a for loop to walk through each coordinate in succession:

RhinoScriptSyntax contains a number of methods to manipulate vectors. See RhinoScript Points and Vectors Methods for details.

## 2.1 Scalars, Vectors, Matrices and Tensors

$s =egin x_1 x_2 cdots x_n end$

• scalars are written in lowercase and italics. For instance: $n$
• vectors are written in lowercase, italics and bold type. For instance: $s$
• matrices are written in uppercase, italics and bold. For instance: $s$

### Example 1.

#### Create a vector with Python and Numpy

Coding tip: Unlike the matrix() function which necessarily creates $2$-dimensional matrices, you can create $n$-dimensionnal arrays with the array() function. The main advantage to use matrix() is the useful methods (conjugate transpose, inverse, matrix operations…). We will use the array() function in this series.

We will start by creating a vector. This is just a $1$-dimensional array:

### Example 2.

#### Create a (3x2) matrix with nested brackets

The array() function can also create $2$-dimensional arrays with nested brackets:

### Shape

The shape of an array (that is to say its dimensions) tells you the number of values for each dimension. For a $2$-dimensional array it will give you the number of rows and the number of columns. Let’s find the shape of our preceding $2$-dimensional array A . Since A is a Numpy array (it was created with the array() function) you can access its shape with:

Let’s check the shape of our first vector:

As expected, you can see that $s$ has only one dimension. The number corresponds to the length of the array:

## Installation

There are multiple ways to create our vector instances using the vectors module.

We can first initialize some vectors and points calling their repsective class contructors as follows.

We can also create a Point instance or a Vector instance with a list using the class method from_list().

We can also create our Vectors from two Point instances using the classmethod from_points().

We can also get access to the vector array to use it with other libraries.

## What is a Vector?

According to Google, a Vector is a quantity having direction as well as magnitude, especially as determining the position of one point in space relative to another.

Vectors are very important in Machine Learning as they not just describe magnitude but also the direction of the features. We can create a vector in NumPy with following code snippet:

row_vector = np.array ( [ 1 , 2 , 3 ] )
print ( row_vector )

In the above code snippet, we created a row vector. We can also create a column vector as:

### Making a Matrix

A matrix can be simply understood as a two-dimensional array. We can make a matrix with NumPy by making a multi-dimensional array:

Although matrix is exactly similar to multi-dimensional array, the matrix data structure is not recommended due to two reasons:

1. The array is the standard when it comes to the NumPy package
2. Most of the operations with NumPy returns arrays and not a matrix

### Using a Sparse Matrix

To remind, a sparse matrix is the one in which most of the items are zero. Now, a common scenario in data processing and machine learning is processing matrices in which most of the elements are zero. For example, consider a matrix whose rows describe every video on Youtube and columns represents each registered user. Each value represents if the user has watched a video or not. Of course, majority of the values in this matrix will be zero. The advantage with sparse matrix is that it doesn’t store the values which are zero. This results in a huge computational advantage and storage optimisation as well.

Let’s create a spark matrix here:

original_matrix = np.array ( [ [ 1 , 0 , 3 ] , [ 0 , 0 , 6 ] , [ 7 , 0 , 0 ] ] )
sparse_matrix = sparse.csr_matrix ( original_matrix )
print ( sparse_matrix )

To understand how the code works, we will look at the output here:

In the above code, we used a NumPy’s function to create a Compressed sparse row matrix where non-zero elements are represented using the zero-based indexes. There are various kinds of sparse matrix, like:

We won’t be diving into other sparse matrices here but know that each of their is use is specific and no one can be termed as ‘best’.

### Applying Operations to all Vector elements

It is a common scenario when we need to apply a common operation to multiple vector elements. This can be done by defining a lambda and then vectorizing the same. Let’s see some code snippet for the same:

mul_5 = lambda x: x * 5
vectorized_mul_5 = np.vectorize ( mul_5 )

To understand how the code works, we will look at the output here:

In the above code snippet, we used vectorize function which is part of the NumPy library, to transform a simple lambda definition into a function which can process each and every element of the vector. It is important to note that vectorize is just a loop over the elements and it has no effect on the performance of the program. NumPy also allows broadcasting, which means that instead of the above complex code, we could have simply done:

And the result would have been exactly the same. I wanted to show the complex part first, otherwise you would have skipped the section!

### Mean, Variance and Standard Deviation

With NumPy, it is easy to perform operations related to descriptive statistics on vectors. Mean of a vector can be calculated as:

Variance of a vector can be calculated as:

Standard deviation of a vector can be calculated as:

The output of the above commands on the given matrix is given here:

### Transposing a Matrix

Transposing is a very common operation which you will hear about whenever you are surrounded by matrices. Transposing is just a way to swap columnar and row values of a matrix. Please note that a vector cannot be transposed as a vector is just a collection of values without those values being categorised into rows and columns. Please note that converting a row vector to a column vector is not transposing (based on the definitions of linear algebra, which is outside the scope of this lesson).

For now, we will find peace just by transposing a matrix. It is very simple to access the transpose of a matrix with NumPy:

The output of the above command on the given matrix is given here:

Same operation can be performed on a row vector to convert it to a column vector.

### Flattening a Matrix

We can convert a matrix into a one-dimensional array if we wish to process its elements in a linear fashion. This can be done with the following code snippet:

The output of the above command on the given matrix is given here:

Note that the flatten matrix is a one-dimensional array, simply linear in fashion.

### Calculating Eigenvalues and Eigenvectors

Eigenvectors are very commonly used in Machine Learning packages. So, when a linear transformation function is presented as a matrix, then X, Eigenvectors are the vectors that change only in scale of the vector but not its direction. We can say that:

Here, X is the square matrix and γ contains the Eigenvalues. Also, v contains the Eigenvectors. With NumPy, it is easy to calculate Eigenvalues and Eigenvectors. Here is the code snippet where we demonstrate the same:

The output of the above command on the given matrix is given here:

### Dot Products of Vectors

Dot Products of Vectors is a way of multiplying 2 vectors. It tells you about how much of the vectors are in the same direction, as opposed to the cross product which tells you the opposite, how little the vectors are in the same direction (called orthogonal). We can calculate the dot product of two vectors as given in the code snippet here:

a = np.array ( [ 3 , 5 , 6 ] )
b = np.array ( [ 23 , 15 , 1 ] )

The output of the above command on the given arrays is given here:

### Adding, Subtracting and Multiplying Matrices

Adding and Subtracting multiple matrices is quite straightforward operation in matrices. There are two ways in which this can be done. Let’s look at the code snippet to perform these operations. For the purpose of keeping this simple, we will use the same matrix twice:

Next, two matrices can be subtracted as:

The output of the above command on the given matrix is given here:

As expected, each of the elements in the matrix is added/subtracted with the corresponding element. Multiplying a matrix is similar to finding the dot product as we did earlier:

The above code will find the true multiplication value of two matrices, given as:

The output of the above command on the given matrix is given here:

#### Conclusion

In this lesson, we went through a lot of mathematical operations related to Vectors, Matrices and Arrays which are commonly used Data processing, descriptive statistics and data science. This was a quick lesson covering only the most common and most important sections of the wide variety of concepts but these operations should give a very good idea about what all operations can be performed while dealing with these data structures.

#### Shubham Aggarwal

I’m a Java EE Engineer with about 4 years of experience in building quality products. I have excellent problem-solving skills in Spring Boot, Hibernate ORM, AWS, Git, Python and I am an emerging Data Scientist.

## Creating Vectors in Python Tutorial

Vectors are a useful way of representing data in python. The better way to create vectors is by using the popular NumPy library. It also includes all the vector operations like addition, subtractions, dot product, etc. However, we will be using inbuilt methods to create vectors in this article. Python has no dedicated module or a method for creating vectors, so we will be using the type alias feature to accomplish it. This also means we will have to write all the functions for addition, subtraction, dot product, finding magnitude, and distance (between two vectors) ourselves. Using this method will be slow from a computational standpoint. This is only to understand the concept level thinking that goes into creating vectors.

#### Vectors

A vector is a quantity that has a tail (starting point), a head (ending point), a direction, and magnitude. A direction and magnitude are enough to locate their path but having a tail and head can help us mark them at the exact location. One can perform operations like vector addition, subtraction, dot product, etc to arrive at necessary results. The below image is a simple illustration of a vector.

The above vector is in a 2D plane. We can however represent vectors in any number of dimensions, except 1D.

#### Representing our data in vector format

One might be thinking what is the use of representing our data in vector format. The use is simple, with vector we can conduct vector operations. We can also multiply them with numbers (scalars). For example, if a person is defined by his height, weight, and age then we can represent him with a 3D vector [height, weight, age] .

This vector will be standard for representing any person. All the persons will fit inside the vector space. We will be utilizing lists to write vectors in python. We will be defining our own functions to conduct vector operations. Before going forward, however, we need to learn two features of python which we will be using in this tutorial. They are type alias and assert keyword.

#### Typing

Python is a dynamically typed programming language. This means we don’t need to specify the type of our variables, arguments, etc. However, we can (since python 3.5) provide type but only as a hint. It will never be enforced by python however we can use third-party checkers like mypy to check if our code is correct. This is useful if we want to be careful about the types we use in our code. Here is a simple example.

In the above code, we are hinting that the argument of the function greeting should be a string. However, it’s not enforced by python. We can still pass any data type we want. It is best used with third-party checkers like mypy. This has many pros including telling our co-developers much-needed information. This is called type hinting. Talking in detail about typing in python is beyond the scope of this article but one can read the official documentation here.

#### Type alias

A type alias is simply creating an alias to a defined type. For example, we are going to be learning vectors in this article. We know that a vector is a list of numbers, preferably floating-point numbers. Hence we assign a list of floating-point numbers as a vector.

From now onwards, we can type-hint vectors using Vector . Third-party checkers will also take note of it.

#### Assert

The assert keyword is used to test our code. It is used for debugging. This is best explained through an example.

Here, we have defined a function addition that takes in two values num1 and num2 . It returns num1 + num2 . We can use assert to check if our function is working as intended. We know the sum of 1 and 2 should be 3. When we run the code, the assert keyword runs the addition function with 1 and 2 as arguments. Then it will check if the returned value is equal to 3. If it is, the code will work just fine. If it isn’t, we will get an assertion error “not working”. This is a useful way of testing our code. We will be using assert in this tutorial because we want to be sure the functions we write are correct. It will be otherwise hard to detect from the returned value.

#### Creating a vector

We already see how we used type alias to create the vector type. Now let’s actually create a vector.

Was that too simple? Well, that’s all it takes. We have defined a list of floats as a vector. So, we have successfully created our first vector. What we really need to do now is creating all the functions for this vector so we can actually do vector operations. Let’s start with vector addition.

While adding two or more vectors, we add the corresponding elements. If the vectors have uneven length, addition is not possible.

See how we used Vector as a type hint while defining our function. We used assert to check if our vectors are of the same length. The zip() creates a new list containing tuples of values having the same index in list/vector v and w . Each tuple in this new list is accessed and their values are summed. One can read about list comprehensions in our pythonic programming tutorial.

#### Vector subtraction

The code will be similar to vector addition. Instead of adding each element, we subtract each element.

#### Scalar multiplication

Scalar multiplication can be achieved by simply multiplying the vector with a number. It is a simple function.

#### Dot product

For finding the dot product of two vectors, we find the sum of the products of each corresponding elements. The vectors must be of equal length.

The sum() accepts a list of values and returns their sum. The list comprehension v_i * w_i for v_i, w_i in zip(v, w) creates a list of the products of each corresponding elements.

#### Finding magnitude

A magnitude of a vector is the length of the vector. It is found out by taking the square root of the sum of squares of each direction. To find the sum of squares, all we need to do is find the dot product of a vector with itself. We already have the function for finding the dot product above. To find the sum of squares, just pass the same vector as both the arguments.

Now we need to find the square root to compute magnitude.

#### Finding the distance between two vectors

Distance between the two vectors is the square root of the sum of squares of differences of the corresponding elements. We already have a magnitude function that finds the square root of the sum of squares. All we have to do is to use vector_subtrataction function to find the difference of corresponding elements and pass it as argument to the magnitude function.

#### Conclusion

In this tutorial, we briefly learned typing, type alias, and assert feature. We learned to utilize them for creating vectors in python. We also created functions for calculating vector addition, subtraction, scalar multiplication, dot product, magnitude, and for finding distance between two vectors.

We recommend using a third-party library like NumPy in your actual data science project. The library has built-in methods for defining vectors and conducting operations on them. However, we hope this tutorial helped you gain some clarity on behind the scenes working of vectors and vector operations.

Unit Vectors are the vectors with the same directions as a normal vector but their magnitude equals 1. These vectors are extremely important for carrying out multiple operations in 3d space. As there is no available method to convert the vector into normal form, we’ll have to use the sum() method of the numpy array.

The output of the above vector scalar multiplication in python is :

### Explanation:

Firstly, we import the necessary classes and initialize a dummy array x. This array has a magnitude not equal to 1. To convert it to 1, we first find its magnitude and divide it. (x**2).sum()**0.5 is used to find the magnitude of vector x.

## Boolean Indexing

Let’s consider an example where we have some data in an array and an array of names with duplicates. I’m going to use here the randn function in numpy.random to generate some random normally distributed data:

Figure 4-2. Two-dimensional array slicing

Suppose each name corresponds to a row in the data array and we wanted to select all the rows with corresponding name 'Bob' . Like arithmetic operations, comparisons (such as == ) with arrays are also vectorized. Thus, comparing names with the string 'Bob' yields a boolean array:

This boolean array can be passed when indexing the array:

The boolean array must be of the same length as the axis it’s indexing. You can even mix and match boolean arrays with slices or integers (or sequences of integers, more on this later):

To select everything but 'Bob' , you can either use != or negate the condition using - :

Selecting two of the three names to combine multiple boolean conditions, use boolean arithmetic operators like & (and) and | (or):

Selecting data from an array by boolean indexing always creates a copy of the data, even if the returned array is unchanged.

### Caution

The Python keywords and and or do not work with boolean arrays.

Setting values with boolean arrays works in a common-sense way. To set all of the negative values in data to 0 we need only do:

Setting whole rows or columns using a 1D boolean array is also easy:

## Transposing a matrix(2D array) and coding them

Transposing a matrix is becomes a very important topic as it further contributes to calculating the inverse of a matrix and other data manipulation where you want to mirror the axes of your dataframe.

Transposing a matrix converts a row vector to a column vector and vice-versa, it is denoted by the superscript T:

Here is what a square matrix(identical number of rows and columns) looks like after transposition with interchanged axes:

And if the matrix is not square, here is how the shape of the matrix changes:

## Vector Math

Objects in computational designs are rarely created explicitly in their final position and form, and are most often translated,rotated, and otherwise positioned based off of existing geometry. Vector math serves as a kind-of geometric scaffolding to give direction and orientation to geometry, as well as to conceptualize movements through 3D space without visual representation.

At its most basic, a vector represents a position in 3D space, and is often times thought of as the endpoint of an arrow from the position (0, 0, 0) to that position. Vectors can be created with the ByCoordinates constructor, taking the x, y, and z position of the newly created Vector object. Note that Vector objects are not geometric objects, and don’t appear in the Dynamo window. However, information about a newly created or modified vector can be printed in the console window:

A set of mathematical operations are defined on Vector objects, allowing you to add, subtract, multiply, and otherwise move objects in 3D space as you would move real numbers in 1D space on a number line.

Vector addition is defined as the sum of the components of two vectors, and can be thought of as the resulting vector if the two component vector arrows are placed “tip to tail.” Vector addition is performed with the Add method, and is represented by the diagram on the left.

Similarly, two Vector objects can be subtracted from each other with the Subtract method. Vector subtraction can be thought of as the direction from first vector to the second vector.

Vector multiplication can be thought of as moving the endpoint of a vector in its own direction by a given scale factor.

Often it’s desired when scaling a vector to have the resulting vector’s length exactly equal to the scaled amount. This is easily achieved by first normalizing a vector, in other words setting the vector’s length exactly equal to one.

c still points in the same direction as a (1, 2, 3), though now it has length exactly equal to 5.

Two additional methods exist in vector math which don’t have clear parallels with 1D math, the cross product and dot product. The cross product is a means of generating a Vector which is orthogonal (at 90 degrees to) to two existing Vectors. For example, the cross product of the x and y axes is the z axis, though the two input Vectors don’t need to be orthogonal to each other. A cross product vector is calculated with the Cross method.

An additional, though somewhat more advanced function of vector math is the dot product. The dot product between two vectors is a real number (not a Vector object) that relates to, but is not exactly, the angle between two vectors. One useful properties of the dot product is that the dot product between two vectors will be 0 if and only if they are perpendicular. The dot product is calculated with the Dot method.