# 扁平化函数ravel()和flatten()

<https://www.cnblogs.com/mzct123/p/8659193.html>

```python
from numpy import *

a = arange(12).reshape(3,4)
print(a)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]
print(a.ravel())
# [ 0  1  2  3  4  5  6  7  8  9 10 11]
print(a.flatten())
# [ 0  1  2  3  4  5  6  7  8  9 10 11]
```

这两个函数实现的功能一样，但我们在平时使用的时候flatten()更为合适。

在使用过程中**flatten()分配了新的内存**，但**ravel()返回的是一个数组的视图**，视图是数组的引用


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/numpy-pandas-matplotlib/numpy/xian-dai/ravel-flatten.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
