#!/usr/bin/env python # coding: utf-8 # 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) # In[6]: def g(x): return x[0] ** 2 + x[1] ** 2 # In[7]: scipy.optimize.minimize(g, np.array([1, 2])) # In[26]: def f(x): return x ** 4 - 10 * x ** 2 + 10 * x # In[27]: import matplotlib.pyplot as plt get_ipython().run_line_magic('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) # 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])) # In[61]: f(np.array([0.1, 0.2])) # In[63]: from scipy.integrate import quad # In[68]: quad(lambda x: np.sqrt(1 - x ** 2), -1, 1)[0] * 2 # In[69]: np.pi # 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) # In[86]: plt.contourf(x, y, X ** 2 - Y ** 2, [1, 5, 7], cmap='spring') # In[87]: import sys # In[88]: get_ipython().system('"{sys.executable}" -m pip install fillplots') # 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') # In[100]: import sys get_ipython().system('"{sys.executable}" -m pip install plotly_express') # 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 # In[108]: px.scatter(dat, x='math', y='socst') # In[112]: px.scatter_matrix(dat, dimensions=['math', 'socst', 'science'], color='schtyp', opacity=0.5) # In[120]: px.scatter(dat, x='math', y='science', color='gender', marginal_x='box', marginal_y='violin') # In[122]: imdb = pd.read_csv("http://math-info.hse.ru/f/2018-19/spb-python/movie_metadata.csv") # In[130]: px.histogram(imdb.fillna(0), x='duration', animation_frame='title_year', range_y=(0, 10)) # In[ ]: