In [2]:
import scipy.optimize
In [3]:
import numpy as np
In [4]:
def f(x):
    return x ** 3 - 3 * x
In [5]:
scipy.optimize.minimize(f, 0)
Out[5]:
      fun: -2.0
 hess_inv: array([[0.330033]])
      jac: array([7.4505806e-08])
  message: 'Optimization terminated successfully.'
     nfev: 12
      nit: 2
     njev: 4
   status: 0
  success: True
        x: array([1.])
In [6]:
def g(x):
    return x[0] ** 2 + x[1] ** 2
In [7]:
scipy.optimize.minimize(g, np.array([1, 2]))
Out[7]:
      fun: 5.675596338880969e-16
 hess_inv: array([[ 0.9, -0.2],
       [-0.2,  0.6]])
      jac: array([-6.40723385e-09, -2.77156293e-08])
  message: 'Optimization terminated successfully.'
     nfev: 12
      nit: 2
     njev: 3
   status: 0
  success: True
        x: array([-1.06541975e-08, -2.13083953e-08])
In [26]:
def f(x):
    return x ** 4 - 10 * x ** 2 + 10 * x
In [27]:
import matplotlib.pyplot as plt
%matplotlib inline
In [42]:
from ipywidgets import interact, FloatSlider
In [50]:
@interact(initial_condition=
          FloatSlider(min=-2, max=2, step=0.1, 
                      description='init',
                      continuous_update=False),
          a = FloatSlider(min=-5, 
                          max=5,
                          continuous_update=False))
def draw_min(initial_condition, a):
    def f(x):
        return x ** 4 - 10 * x ** 2 + a * x
    x = np.linspace(-4, 4)
    plt.plot(x, f(x))
    opt = scipy.optimize.minimize(f, 
                                  initial_condition)
    plt.ylim(-30, 120)
    plt.plot(opt.x, [opt.fun], 'o')
    plt.plot([initial_condition], 
             [f(initial_condition)], 'o')
In [55]:
scipy.optimize.minimize(f, 0.1)
Out[55]:
      fun: -48.495289360338106
 hess_inv: array([[0.01916371]])
      jac: array([-2.86102295e-06])
  message: 'Optimization terminated successfully.'
     nfev: 27
      nit: 4
     njev: 9
   status: 0
  success: True
        x: array([-2.45336671])
In [57]:
def f(x):
    return x[0] * x[1]
    # z = x_1 × x_2
In [59]:
scipy.optimize.minimize(f, np.array([0.1, 0.2]))
Out[59]:
      fun: -5.354679891023784e+17
 hess_inv: array([[ 2.25  , -0.125 ],
       [-0.125 ,  0.5625]])
      jac: array([0., 0.])
  message: 'Optimization terminated successfully.'
     nfev: 76
      nit: 2
     njev: 19
   status: 0
  success: True
        x: array([-1.36007298e+09,  3.93705336e+08])
In [61]:
f(np.array([0.1, 0.2]))
Out[61]:
0.020000000000000004
In [63]:
from scipy.integrate import quad
In [68]:
quad(lambda x: np.sqrt(1 - x ** 2), -1, 1)[0] * 2
Out[68]:
3.1415926535897967
In [69]:
np.pi
Out[69]:
3.141592653589793
In [81]:
x = np.linspace(-5, 5, 200)
y = np.linspace(-5, 5, 200)
X, Y = np.meshgrid(x, y)
plt.figure(figsize=(6, 6))
plt.contour(x, y, X ** 2 - Y ** 2, 50)
Out[81]:
<matplotlib.contour.QuadContourSet at 0x10d5f7a58>
In [86]:
plt.contourf(x, y, X ** 2 - Y ** 2, [1, 5, 7],
            cmap='spring')
Out[86]:
<matplotlib.contour.QuadContourSet at 0x10e32a588>
In [87]:
import sys
In [88]:
!"{sys.executable}" -m pip install fillplots
Collecting fillplots
  Downloading https://files.pythonhosted.org/packages/f8/00/6cc5478e007bf4a0858842c7a7ae44f5d6d79fe64f38b3051bf032da3162/fillplots-0.0.2.tar.gz
Building wheels for collected packages: fillplots
  Running setup.py bdist_wheel for fillplots ... done
  Stored in directory: /Users/user/Library/Caches/pip/wheels/81/56/b0/2ae9da1360eb461e294bedc11a4ff6258a16a1881621e6a404
Successfully built fillplots
Installing collected packages: fillplots
Successfully installed fillplots-0.0.2
In [99]:
x = np.linspace(-3, 3, 500)
plt.fill_between(x, 1 - x ** 2, x ** 2, 
                 where=1 - x ** 2 > x ** 2, 
                 color='green')
