```python
import torch

import pandas as pd
import numpy as np
import torch.nn as nn
```


```python
a = np.eye(5, 3)[np.array([0, 1, 0, 2, 1])]
a.reshape(-1, 3)
```




    array([[1., 0., 0.],
           [0., 1., 0.],
           [1., 0., 0.],
           [0., 0., 1.],
           [0., 1., 0.]])




```python
a = np.zeros((5, 4, 3))
b = np.random.randn(5, 4, 3)
a - b[:, :, 1].reshape(5, -1, 1)
```




    array([[[ 0.27490142,  0.27490142,  0.27490142],
            [-0.28210933, -0.28210933, -0.28210933],
            [-0.54029727, -0.54029727, -0.54029727],
            [ 0.09021319,  0.09021319,  0.09021319]],
    
           [[ 1.53565429,  1.53565429,  1.53565429],
            [-1.03396388, -1.03396388, -1.03396388],
            [ 0.38334356,  0.38334356,  0.38334356],
            [ 0.12727384,  0.12727384,  0.12727384]],
    
           [[-0.83364892, -0.83364892, -0.83364892],
            [-0.31906883, -0.31906883, -0.31906883],
            [ 0.10114786,  0.10114786,  0.10114786],
            [-2.08064701, -2.08064701, -2.08064701]],
    
           [[-1.82743214, -1.82743214, -1.82743214],
            [-0.88780012, -0.88780012, -0.88780012],
            [-0.59953832, -0.59953832, -0.59953832],
            [-1.14967454, -1.14967454, -1.14967454]],
    
           [[ 1.45511417,  1.45511417,  1.45511417],
            [ 0.2408046 ,  0.2408046 ,  0.2408046 ],
            [-0.08786458, -0.08786458, -0.08786458],
            [ 1.01358446,  1.01358446,  1.01358446]]])




```python
a = np.eye(5, 4)[np.array([1, 2, 1, 0, 4])]
target = np.array([1, 2, 1, 0, 4]).reshape(-1, 1)
target1 = np.array([[1, 2], [0, 3], [2, 0], [2, 1]])
```


```python
from ylearn.estimator_model.utils import cartesian
```


```python
label = cartesian([[1, 2, 3], [4, 5]])
t = np.array([[1, 4], [2, 5], [3, 5], [2, 4]])
v = np.zeros((4, 1))
label_dict = {tuple(k):i for i, k in enumerate(label)}
for i, t_i in enumerate(t):
    v[i] = label_dict[tuple(t_i)]
```


```python
import ylearn.estimator_model.double_ml as dml
from sklearn.linear_model import LinearRegression
```


```python
a = dml.DML4CATE(
    x_model=LinearRegression(),
    y_model=LinearRegression()
)

```


```python
len({1:2})
```




    1




```python
t = np.array([['a', 'hello'], ['b', 'bad'], ['a', 'not']])
a.comp_transormer(t, categories='auto')
a._is_fitted = True
```


```python
d = a.comp_transormer(np.array(['a', 'hello']).reshape(1, -1))
d
```




    array([[0., 1., 0., 0., 0., 0.]])




```python
any([True, False, True])
```




    True




```python
from ylearn.estimator_model.utils import convert4onehot, get_tr_ctrl
# convert4onehot(d).astype(int)[0]
get_tr_ctrl(['b', 'not'], a.comp_transormer, True)
```




    5




```python
np.eye(5,3)[np.array([2, 1, 1, 2, 2])]
```




    array([[0., 0., 1.],
           [0., 1., 0.],
           [0., 1., 0.],
           [0., 0., 1.],
           [0., 0., 1.]])




```python
np.array([1, 0, 0])
```




    array([1, 0, 0])




```python
np.ones((5, 4))[:, np.array([2])]
```




    array([[1.],
           [1.],
           [1.],
           [1.],
           [1.]])




```python
a.label_dict
```




    {(0, 0): 0, (0, 1): 1, (0, 2): 2, (1, 0): 3, (1, 1): 4, (1, 2): 5}




