関数電卓として利用してみる


In [1]:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from pandas import Series, DataFrame

In [2]:
2*(1+3)


Out[2]:
8

In [3]:
2**10


Out[3]:
1024

In [4]:
_ * 2


Out[4]:
2048

In [5]:
1/2


Out[5]:
0

In [6]:
1.0/2


Out[6]:
0.5

In [7]:
float(1)/2


Out[7]:
0.5

In [8]:
np.pi


Out[8]:
3.141592653589793

In [9]:
np.e


Out[9]:
2.718281828459045

In [10]:
np.sin(np.pi/4)


Out[10]:
0.70710678118654746

In [11]:
np.sqrt(2)


Out[11]:
1.4142135623730951

In [12]:
np.sqrt([0,1,2,3])


Out[12]:
array([ 0.        ,  1.        ,  1.41421356,  1.73205081])

In [13]:
data_x = [0.0, -0.95, -0.59, 0.59, 0.95]
data_y = [1.0, 0.31, -0.81, -0.81, 0.31]

plt.scatter(data_x,data_y)


Out[13]:
<matplotlib.collections.PathCollection at 0x4421050>

In [14]:
data_x = [0,1,2,3,4,5]
data_y = [0,1,4,9,16,25]

plt.plot(data_x,data_y)


Out[14]:
[<matplotlib.lines.Line2D at 0x46a3e90>]

In [15]:
data_x = np.linspace(0,1,101)
data_y = np.sin(2.0*np.pi*data_x)

plt.plot(data_x,data_y)


Out[15]:
[<matplotlib.lines.Line2D at 0x476ac90>]

練習問題

(1) 次の関数のグラフを描いてください。この時、x座標のサンプル数を10, 50, 100として、それぞれのグラフの違いを観察してください。

  • y = cos(2πx) (0≦x≦1)
  • y = exp(-x*x) (-5≦x≦5)
  • y = √(1-x*x) (-1≦x≦1)

(2) 次のように各データの座標値 (x,y) のリストが与えられています。このデータの散布図を描いてください。


In [16]:
data = [(0.0,1.0), (-0.95,0.31), (-0.59,-0.81), (0.59,-0.81), (0.95, 0.31)]