Out[99]:
<matplotlib.collections.PolyCollection at 0x10eb3c208>
In [100]:
import sys
!"{sys.executable}" -m pip install plotly_express
Collecting plotly_express
  Downloading https://files.pythonhosted.org/packages/c7/b5/c1e390467d048fd0550a09a386029d3f17f96abe45b33b083845dddee7d6/plotly_express-0.1.3-py2.py3-none-any.whl (72kB)
    100% |████████████████████████████████| 81kB 468kB/s ta 0:00:01
Requirement already satisfied: pandas>=0.20.0 in /usr/local/lib/python3.7/site-packages (from plotly_express) (0.24.1)
Requirement already satisfied: statsmodels>=0.9.0 in /usr/local/lib/python3.7/site-packages (from plotly_express) (0.9.0)
Requirement already satisfied: scipy>=0.18 in /usr/local/lib/python3.7/site-packages (from plotly_express) (1.1.0)
Requirement already satisfied: numpy>=1.11 in /usr/local/lib/python3.7/site-packages (from plotly_express) (1.14.5)
Collecting plotly>=3.7.1 (from plotly_express)
  Downloading https://files.pythonhosted.org/packages/37/25/32fb6f58bfc6f462d61bf0de95e8c0bbf07bfaba11ed0ecb81c0590f3584/plotly-3.7.1-py2.py3-none-any.whl (35.7MB)
    100% |████████████████████████████████| 35.7MB 230kB/s ta 0:00:01    76% |████████████████████████▍       | 27.2MB 187kB/s eta 0:00:46
Requirement already satisfied: patsy>=0.5 in /usr/local/lib/python3.7/site-packages (from plotly_express) (0.5.0)
Requirement already satisfied: python-dateutil>=2.5.0 in /usr/local/lib/python3.7/site-packages (from pandas>=0.20.0->plotly_express) (2.7.3)
Requirement already satisfied: pytz>=2011k in /usr/local/lib/python3.7/site-packages (from pandas>=0.20.0->plotly_express) (2018.5)
Requirement already satisfied: requests in /usr/local/lib/python3.7/site-packages (from plotly>=3.7.1->plotly_express) (2.19.1)
Requirement already satisfied: nbformat>=4.2 in /usr/local/lib/python3.7/site-packages (from plotly>=3.7.1->plotly_express) (4.4.0)
Requirement already satisfied: six in /usr/local/lib/python3.7/site-packages (from plotly>=3.7.1->plotly_express) (1.11.0)
Requirement already satisfied: decorator>=4.0.6 in /usr/local/lib/python3.7/site-packages (from plotly>=3.7.1->plotly_express) (4.3.0)
Requirement already satisfied: retrying>=1.3.3 in /usr/local/lib/python3.7/site-packages (from plotly>=3.7.1->plotly_express) (1.3.3)
Requirement already satisfied: idna<2.8,>=2.5 in /usr/local/lib/python3.7/site-packages (from requests->plotly>=3.7.1->plotly_express) (2.7)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /usr/local/lib/python3.7/site-packages (from requests->plotly>=3.7.1->plotly_express) (3.0.4)
Requirement already satisfied: urllib3<1.24,>=1.21.1 in /usr/local/lib/python3.7/site-packages (from requests->plotly>=3.7.1->plotly_express) (1.23)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/site-packages (from requests->plotly>=3.7.1->plotly_express) (2018.4.16)
Requirement already satisfied: jupyter-core in /usr/local/lib/python3.7/site-packages (from nbformat>=4.2->plotly>=3.7.1->plotly_express) (4.4.0)
Requirement already satisfied: traitlets>=4.1 in /usr/local/lib/python3.7/site-packages (from nbformat>=4.2->plotly>=3.7.1->plotly_express) (4.3.2)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /usr/local/lib/python3.7/site-packages (from nbformat>=4.2->plotly>=3.7.1->plotly_express) (2.6.0)
Requirement already satisfied: ipython-genutils in /usr/local/lib/python3.7/site-packages (from nbformat>=4.2->plotly>=3.7.1->plotly_express) (0.2.0)
Installing collected packages: plotly, plotly-express
  Found existing installation: plotly 3.0.0
    Uninstalling plotly-3.0.0:
      Successfully uninstalled plotly-3.0.0
Successfully installed plotly-3.7.1 plotly-express-0.1.3
In [101]:
import pandas as pd
In [102]:
dat = pd.read_csv("https://bit.ly/2sqZ1k5")
In [105]:
import plotly_express as px
In [106]:
dat.columns
Out[106]:
Index(['id', 'gender', 'race', 'ses', 'schtyp', 'prog', 'read', 'write',
       'math', 'science', 'socst'],
      dtype='object')
In [108]:
px.scatter(dat, x='math', y='socst')