```python
a.label_dict
```




    {(0, 0): 0, (0, 1): 1, (0, 2): 2, (1, 0): 3, (1, 1): 4, (1, 2): 5}




```python
np.full((v.shape[0], 1), np.NaN)

```




    array([[nan],
           [nan],
           [nan],
           [nan]])




```python
OrdinalEncoder(categories='auto')
```


```python
cartesian([[1, 2, 3], [4, 5]])
```




    array([[1, 4],
           [1, 5],
           [2, 4],
           [2, 5],
           [3, 4],
           [3, 5]])




```python
{(1, 2, 3):2}
```




    {(1, 2, 3): 2}




```python
np.array([0, 1, 0, 0]).nonzero()[0]
```




    (array([1]),)




```python
tuple(np.array([[1, 2, 3], [4, 5, 6]]))
```




    (array([1, 2, 3]), array([4, 5, 6]))




```python
from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder
ordi = OrdinalEncoder()
o.fit_transform(target1).toarray()
ordi.fit_transform(np.array([['good', 2, 1], ['bad', 3, 2]])).astype(int)
```




    array([[1, 0, 0],
           [0, 1, 1]])




```python
o = OneHotEncoder(categories=[np.array([1, 2, 3])])
o.fit_transform(np.array([1, 2, 3, 2, 1]).reshape(-1, 1)).toarray()
# o.n_features_in_
```




    array([[1., 0., 0.],
           [0., 1., 0.],
           [0., 0., 1.],
           [0., 1., 0.],
           [1., 0., 0.]])




```python
ordi.categories_
```




    [array(['bad', 'good'], dtype='<U21'),
     array(['2', '3'], dtype='<U21'),
     array(['1', '2'], dtype='<U21')]




```python
np.arange(len(ordi.categories_[0]))
```




    array([0, 1])




```python
np.array([[5]]) * np.ones((4, 3))
```




    array([[5., 5., 5.],
           [5., 5., 5.],
           [5., 5., 5.],
           [5., 5., 5.]])




```python
a = np.random.randint(0, 5, (4, 2))
np.repeat(a, 3, 0)
np.tile(a, (2, 3))
```




    array([[2, 1, 2, 1, 2, 1],
           [0, 4, 0, 4, 0, 4],
           [4, 2, 4, 2, 4, 2],
           [0, 3, 0, 3, 0, 3],
           [2, 1, 2, 1, 2, 1],
           [0, 4, 0, 4, 0, 4],
           [4, 2, 4, 2, 4, 2],
           [0, 3, 0, 3, 0, 3]])




```python
a = np.all(np.array([1, 1]) == np.array([[1, 1], [1, 0], [1, 1], [0, 0]]), axis=1)
b = np.all(np.array([0, 0]) == np.array([[1, 0], [0, 0], [1, 1], [1, 1]]), axis=1)
np.any((a, b), axis=0)
```




    array([ True,  True,  True, False])



\begin{equation}
(\frac{I(x_i=xt)y_i}{ps_{x_i=xt}(w_i)}
            - yt_i\frac{I(x_i=xt)-ps_{x_i=xt}(w_i)}{ps_{x_i=xt}(w_i)})
            (\frac{I(x_i=x0)y_i}{ps_{x_i=x0}(w_i)}
            - y0_i\frac{I(x_i=x0)-ps_{x_i=x0}(w_i)}{ps_{x_i=x0}(w_i)})
\end{equation}


```python
from estimator_model.utils import get_wv, _get_wv
a = np.ones((3, 2))
b = np.zeros((3, 5))
c = None
get_wv(c, a) == _get_wv(c, a)
```




    array([[ True,  True],
           [ True,  True],
           [ True,  True]])




```python
np.zeros(np.zeros((3, 4)).shape + (3,)).shape
```




    (3, 4, 3)




```python
from sklearn.preprocessing import PolynomialFeatures
```


```python
x = np.random.randint(0, 6, size=(4, 3))
x
```




    array([[4, 3, 1],
           [2, 2, 3],
           [3, 1, 0],
           [3, 0, 5]])




