Code Monkey home page Code Monkey logo

dsc-2-13-11-linalg-python-vs-numpy-lab-online-ds-sp-000's Introduction

Pure Python vs. Numpy - Lab

Introduction

Numpy, Scipy and Pandas libraries provide a significant increase in computational efficiency with complex mathematical operations as compared to Python's built in arithmatic functions. In this lab we shall calculate and compare the processing speed required for calculating a dot product both using basic arithmatic operations in Python and Numpy's .dot() method.

Objectives

You will be able to:

  • Compare the performance of high dimensional matrix operations in Numpy vs. pure Python

Problem

Write a routine to calculate the dot product between two 200 x 200 dimensional matrices using:

a) Pure Python

b) Numpy's .dot()

Create two 200 x 200 matrices in Python and fill them with random values using np.random.rand()

# Compare 200x200 matrix-matrix multiplication speed
import numpy as np

# Set up the variables
A = None
B = None

Pure Python

  • Initialize an zeros filled numpy matrix with necessary rows and columns for storing result.
  • In Python Calculate the dot product using the formula
  • Use Python's timeit library to calculate the processing time.
  • Visit this link for an indepth explanation on how to time a function or routine in python.

Hint: Use nested for loop for accessing, calculating and storing each scalar value in the result matrix.

import timeit

# Start the timer
start = None

# Matrix multiplication in pure Python


time_spent = None

print('Pure Python Time:', time_spent, 'sec.')
Pure Python Time: None sec.

Numpy

Set the timer and calculate the time taken by .dot() function for multiplying A and B

# start the timer
start = None

# Matrix multiplication in numpy


time_spent = None
print('Numpy Time:', time_spent, 'sec.')
Numpy Time: None sec.

Your comments

Summary

In this lab, we performed a quick comparison between calculating a dot product in numpy vs python built in function. We saw that Numpy is computationally much more efficient that Python code due to highly sophisticated implementation of Numpy source code. You are encouraged to always perform such tests to fully appreciate the use of an additional library in Python.

dsc-2-13-11-linalg-python-vs-numpy-lab-online-ds-sp-000's People

Contributors

shakeelraja avatar loredirick avatar

Watchers

James Cloos 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.