Code Monkey home page Code Monkey logo

dsc-linalg-python-vs-numpy-lab's Introduction

Pure Python vs. Numpy - Lab

Introduction

Numpy, Scipy, and Pandas provide a significant increase in computational efficiency with complex mathematical operations as compared to Python's built-in arithmetic functions. In this lab, you will calculate and compare the processing speed required for calculating a dot product using both basic arithmetic 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 \times 200$ dimensional matrices using:

a) Pure Python (no libraries)

b) Numpy's .dot() method

Create two $200 \times 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 a zeros-filled numpy matrix
  • In Python, calculate the dot product using the formula

$$ \large C_{i,j}= \sum_k A_{i,k}B_{k,j}$$

  • Use Python's timeit library to calculate the processing time
  • Visit this link for an in-depth explanation on how to time a function or routine in Python

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

import timeit

# Start the timer
start = None

# Matrix multiplication in pure Python


time_spent = None

print('Pure Python time:', time_spent, 'sec.')

Numpy

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

# Start the timer
start = None

# Matrix multiplication in numpy


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

Your comments

Summary

In this lab, you performed a quick comparison between calculating a dot product in Numpy vs pure Python. You saw that Numpy is computationally much more efficient than pure Python code because of the sophisticated implementation of Numpy source code. You're encouraged to always perform time tests to fully appreciate the use of an additional library in Python.

dsc-linalg-python-vs-numpy-lab's People

Contributors

shakeelraja avatar loredirick avatar sumedh10 avatar fpolchow 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.