```python
poly = PolynomialFeatures(2)
poly.fit(x)
poly.transform(x)
```




    array([[ 1.,  4.,  3.,  1., 16., 12.,  4.,  9.,  3.,  1.],
           [ 1.,  2.,  2.,  3.,  4.,  4.,  6.,  4.,  6.,  9.],
           [ 1.,  3.,  1.,  0.,  9.,  3.,  0.,  1.,  0.,  0.],
           [ 1.,  3.,  0.,  5.,  9.,  0., 15.,  0.,  0., 25.]])




```python
pol = PolynomialFeatures()

```


```python
e = np.array([['hello', 'bad'], ['good', 'bad'], ['hey', 'not'], ['ok', 'not'
]])
```


```python
from sklearn.preprocessing import OrdinalEncoder, OneHotEncoder
c = OneHotEncoder()
c.fit(e)
c.transform(e).toarray()
```




    array([[0., 1., 0., 0., 1., 0.],
           [1., 0., 0., 0., 1., 0.],
           [0., 0., 1., 0., 0., 1.],
           [0., 0., 0., 1., 0., 1.]])




```python
f = OrdinalEncoder()
f.fit_transform(e)
f.categories_
```




    [array(['good', 'hello', 'hey', 'ok'], dtype='<U5'),
     array(['bad', 'not'], dtype='<U5')]




```python
from itertools import product
list(product([np.array([0, 1, 2, 3]), np.array([0, 1]), np.array([0, 1])]))
```




    [(array([0, 1, 2, 3]),), (array([0, 1]),), (array([0, 1]),)]




```python
from sklearn.linear_model import LinearRegression
l = LinearRegression()
x = np.array([[1], [2], [3], [4]])
y = (np.random.normal() * (x)).ravel()
l.fit(x, y)
```




    LinearRegression()




```python
np.all(np.array([[1, 2, 3], [4, 5, 6]]) == np.array([1, 2, 3]), axis=1)
```




    array([ True, False])




```python
a = np.ones((3, 2, 4))
a[:, :, 1] = np.zeros((3, 2))
```


```python
np.ones((3, 2)).squeeze()
```




    array([[1., 1.],
           [1., 1.],
           [1., 1.]])




```python
def broadcast_unit_treatments(X, d_t):
    """
    Generate `d_t` unit treatments for each row of `X`.
    Parameters
    ----------
    d_t: int
        Number of treatments
    X : array
        Features
    Returns
    -------
    X, T : (array, array)
        The updated `X` array (with each row repeated `d_t` times),
        and the generated `T` array
    """
    d_x = X.shape[0]
    eye = np.eye(d_t)
    # tile T and repeat X along axis 0 (so that the duplicated rows of X remain consecutive)
    T = np.tile(eye, (d_x, 1))
    Xs = np.repeat(X, d_t, axis=0)
    return Xs, T
broadcast_unit_treatments(np.random.randint(0, 3, (3, 2)), 3)
```




    (array([[0, 1],
            [0, 1],
            [0, 1],
            [2, 1],
            [2, 1],
            [2, 1],
            [2, 0],
            [2, 0],
            [2, 0]]),
     array([[1., 0., 0.],
            [0., 1., 0.],
            [0., 0., 1.],
            [1., 0., 0.],
            [0., 1., 0.],
            [0., 0., 1.],
            [1., 0., 0.],
            [0., 1., 0.],
            [0., 0., 1.]]))




```python
a = [1, 2, 3]
a.insert(0, 0)
np.ones((5,1)).repeat(3, axis=1)
```




    array([[1., 1., 1.],
           [1., 1., 1.],
           [1., 1., 1.],
           [1., 1., 1.],
           [1., 1., 1.]])




```python
a = np.zeros((1, 5))
a[:, 0] = 1
np.repeat(a, 2, axis=0)
```




    array([[1., 0., 0., 0., 0.],
           [1., 0., 0., 0., 0.]])




```python
from sklearn.preprocessing import OneHotEncoder
```


```python
np.array([[1, 2, 3], [4, 5, 6]])[np.array([True, False])]
```


```python
len(np.array([2, 3, 4]))
```




    3




