<jupyter_start><jupyter_text>Numpy[numpy的功能](https://www.numpy.org/devdocs/user/quickstart.html)  引入Numpy的目的- 为何不直接用list？    - 慢    - 缺少数学操作<jupyter_code>heights = [1.73, 1.68, 1.71, 1.89, 1.79] # 5人的身高
weights = [65.4, 59.2, 63.6, 88.4, 68.7] # 5人的体重
bmis = weights / heights ** 2            # 却不能直接计算5人的BMI
import numpy as np 
np_heights = np.array(heights)        # array，向量，通常只包含数值
np_weights = np.array(weights)
bmis = np_weights / np_heights ** 2   # 这里计算的单元是一个向量
bmis<jupyter_output><empty_output><jupyter_text>更新BMI公式，现在是对向量做计算\begin{equation}\vec{BMIS} = \frac{\vec{weights}}{\vec{heights}^{2}}\end{equation}<jupyter_code>bmis > 21  # 符合直觉的结果，
round(bmis) # 只有numpy.ndarray支持的方法
np.round(bmis) # 需要使用np重载的方法
np.max(bmis) == max(bmis) # 大胆地猜想这两种方法应该都可以
help(np.ndarray)  # 文档即教程<jupyter_output>Help on class ndarray in module numpy:

class ndarray(builtins.object)
 |  ndarray(shape, dtype=float, buffer=None, offset=0,
 |          strides=None, order=None)
 |  
 |  An array object represents a multidimensional, homogeneous array
 |  of fixed-size items.  An associated data-type object describes the
 |  format of each element in the array (its byte-order, how many bytes it
 |  occupies in memory, whether it is an integer, a floating point number,
 |  or something else, etc.)
 |  
 |  Arrays should be constructed using `array`, `zeros` or `empty` (refer
 |  to the See Also section below).  The parameters given here refer to
 |  a low-level method (`ndarray(...)`) for instantiating an array.
 |  
 |  For more information, refer to the `numpy` module and examine the
 |  methods and attributes of an array.
 |  
 |  Parameters
 |  ----------
 |  (for the __new__ method; see Notes below)
 |  
 |  shape : tuple of ints
 |      Shape of created array.
 |  dtype : data-type, optional
 |[...]<jupyter_text>关于Numpy类型<jupyter_code>type(np_weights)                   # 应该返回numpy的类型
mixed_list = [1.0, "is", True] # 通用类型的list
type(np.array(mixed_list))     # 转化为numpy array
np.array(mixed_list)               # 数据类型是字符串<jupyter_output><empty_output><jupyter_text>深入了解numpy数据类型[dtype](https://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html)  快速检索开发文档>具有本地索引的应用程序，Jupyter的help菜单速度不够快- [Dash](https://kapeli.com/dash)- [velocity](http://velocity.silverlakesoftware.com/)  数理统计和线性代数基础  数理统计的例子- 在大样本数据上计算身高体重指数- 把前面5人的例子扩大1000倍- 随机产生一个正态分布 > [正态分布生成](https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.normal.html?highlight=random%20normalnumpy.random.normal)文档![文档链接](http://bazhou.blob.core.windows.net/learning/mpp/npnormal_doc.png) ![正态分布生成文档快照](http://bazhou.blob.core.windows.net/learning/mpp/npnormal.png)  中国成年人身高，体重均值和标准差的参考值 （2015）|性别|东北华北|西北|东南|华中|华南|西南||-|-|-|-|-|-|-||身高，体重|均值，标准差|均值，标准差|均值，标准差|均值，标准差|均值，标准差|均值，标准差||男(mm)|1693, 56.6|1684, 53.7|1686, 55,2|1669, 56.3|1650, 57.1|1647, 56.7||女(mm)|1586, 51.8|1575, 51.9|1575, 50.8|1560, 50.7|1549, 49.7|1546, 53.9||男(kg)|64, 8.2|60, 7.6|59, 7.7|57, 6.9|56, 6.9|55, 6.8||女(kg)|55, 7.1|52, 7.1|51, 7.2|50, 6.8|49, 6.5|50, 6.9|> 选择东南地区成年男性数据：身高：1686, 55.2，体重：59, 7.7<jupyter_code>height_5k = np.random.normal(1.686, 0.0552, 5000)   # 5000身高数据
weight_5k = np.random.normal(59, 7.7, 5000)         # 5000体重数据
shmale_5k = np.column_stack((height_5k, weight_5k)) # 5000上海男性数据
shmale_5k                                           # 随机产生一些瘦子和胖子
shmale_weight = shmale_5k[:,1]
shmale_height = shmale_5k[:,0]
shmale_height_mean = np.mean(shmale_height)  # 身高均值
shmale_height_std = np.std(shmale_height)    # 身高标准差
shmale_weight_mean = np.mean(shmale_weight)  # 体重均值
shmale_weight_std = np.std(shmale_weight)    # 体重标准差
from tabulate import tabulate                # 格式化成表格样式
print(tabulate([['身高（米）', shmale_height_mean, shmale_height_std], 
                ['体重（公斤）', shmale_weight_mean, shmale_weight_std]], 
               headers=['上海男','均值', '标准差']))

shmale_bmi = shmale_weight / shmale_height ** 2 # 计算5000身高体重指数
shmale_bmi
print(np.mean(shmale_bmi), np.std(shmale_bmi))  # 上海男性体型分布<jupyter_output>20.854954078674073 3.074664871185173<jupyter_text>线性代数例子- 解方程组\begin{equation}2x + 3y = 8 \end{equation}\begin{equation}5x + 2y = 9 \end{equation}- 用矩阵的形式表示为$$ A = \begin{bmatrix}2 & 3\\5 & 2\end{bmatrix} \;\;\;\; \vec{b} = \begin{bmatrix}8\\9\end{bmatrix}$$- 目标是求向量x$$ A\vec{x}= \vec{b} $$<jupyter_code>a = np.array([[2, 3], [5, 2]]) # 方程组系数矩阵
a.transpose()                  # 转置
b = np.array([8, 9])
print(b.shape, a.shape)         # shape不是函数，是tuple
b.transpose()
# 矩阵的索引
print(tabulate([['0', a[0,0], a[0,1]], 
                ['1', a[1,0], a[1,1]]], 
               headers=['A', '0', '1']))<jupyter_output>A    0    1
---  ---  ---
  0    2    3
  1    5    2<jupyter_text>>[矩阵运算](https://docs.scipy.org/doc/numpy/reference/routines.linalg.html)文档![linalg](http://bazhou.blob.core.windows.net/learning/mpp/linalg.png) ![矩阵求逆](http://bazhou.blob.core.windows.net/learning/mpp/linalg.inv.png)<jupyter_code># 矩阵求逆
from numpy.linalg import inv as inv
a_inv = inv(a)
a_inv<jupyter_output><empty_output><jupyter_text>$$ A^{-1}A=\begin{bmatrix}1 & 0\\0 & 1\end{bmatrix} $$<jupyter_code>np.round(a_inv @ a) # 逆矩阵和原矩阵乘来验证求逆, @代表矩阵乘<jupyter_output><empty_output><jupyter_text>$$ A^{-1}A\vec{x}=A^{-1}\vec{b}$$$$ \begin{bmatrix}1 & 0\\0 & 1\end{bmatrix}\vec{x}= A^{-1}\vec{b}$$$$ \vec{x} = A^{-1}\vec{b}$$<jupyter_code>x = a_inv @ b
x
from numpy.linalg import solve as solve # 引入求解方法
solve(a, b)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1738245094409478, 'fraction_numerical': 0.06053313587560163, 'mean_word_length': 4.0542563143124415, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 35, 'https://': 5, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '54384', 'license': 'Apache-2.0', 'n_tokens_mistral': 2759, 'n_tokens_neox': 2666, 'n_words': 368, 'path': '基础教程/A1-Python与基础知识/Python基础/04_Numpy.ipynb', 'repo_name': 'microsoft/ai-edu', 'size': '5402'}