# 开源框架#

## NumPy#

Fundamental matrix and array processing capabilities are provided by the excellent NumPy library.

For example, let’s build some arrays

import numpy as np                     # Load the library

a = np.linspace(-np.pi, np.pi, 100)    # Create even grid from -π to π
b = np.cos(a)                          # Apply cosine to each element of a
c = np.sin(a)                          # Apply sin to each element of a


Now let’s take the inner product

b @ c

3.885780586188048e-16


The number you see here might vary slightly due to floating point arithmetic but it’s essentially zero.

As with other standard NumPy operations, this inner product calls into highly optimized machine code.

It is as efficient as carefully hand-coded FORTRAN or C.

## SciPy#

The SciPy library is built on top of NumPy and provides additional functionality.

For example, let’s calculate $$\int_{-2}^2 \phi(z) dz$$ where $$\phi$$ is the standard normal density.

from scipy.stats import norm
from scipy.integrate import quad

ϕ = norm()
value, error = quad(ϕ.pdf, -2, 2)  # Integrate using Gaussian quadrature
value

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In , line 1
----> 1 from scipy.stats import norm
2 from scipy.integrate import quad
4 ϕ = norm()

ModuleNotFoundError: No module named 'scipy'


SciPy includes many of the standard routines used in

See them all here.

## Graphics#

The most popular and comprehensive Python library for creating figures and graphs is Matplotlib, with functionality including

• plots, histograms, contour images, 3D graphs, bar charts etc.

• output in many formats (PDF, PNG, EPS, etc.)

• LaTeX integration

Example 2D plot with embedded LaTeX annotations

Example contour plot

Example 3D plot

More examples can be found in the Matplotlib thumbnail gallery.

Other graphics libraries include

## Symbolic Algebra#

It’s useful to be able to manipulate symbolic expressions, as in Mathematica or Maple.

The SymPy library provides this functionality from within the Python shell.

from sympy import Symbol

x, y = Symbol('x'), Symbol('y')  # Treat 'x' and 'y' as algebraic symbols
x + x + x + y


We can manipulate expressions

expression = (x + y)**2
expression.expand()


solve polynomials

from sympy import solve

solve(x**2 + x + 2)


and calculate limits, derivatives and integrals

from sympy import limit, sin, diff

limit(1 / x, x, 0)

limit(sin(x) / x, x, 0)

diff(sin(x), x)


The beauty of importing this functionality into Python is that we are working within a fully fledged programming language.

We can easily create tables of derivatives, generate LaTeX output, add that output to figures and so on.

## Pandas#

One of the most popular libraries for working with data is pandas.

Pandas is fast, efficient, flexible and well designed.

Here’s a simple example, using some dummy data generated with Numpy’s excellent random functionality.

import pandas as pd
np.random.seed(1234)

data = np.random.randn(5, 2)  # 5x2 matrix of N(0, 1) random draws
dates = pd.date_range('28/12/2010', periods=5)

df = pd.DataFrame(data, columns=('price', 'weight'), index=dates)
print(df)

df.mean()


## AKShare#

AKShare 是一款基于 Python 的开源财经数据接口库，帮助大家方便的获取财经数据，参见 AKShare

import akshare as ak
ak.__version__

stock_zh_a_hist_df = ak.stock_zh_a_hist(symbol="000001")
print(stock_zh_a_hist_df)

stock_zh_a_hist_df.info()