```python
product()
```


```python
np.array([[1, 2, 3], [4, 5,5]]).ravel()
```




    array([1, 2, 3, 4, 5, 5])




```python
np.eye(5, 3)[np.array([1, 2, 2, 1, 1])]
```




    array([[0., 1., 0.],
           [0., 0., 1.],
           [0., 0., 1.],
           [0., 1., 0.],
           [0., 1., 0.]])




```python
a = np.array([['good', 'hello'], ['bad', 'hello'], ['good', 'not']])
from sklearn.preprocessing import OneHotEncoder, OrdinalEncoder
o = OneHotEncoder()
o.fit(np.array([1, 1, 0, 1]).reshape(-1, 1))
o.categories_
```




    [array([0, 1])]




```python
np.array([np.ones(2,), np.ones(2, ), np.ones(2, )])
```




    array([[1., 1.],
           [1., 1.],
           [1., 1.]])




```python
list([1, 2, 3])
```




    [1, 2, 3]




```python
np.repeat(np.array([[4, 5, 6]]), 3, axis=0)
```




    array([[4, 5, 6],
           [4, 5, 6],
           [4, 5, 6]])




```python
from estimator_model.utils import get_group_ids
target = np.repeat(np.array([[4, 5, 6]]), 3, axis=0)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([[2, 3, 4], [3, 4, 5], [1, 1, 1]])
get_group_ids(target, a, a, b)
```




    [array([[4, 5, 6]]), array([[3, 4, 5]])]




```python
np.mean()
```


```python
np.concatenate()
```


```python
index = np.all(np.array([[1, 2, 3], [1, 0, 1]]) == np.array([[1, 0, 3], [1, 0, 1]]), axis=1)
np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])[np.where(index)]
```




    array([[4, 5, 6]])




```python
np.argmax(np.random.normal(size=((3, 2))), axis=1)
```




    array([1, 1, 1])




```python
np.concatenate((np.ones((3, 1)), np.random.normal(size=((3, 2))), np.zeros((3, 1))), axis=1)
```




    array([[ 1.        ,  0.19681735, -1.1459864 ,  0.        ],
           [ 1.        ,  1.92138143,  0.37209335,  0.        ],
           [ 1.        , -0.80434229,  0.55304074,  0.        ]])




```python
np.zeros)
```


```python
torch.argmax(
    nn.Softmax(dim=1)(torch.randn(5, 3))
, dim=1).reshape(-1, 1)
```




    tensor([[2],
            [0],
            [1],
            [1],
            [1]])




```python
a = nn.Softmax(dim=1)
b = nn.LogSoftmax(dim=1)
c = torch.randn(5, 3)
torch.log(a(c))
nn.CrossEntropyLoss
```




    tensor([[-0.1901, -2.6149, -2.3034],
            [-1.4519, -0.8391, -1.0973],
            [-0.9513, -0.8132, -1.7701],
            [-3.6564, -2.8858, -0.0852],
            [-1.4684, -0.3985, -2.3190]])




```python
b(c)
```




    tensor([[-0.1901, -2.6149, -2.3034],
            [-1.4519, -0.8391, -1.0973],
            [-0.9513, -0.8132, -1.7701],
            [-3.6564, -2.8858, -0.0852],
            [-1.4684, -0.3985, -2.3190]])




```python
a = torch.cat((torch.ones(5, 3), torch.zeros(5, 2)), dim=1)
torch.cat((a[:, :5], a[:, 5:5]), dim=1)
```




    tensor([[1., 1., 1., 0., 0.],
            [1., 1., 1., 0., 0.],
            [1., 1., 1., 0., 0.],
            [1., 1., 1., 0., 0.],
            [1., 1., 1., 0., 0.]])




```python
m = nn.Embedding(5, 4)
m(torch.tensor([1, 2, 3, 4,2]))
```




    tensor([[ 0.9123, -0.3915,  0.1124,  0.3993],
            [ 2.1194,  0.1742, -0.6385,  0.0089],
            [ 0.0088, -0.6977, -0.4991,  1.0913],
            [ 0.3151, -0.0975, -0.6318, -0.1264],
            [ 2.1194,  0.1742, -0.6385,  0.0089]], grad_fn=<EmbeddingBackward0>)




