import altair as alt
from vega_datasets import data
cars = data.cars()
alt.Chart(cars).mark_point().encode(
x='Horsepower',
y='Miles_per_Gallon',
color='Origin',
).interactive()
# 公众号 Python实用宝典
import autograd.numpy as np
from autograd import grad
def oneline(x):
y = x/2
return y
grad_oneline = grad(oneline)
print(grad_oneline(3.0))
运行代码,传入任意X值,你就能得到在该X值下的斜率:
(base) G:\push\20220724>python 1.py
0.5
由于这是一条直线,因此无论你传什么值,都只会得到0.5的结果。
那么让我们再试试一个tanh函数:
# 公众号 Python实用宝典
import autograd.numpy as np
from autograd import grad
def tanh(x):
y = np.exp(-2.0 * x)
return (1.0 - y) / (1.0 + y)
grad_tanh = grad(tanh)
print(grad_tanh(1.0))
def training_loss(weights):
# Training loss is the negative log-likelihood of the training labels.
preds = logistic_predictions(weights, inputs)
label_probabilities = preds * targets + (1 - preds) * (1 - targets)
return -np.sum(np.log(label_probabilities))
# Define a function that returns gradients of training loss using Autograd.
training_gradient_fun = grad(training_loss)
# Optimize weights using gradient descent.
weights = np.array([0.0, 0.0, 0.0])
print("Initial loss:", training_loss(weights))
for i in range(100):
weights -= training_gradient_fun(weights) * 0.01
print("Trained loss:", training_loss(weights))
(base) G:\push\20220623>python 1.py
Traceback (most recent call last):
File "1.py", line 12, in <module>
drawer.draw_locations(df[cpca._ADCODE], "df.html")
File "G:\Anaconda3\lib\site-packages\cpca\drawer.py", line 41, in draw_locations
import folium
ModuleNotFoundError: No module named 'folium'
from interval import interval
a = interval[1,5]
# interval([1.0, 5.0])
print(3 in a)
# True
此外,你还可以构建一个多区间:
from interval import interval
a = interval([0, 1], [2, 3], [10, 15])
print(2.5 in a)
# True
interval.hall 方法还可以将多个区间合并,取其最小及最大值为边界:
from interval import interval
a = interval.hull((interval[1, 3], interval[10, 15], interval[16, 2222]))
# interval([1.0, 2222.0])
print(1231 in a)
# True
区间并集计算:
from interval import interval
a = interval.union([interval([1, 3], [4, 6]), interval([2, 5], 9)])
# interval([1.0, 6.0], [9.0])
print(5 in a)
# True
print(8 in a)
# False