Giter Site home page Giter Site logo

dsc-2-13-07-linalg-vector-addition-codealong-online-ds-sp-000's Introduction

Vector Addition and Broadcasting in Numpy - Code Along

Introduction

This lesson is a supplement to the previous lesson where we learnt how to create numpy arrays as vectors or matrices and performed basic manipulations on these objects. In this lesson we shall look at matrix addition and broadcasting features offered by Numpy.

Objectives

You will be able to:

  • Understand and implement vector addition in Numpy
  • Understand how broadcasting differs from addition when adding mismatched dimensions.

Vector Addition

We shall now look at simple vector addition, where all operations are performed element-wise between two vectors/matrices of equal size to result in a new vector/matrix with the same size.

Two arrays A and B with the same dimensions can be added together if:

  • they have the same shape:
  • each cell of A is added to the corresponding cell of B:

A i,j + B i,j = C i,j

here A(i,j) and B(i,j) represent row and column locations. This is a more standard notation that you will find in most literature. Another visual representation of this process can be shown as :

1 dimensional arrays can be added together in exactly the same way following similar assumptions. The addition of two vectors x and y may be represented graphically by placing the start of the arrow y at the tip of the arrow x, and then drawing an arrow from the start (tail) of x to the tip (head) of y. The new arrow drawn represents the vector x + y

We can perform addition operations in Numpy as:

import numpy as np

# Adding 1-D arrays
a = np.array([1,2,3])
b = np.array ([4,5,6]) 
c=a+b
c
# Code here 

Subtracting a vector is the same as adding its negative. So, the difference of the vectors x and y is equal to the sum of x and -y:

x - y = x + (-y)

Geometrically, when we subtract y from x, we place the end points of x and y at the same point, and then draw an arrow from the tip of y to the tip of x. That arrow represents the vector x - y.

Mathematically, we subtract the corresponding components of vector y from the vector x.

# Subtracting 1-D arrays
a = np.array([1,2,3])
b = np.array ([4,5,6]) 
c=b-a
c
# Code here

Now lets try addition with matrices.

# Adding 2-D matrices
A = np.array([[1, 2], [3, 4], [5, 6]])
B = np.array([[1, 4], [2, 5], [2, 3]])
# Add matrices A and B
C = A + B
C
# Code here 
# Add matrices with mismatched dimensions
A = np.array([[1, 2], [3, 4], [5, 6]])
B = np.array([[1, 4], [2, 5]])
# Add matrices A and B
C = A + B
C
# Code here 

Yes, an error, as expected due to dimension mismatch. Here it seems very intuitive to know why this happened , but when working with large matrices and tensors, shape mismatch could become a real problem and as data scientists, we must be sure about what dimensions our datasets carry.

Vector Scalar Addition

Scalar values can be added to matrices and vectors. In this case, the scalar value is added to each element of array as shown below:

# Add scalars to arrays
# Add a scalar to a 1-D vector
print(a+4)
# Add a scalar to a 2-D matrix
print(A+4)
# Code here 

Broadcasting

Numpy can also handle operations on arrays of different shapes as some machine learning algorithms need that. The smaller array gets extended to match the shape of the larger array. In the scalar-vector addition, we used broadcasting so the scalar was converted in an array of same shape as A.

Let's see this is action while trying to add arrays with different shapes

A = np.array([[1, 2], [3, 4], [5, 6]])
print(A)
B = np.array([[2], [4], [6]])
print(B)
A+B
# Code here 

Summary

In this lesson, we saw how to add vectors and matrices and also looked at the dimension match assumption necessary for this addition. We also looked at how numpy allows you to use broadcasting to add scalars and vector/matrices to other objects with different dimensions. In the following lessons, we shall look at more complicated mathematical operations and their use in real life data analysis.

dsc-2-13-07-linalg-vector-addition-codealong-online-ds-sp-000's People

Contributors

shakeelraja avatar loredirick avatar

Watchers

 avatar James Cloos avatar Kevin McAlear avatar  avatar Victoria Thevenot avatar Belinda Black avatar  avatar Joe Cardarelli avatar Sam Birk avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Jaichitra (JC) Balakrishnan avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar A. Perez avatar Nicole Kroese  avatar  avatar  avatar Nicolas Marcora avatar Lisa Jiang avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.