pythonで作るサンプルデータ

# サンプルデータ



## 線形データ


```python
n=20
```


```python
a = np.arange(n).reshape(4, -1); a  # 5列の行列
```




    array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
            17, 18, 19, 20, 21, 22, 23, 24],
           [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
            42, 43, 44, 45, 46, 47, 48, 49],
           [50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
            67, 68, 69, 70, 71, 72, 73, 74],
           [75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
            92, 93, 94, 95, 96, 97, 98, 99]])




```python
df = pd.DataFrame(a, columns=list('abcde')); df
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
      <th>e</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>0</td>
      <td>1</td>
      <td>2</td>
      <td>3</td>
      <td>4</td>
    </tr>
    <tr>
      <th>1</th>
      <td>5</td>
      <td>6</td>
      <td>7</td>
      <td>8</td>
      <td>9</td>
    </tr>
    <tr>
      <th>2</th>
      <td>10</td>
      <td>11</td>
      <td>12</td>
      <td>13</td>
      <td>14</td>
    </tr>
    <tr>
      <th>3</th>
      <td>15</td>
      <td>16</td>
      <td>17</td>
      <td>18</td>
      <td>19</td>
    </tr>
  </tbody>
</table>
</div>



## ランダムデータ


```python
r = np.random.randn(4, 5); r
```




    array([[-0.37840346, -0.84591793,  0.50590263,  0.0544243 ,  0.59361247],
           [-0.2726931 , -1.74415635,  0.0199559 , -0.20695113, -1.19559455],
           [-0.59799566, -0.26810224, -0.18738038,  1.05843686,  0.72317579],
           [ 1.23389386,  1.91293041, -1.33322818,  0.78255026,  2.04737357]])




```python
df = pd.DataFrame(r, columns=list('abcde')); df
```




<div>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
      <th>c</th>
      <th>d</th>
      <th>e</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-0.378403</td>
      <td>-0.845918</td>
      <td>0.505903</td>
      <td>0.054424</td>
      <td>0.593612</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-0.272693</td>
      <td>-1.744156</td>
      <td>0.019956</td>
      <td>-0.206951</td>
      <td>-1.195595</td>
    </tr>
    <tr>
      <th>2</th>
      <td>-0.597996</td>
      <td>-0.268102</td>
      <td>-0.187380</td>
      <td>1.058437</td>
      <td>0.723176</td>
    </tr>
    <tr>
      <th>3</th>
      <td>1.233894</td>
      <td>1.912930</td>
      <td>-1.333228</td>
      <td>0.782550</td>
      <td>2.047374</td>
    </tr>
  </tbody>
</table>
</div>




```python
df.plot()
```




    <matplotlib.axes._subplots.AxesSubplot at 0x17699af2a58>



![output_8_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/af4e2451-fdbb-d392-ef80-845f40ef1a74.png)



```python
df = pd.DataFrame(np.random.randn(n,n))
```


```python
plt.contourf(df, cmap='jet')
```




    <matplotlib.contour.QuadContourSet at 0x1769a1a12b0>




![output_10_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/52e3fc91-9e3d-8a8f-f630-e802d667928e.png)



等高線表示


```python
plt.pcolor(df, cmap='jet')
```




    <matplotlib.collections.PolyCollection at 0x1769b1e2208>




![output_12_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/2253dbb3-2b7e-965b-0ae3-a592432fd2a0.png)



カラーマップ表示

## sin波


```python
n=100
x = np.linspace(0, 2*np.pi, n)
```


```python
s = pd.Series(np.sin(x), index=x)
s.plot()
```




    <matplotlib.axes._subplots.AxesSubplot at 0x1769e695780>




![output_16_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/1bf50a4a-94ad-5a1d-6cf6-946001abb5ee.png)



sin波


```python
snoise = s + 0.1 * np.random.randn(n)
sdf = pd.DataFrame({'sin wave':s, 'noise wave': snoise})
sdf.plot(color=('r', 'b'))
```




    <matplotlib.axes._subplots.AxesSubplot at 0x1769e8586d8>




![output_18_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/e7d8848f-6173-8dd8-3451-87cb2cdf0521.png)



ノイズをのせた

## 正規分布


```python
from  scipy import stats as ss
```


```python
median = x[int(n/2)]  # xの中央値
g = pd.Series(ss.norm.pdf(x, loc=median), x)
g.plot()
```




    <matplotlib.axes._subplots.AxesSubplot at 0x1769ffba128>




![output_22_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/f77a730c-426f-0941-476e-efc2b95260b5.png)




```python
gnoise = g + 0.01 * np.random.randn(n)
df = pd.DataFrame({'gauss wave':g, 'noise wave': gnoise})
df.plot(color=('r', 'b'))
```




    <matplotlib.axes._subplots.AxesSubplot at 0x1769e970828>




![output_23_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/33d19316-5160-8e7c-72de-2ec4e57b4239.png)



## log関数


```python
median = x[int(n/2)]  # xの中央値
x1 = x + 10e-3
l = pd.Series(np.log(x1), x1)
l.plot()
```




    <matplotlib.axes._subplots.AxesSubplot at 0x1769ffba5f8>




![output_25_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/5d9a4487-571f-20fe-5bdd-2c13900bc1ac.png)




```python
lnoise = l + 0.1 * np.random.randn(n)
df = pd.DataFrame({'log wave':l, 'noise wave': lnoise})
df.plot(color=('r', 'b'))
```





    <matplotlib.axes._subplots.AxesSubplot at 0x176a00ec358>




![output_26_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/38617bde-5ee0-a872-d53f-abc9c356d083.png)


## ランダムウォーク


```python
n = 1000
se = pd.Series(np.random.randint(-1, 2, n)).cumsum()
se.plot()
```




    <matplotlib.axes._subplots.AxesSubplot at 0x284f3c62c18>




![README_28_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/7b69f291-8443-d3f0-92ef-1ef833711975.png)



np.random.randint(-1, 2, n)で(-1, 0, 1)のどれかをランダムにn個生成し、cumsum()で積み上げ合計していくことでランダムウォークを描く。


```python
sma100 = se.rolling(100).mean()
ema100 = se.ewm(span=100).mean()

df = pd.DataFrame({'Chart': se,  'SMA100': sma100, 'EMA100': ema100})
df.plot(style = ['--','-','-'])
```




    <matplotlib.axes._subplots.AxesSubplot at 0x284f3cadcc0>



![README_30_1.png](https://qiita-image-store.s3.amazonaws.com/0/113494/5125c7a9-ea5b-55c2-f086-a16c2049b2b9.png)


単純移動平均線(Simple Moving Average)と指数移動平均線(Exponential Moving Average)を同時に描画した。
EMAの方がSMAと比べて一般的に直近の動きを反映しやすく、トレンドに追随しやすいといわれている。



記事の内容とは関係ないけど、今さらながらjupyter notebookで書いてmd形式で落とすと、qiitaにはっつけるだけでいいからすごい楽。