```python
torch.argmax(torch.eye(5, 4), dim=0).reshape(-1, 1)
```




    tensor([[0],
            [1],
            [2],
            [3]])




```python
m = nn.Softmax(dim=1)
m(torch.randn(5, 3))
```




    tensor([[0.1918, 0.3555, 0.4527],
            [0.1439, 0.4630, 0.3932],
            [0.8279, 0.1352, 0.0369],
            [0.1293, 0.3380, 0.5327],
            [0.1388, 0.7279, 0.1333]])




```python
torch.where(torch.eye(5, 4).index_select(dim=0, index=(torch.ones(5,) * 2).int()) == 1)
```




    tensor([2, 2, 2, 2, 2])




```python
torch.zeros(5,).mean(dim=0)
```




    tensor(0.)




```python
pd.DataFrame({'a':['hello', 'good', 2]}).values
```




    array([['hello'],
           ['good'],
           [2]], dtype=object)




```python
a = np.array([1, 2, 3, 3, 1])
torch.tensor(a).shape
```




    torch.Size([5])




```python
a = np.random.randn(3, 4)
a
```




    array([[ 0.3866119 ,  0.53135121,  1.57163397, -0.05024657],
           [-1.15621478,  0.51949391,  0.46480889,  0.3432628 ],
           [-1.13285389, -0.21865636,  0.2157348 , -0.33427615]])




```python
for i, j in enumerate(a.T):
    a = j

```




    array([-0.05024657,  0.3432628 , -0.33427615])




```python
a = np.random.randn(3, 5)
a.argmax(axis=1)
```




    array([4, 4, 4])




```python
w = [
        np.random.normal(size=(4, 1)) for i in range(3)
    ]
w = np.concatenate(tuple(w), axis=1)
w_coef = np.random.normal(
    1, 0.2, size=(2, 3)
)
x = w.dot(w_coef.transpose())
```


```python
np.dot(np.random.randn(3,).transpose(), np.random.randn(3, 2)).transpose()
```




    array([4.07799799, 0.88329315])




```python
np.eye(2)[x.argmax(axis=1)]
```




    array([[1., 0.],
           [1., 0.],
           [0., 1.],
           [0., 1.]])




```python
x
```




    array([[-1.85906821, -1.32622782, -1.19364618, -1.53628564],
           [-1.81711645, -1.8115858 , -1.55145149, -1.7550974 ],
           [-0.85039268, -0.28857254, -0.35973296, -0.64413723],
           [-1.45962002, -1.51957314, -1.2924692 , -1.43797645]])




```python
nn.Softmax(dim=1)(a).argmax(dim=1).view(3, -1)
```




    tensor([[0],
            [0],
            [0]])




```python
a.transpose().shape
```




    (4, 3)




```python
a = torch.randn(3, 2)
b = torch.randn(3, 2)
torch.sum(a * b, dim=1).unsqueeze(1).shape
```




    torch.Size([3, 1])




```python
pi = torch.randn(3, 2)
pi_ = torch.exp(pi)
pi_
```




    tensor([[0.8265, 0.9079],
            [0.5721, 5.1133],
            [2.0180, 0.2811]])




```python
pi_ = pi_ / torch.sum(pi_, dim=1).unsqueeze(1).expand_as(pi_)
pi_
```




    tensor([[0.4765, 0.5235],
            [0.1006, 0.8994],
            [0.8777, 0.1223]])




```python
mu = torch.randn(3, 2, 3)
sigma = torch.abs(torch.randn(3, 2, 3))
```


```python
mix = torch.distributions.Categorical(pi_)
comp = torch.distributions.Independent(torch.distributions.Normal(mu, sigma), 1)
gmm = torch.distributions.MixtureSameFamily(mix, comp)
```


```python
d = gmm.sample()
d
```




    tensor([[ 0.1713, -1.5641, -0.1730],
            [-2.0129,  0.8305, -1.4073],
            [-0.1666, -0.4928,  0.3084]])




```python
d.view(-1, 3)
```




    tensor([[ 0.1713, -1.5641, -0.1730],
            [-2.0129,  0.8305, -1.4073],
            [-0.1666, -0.4928,  0.3084]])




```python
a = torch.tensor([1, 2, 3, 4])
b = torch.zeros((4, 4))
torch.eye(4, 4)[1]
```




    tensor([0., 1., 0., 0.])




```python
f = torch.rand(3, 1)
```


```python
g = f.clone().detach()
g
```




    tensor([[0.6147],
            [0.0253],
            [0.5778]])




```python

for i in range(5):
    f = torch.rand(3, 1)
    g = torch.concat((f, g), dim=0)
g

```




    tensor([[0.1551],
            [0.1150],
            [0.5389],
            [0.4203],
            [0.2498],
            [0.2340],
            [0.3530],
            [0.0219],
            [0.5971],
            [0.1908],
            [0.9878],
            [0.4121],
            [0.1878],
            [0.9655],
            [0.9310],
            [0.6147],
            [0.0253],
            [0.5778]])




```python
g.shape
```




    torch.Size([18, 1])




```python
d = g.view(6, -1)
```


```python
g[0][0] = 1
d
```




    tensor([[1.0000, 0.1150, 0.5389],
            [0.4203, 0.2498, 0.2340],
            [0.3530, 0.0219, 0.5971],
            [0.1908, 0.9878, 0.4121],
            [0.1878, 0.9655, 0.9310],
            [0.6147, 0.0253, 0.5778]])




```python
a = torch.randn(6, 2)
b = torch.randn(6, 2)
c = a * b
d = torch.sum(c, dim=1)
```


```python
a = torch.randn(3, 2)
b = a.unsqueeze(dim=1)
b
```




    tensor([[[-2.1470,  0.4693]],
    
            [[-0.7005, -2.2612]],
    
            [[-0.2056, -0.6673]]])




```python
a = torch.randn(3, 4)
a
```




    tensor([[ 2.0146,  1.1581, -0.6280,  1.2848],
            [ 0.0081, -1.2483,  0.0801,  0.6994],
            [ 1.7687, -0.8702, -0.4849,  0.2106]])




```python
x = torch.randn(3, 1)
z = torch.zeros(3, 1)
g = torch.cat((x, z), dim=1)
g.requires_grad = True
# x.requires_grad = True
# z.requires_grad = True
l = torch.mean(-torch.square(g))
l.backward()
```


```python
g[:,1:]
```




    tensor([[0.],
            [0.],
            [0.]], grad_fn=<SliceBackward0>)




```python
def t(x):
    return x ** 2 + 2
a = torch.ones(3, 2, requires_grad=True)
b = torch.zeros(3, 1)
c = torch.cat((a, b), dim=1)
z = torch.sum(t(c) - torch.zeros_like(c))
z.backward()
```


```python

```




    tensor(24., grad_fn=<SumBackward0>)




```python
c = torch.randn(3, 2, 2)
c
```




    tensor([[[-1.2056, -0.2942],
             [ 0.0869, -0.6678]],
    
            [[-0.8665, -0.3370],
             [-0.3098, -0.9897]],
    
            [[-1.2215,  1.1381],
             [ 0.5559,  2.0055]]])




```python
d = torch.randn(3, 2).unsqueeze(dim=2)
e = d.expand_as(c)
e[1]
```




    tensor([[ 2.0886,  2.0886],
            [-1.7030, -1.7030]])




```python
g = torch.sum(c * e, dim=1)
```




    tensor([[-1.1951, -0.0179],
            [-1.2823,  0.9815],
            [ 1.0219, -4.3168]])




```python
kk = torch.distributions.Normal(torch.tensor([1.0]), torch.tensor([1.0]))
```


```python
torch.normal(torch.randn(3, 5), torch.abs(torch.randn(3, 5))).shape
```




    torch.Size([3, 5])




