簡體   English   中英

Matplotlib和Numpy數學

[英]Matplotlib and Numpy Math

我正在嘗試通過Matplotlib和Numpy吸引一些人,但這並不是一件容易的事。

我正在做一個小型項目,開始處理Matplotlib和Numpy,但我遇到了麻煩...

這是代碼:

# Modules
import datetime
import numpy as np
import matplotlib.finance as finance
import matplotlib.mlab as mlab
import matplotlib.pyplot as plot

# Define quote
startdate = datetime.date(2010,10,1)
today = enddate = datetime.date.today()
ticker = 'uso'

# Catch CSV
fh = finance.fetch_historical_yahoo(ticker, startdate, enddate)

# From CSV to REACARRAY
r = mlab.csv2rec(fh); fh.close()
# Order by Desc
r.sort()


### Methods Begin
def moving_average(x, n, type='simple'):
    """
    compute an n period moving average.

    type is 'simple' | 'exponential'

    """
    x = np.asarray(x)
    if type=='simple':
        weights = np.ones(n)
    else:
        weights = np.exp(np.linspace(-1., 0., n))

    weights /= weights.sum()


    a =  np.convolve(x, weights, mode='full')[:len(x)]
    a[:n] = a[n]
    return a
### Methods End


prices = r.adj_close
dates = r.date
ma20 = moving_average(prices, 20, type='simple')
ma50 = moving_average(prices, 50, type='simple')

# Get when ma20 crosses ma50
equal = np.round(ma20,1)==np.round(ma50,1)
dates_cross  = (dates[equal])
prices_cross = (prices[equal])

# Get when ma20 > ma50
ma20_greater_than_ma50 = np.round(ma20,1) > np.round(ma50,1)
dates_ma20_greater_than_ma50  = (dates[ma20_greater_than_ma50])
prices_ma20_greater_than_ma50 = (prices[ma20_greater_than_ma50])

print dates_ma20_greater_than_ma50
print prices_ma20_greater_than_ma50

現在,我需要執行以下操作:

store the price of the "price_cross"
see if one day after the "ma20_greater_than_ma50" statment is true, if true store the price as "price of the one day after"
now do "next price_cross" - "price of the one day after"  (price2 - price1) for all occurences

我該怎么做數學更重要。 如何通過Matplotlib和Numpy獲得牽引力。 我應該買什么書?

給我一些線索。

最好的祝福,

我同意Josh,但想添加matplotlib畫廊:

http://matplotlib.sourceforge.net/gallery.html

我的大多數情節都是從直接復制接近我想要的內容開始,然后對其進行修改以滿足我的需要。 matplotlib畫廊有很多這樣的例子。

我要說的是,您不一定需要外出購買任何書籍。 更好(更便宜)的解決方案是看一下在線教程,例如:

http://www.scipy.org/Tentative_NumPy_Tutorial

http://matplotlib.sourceforge.net/examples/index.html

並嘗試從文檔中整理內容並搜索相關的關鍵字。 從您提供的代碼(假設您編寫了代碼)中,您已經掌握了numpy。 您需要對遇到的問題更加具體,以獲得更具體/詳細的幫助。

從下面的列表開始,您可能會在瀏覽它們之后找到對您來說最重要的部分:

  1. Python教程http://docs.python.org/tutorial/
  2. 來自http://docs.scipy.org/doc/的 Numpy用戶指南
  3. Matplotlib用戶指南http://matplotlib.sourceforge.net/users/index.html
  4. Numpy / Scipy其他文檔來源http://www.scipy.org/Additional_Documentation

您可能要訂閱numpy和/或matplotlib的郵件列表。

matplotlib和numpy有很多有用的功能,在實現之前,您應該始終先搜索google。

例如,請參見matplotlib movavg函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM