Numpy 入门(一)Basic visualization & Indexing and slicing
本帖最后由 hj170520 于 2020-6-9 10:21 编辑入门代码(一)
建议输入jupyter notebook 使用
# 1. Basic visualization
```
%matplotlib inline
import matplotlib.pyplot as plt # the tidy way
import pandas as pd
import numpy as np
```
### 1D plotting:
```
x = np.linspace(0, 3, 20)
y = np.linspace(0, 9, 20)
plt.plot(x, y) # line plot
plt.plot(x, y, 'o') # dot plot
plt.show() # <-- shows the plot (not needed with interactive plots)
```
### 2D plotting (such as images):
```
image = np.random.rand(30, 30)
plt.imshow(image, cmap=plt.cm.hot)
plt.colorbar()
plt.show() # <-- shows the plot (not needed with interactive plots)
```
# 2. Indexing and slicing
### 1D array
```
a = np.arange(10)
a
a, a, a[-1]
a[::-1] # <-- reversing a sequence
a #
a[:4] # <-- start from 1st number to 5th number
a# <-- start from 2st number to 4th number
a[::2]# <-- start from 1st number(default) in 2 steps
a# <-- start from 4st number in 1 steps(default)
```
### 2D arrays (1)
```
a = np.diag(np.arange(3)) # <-- super/sub diagional matrix
a
a
a = 10 # third line(2), second column(1)
```
### 2D array (2)
```
a= np.arange(6) + np.arange(0, 51, 10)[:, np.newaxis]
a
a# <-- row is equal to 1; columns start from 4th to 6th
a# <-- rows start from 5th; columns start from 5th
a[:,2]# <-- rows start from 1st; columns is equal to 2
a # <-- rows start from 3rd number in 2 steps ; columns start from 1st number in 2 steps
```
有图还是挺直观的 skyward 发表于 2020-6-8 12:06
有图还是挺直观的
有图再配上说明,就很棒棒哦{:301_986:} 希望持續教學
3q..
页:
[1]