```python
a = torch.randn(1, 2, 4)
a
```




    tensor([[[-1.4792, -0.2155,  0.3315, -0.2551],
             [ 0.0538, -1.3072,  0.7378,  0.1132]]])




```python
a.squeeze(dim=0).shape
```




    torch.Size([2, 4])




```python
torch.tensor([1, 2, 3]).unsqueeze(0).repeat(5, 1).shape
```




    torch.Size([5, 3])




```python
torch.mean(torch.log(torch.abs(g)), dim=0)
```




    tensor([ 0.1495, -0.8602])




```python
torch.cat((a, b), dim=0)
```




    tensor([[ 0.5812, -0.7614],
            [ 0.9483,  0.3945],
            [-1.3171,  0.0244],
            [-0.7375,  0.4646],
            [ 0.9193,  0.7448],
            [-0.2335,  0.8908],
            [ 0.7415,  0.5046],
            [ 0.5779,  1.0458],
            [ 1.1320, -0.6056],
            [ 0.8067, -0.9804],
            [ 2.1514, -0.6210],
            [ 0.2589, -1.3588]])




```python
f = torch.exp(d.view(3, -1))
```


```python
f / torch.sum(f, dim=1).unsqueeze(1)
```




    tensor([[0.2862, 0.7138],
            [0.3881, 0.6119],
            [0.9419, 0.0581]])




```python
g / h
```




    tensor([[0.2862, 0.7138],
            [0.3881, 0.6119],
            [0.9419, 0.0581]])




```python
for i in range(1, 5):
    print(i)
```

    1
    2
    3
    4



```python
mu = torch.randn((3, 1))
mu = mu.repeat(2, 1)
mu
```




    tensor([[0.5439],
            [0.0771],
            [0.2039],
            [0.5439],
            [0.0771],
            [0.2039]])




```python
mu.shape
```




    torch.Size([6, 1])




```python
c = mu.view(3, -1)
c.shape
```




    torch.Size([3, 2])




```python
a = pd.DataFrame({'a': [1, 2, 3],
     'b': [4, 5, 6]})
```


```python
(a['b'].value_counts() / a.shape[0]).values
```




    array([0.33333333, 0.33333333, 0.33333333])




```python
a['a'] = 1
a
```




<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }

    .dataframe tbody tr th {
        vertical-align: top;
    }

    .dataframe thead th {
        text-align: right;
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>a</th>
      <th>b</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>1</td>
      <td>4</td>
    </tr>
    <tr>
      <th>1</th>
      <td>1</td>
      <td>5</td>
    </tr>
    <tr>
      <th>2</th>
      <td>1</td>
      <td>6</td>
    </tr>
  </tbody>
</table>
</div>




```python
torch.tensor(a['a'].values)
```




    tensor([1, 2, 3])




```python
c = torch.tensor([1, 1, 1])
d = torch.tensor([2, 2, 2])
e = torch.cat((c, d), dim=0)
c[:] = 3
e
c
```




    tensor([3, 3, 3])




```python
c
```




    2




```python
x = torch.randn((3, 3))
```


```python
x1 = x.unsqueeze(dim=1).expand_as(mu)
x1.size()
```




    torch.Size([3, 2, 3])




```python

```




    tensor([[[-0.4188, -0.3362, -0.3925],
             [-0.4188, -0.3362, -0.3925]],
    
            [[ 1.1694,  0.1003,  0.2921],
             [ 1.1694,  0.1003,  0.2921]],
    
            [[-1.4250, -0.2158,  1.0102],
             [-1.4250, -0.2158,  1.0102]]])



bbf3e757c51e00e0934fab77ac7fc8561f32835e
ipynb
Jupyter Notebook
trivial.ipynb
DataCanvasIO/YLearn
d65b5afb83deed154c710de9096317165d95014a
Apache-2.0
2022-03-28T07:41:28.000Z
2022-03-29T06:24:52.000Z
trivial.ipynb
DataCanvasIO/YLearn
d65b5afb83deed154c710de9096317165d95014a
Apache-2.0
trivial.ipynb
DataCanvasIO/YLearn
d65b5afb83deed154c710de9096317165d95014a
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES