Book Image

NumPy 1.5 Beginner's Guide

By : Ivan Idris
Book Image

NumPy 1.5 Beginner's Guide

By: Ivan Idris

Overview of this book

<p>In today's world of science and technology, the hype is all about speed and flexibility. When it comes to scientific computing, NumPy is on the top of the list. NumPy is the fundamental package needed for scientific computing with Python. NumPy will give you both speed and high productivity. Save thousands of dollars on expensive software, while keeping all the flexibility and power of your favourite programming language.<br /><br /><i>NumPy 1.5 Beginner's Guide</i> will teach you about NumPy from scratch. It includes everything from installation, functions, matrices, and modules to testing, all explained with appropriate examples. <br /><br /><i>Numpy 1.5 Beginner's Guide</i> will teach you about installing and using NumPy and related concepts.</p> <p>This book will give you a solid foundation in NumPy arrays and universal functions. At the end of the book, we will explore related scientific computing projects such as Matplotlib for plotting and the SciPy project through examples.</p> <p><i>NumPy 1.5 Beginner's Guide</i> will help you be productive with NumPy and write clean and fast code.</p>
Table of Contents (18 chapters)
NumPy 1.5
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – detecting a trend in QQQ


Often we are more interested in the trend of a data sample than in detrending it. Still we can get the trend back easily after detrending. Let's do that for 1 year of price data for QQQ:

  1. Download quotes : Write code that gets the close price and corresponding dates for QQQ.

    today = date.today()
    start = (today.year - 1, today.month, today.day)
    
    quotes = quotes_historical_yahoo("QQQ", start, today)
    quotes = numpy.array(quotes)
    
    dates = quotes.T[0]
    qqq = quotes.T[4]
  2. Detrend the signal: Detrend the signal.

    y = scipy.signal.detrend(qqq)
  3. Create locators: Create month and day locators for the dates.

    alldays = DayLocator()
    months = MonthLocator()
    
  4. Date formatter: Create a date formatter that creates a string of month name and year.

    month_formatter = DateFormatter("%b %Y")
  5. Figure and subplot: Create a figure and subplot.

    fig = matplotlib.pyplot.figure()
    ax = fig.add_subplot(111)
  6. Data and underlying trend: Plot the data and underlying trend by subtracting the detrended...