Ejemplo n.º 1
0
    def check_broadcast_block_correctness(block, grid_shape):
        _np_result = np.tile(block, grid_shape)

        ns_a = nps.zeros(_np_result.shape).reshape(block_shape=block.shape)
        ns_b = nps.array(block)

        _ns_result = ns_a + ns_b

        assert np.allclose(_np_result, _ns_result.get())
def test_trace(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    a: BlockArray = nps.array([1.0, 2.0, 3.0, 4.0])

    # Construct diagonal matrix with nums and numpy.
    a_diag = nps.diag(a)
    a_diag_np = np.diag(a.get())

    # Apply trace to diagonal matrices.
    a_diag_trace = nps.trace(a_diag).get()
    a_diag_np_trace = np.trace(a_diag_np)

    assert np.allclose(a_diag_trace, a_diag_np_trace)

    # Test pre-defined diagonal matrices.
    b: BlockArray = nps.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0],
                               [7.0, 8.0, 9.0]])

    b_diag_trace = nps.trace(b).get()
    b_diag_np_trace = np.trace(b.get())

    assert np.allclose(b_diag_trace, b_diag_np_trace)

    # Test that trace raises on arrays with 3+ axes.
    mat: BlockArray = nps.zeros((2, 3, 2))
    with pytest.raises(ValueError):
        nps.trace(mat)

    # Test that trace raises when called with non-zero offset.
    mat: BlockArray = nps.array([1.0, 2.0, 3.0, 4.0])
    mat_diag = nps.diag(mat)
    with pytest.raises(NotImplementedError):
        nps.trace(mat_diag, offset=2)

    # Test data type of trace.
    mat_diag = nps.diag(nps.array([1.01, 2.02, 3.03, 4.04]))
    mat_diag_np = np.diag(np.array([1.01, 2.02, 3.03, 4.04]))
    mat_diag_trace = nps.trace(mat_diag, dtype=int).get()
    mat_diag_np_trace = np.trace(mat_diag_np, dtype=int)

    assert np.allclose(mat_diag_trace, mat_diag_np_trace)
    assert mat_diag_trace.dtype == int

    # Test trace on non-square matrices
    ba: BlockArray = nps.array(np.full((10, 12), 1))
    ba = ba.reshape(block_shape=(3, 4))
    np_arr = ba.get()
    ba = nps.trace(ba)
    np_arr = np.trace(np_arr)

    assert np.allclose(ba.get(), np_arr)
def test_basic_creation(nps_app_inst):
    import nums.numpy as nps

    assert nps_app_inst is not None

    ops = "empty", "zeros", "ones"
    shape = (2, 3, 4)
    for op in ops:
        ba: BlockArray = nps.__getattribute__(op)(shape=shape)
        if "zeros" in op:
            assert nps.allclose(nps.zeros(shape), ba)
        if "ones" in op:
            assert nps.allclose(nps.ones(shape), ba)

        ba2: BlockArray = nps.__getattribute__(op + "_like")(ba)
        assert ba.shape == ba2.shape
        assert ba.dtype == ba2.dtype
        assert ba.block_shape == ba2.block_shape
Ejemplo n.º 4
0
Archivo: pmf.py Proyecto: Ant1ng2/nums
    def __init__(self, train_size=0.75, lambda_U = 0.3, lambda_V = 0.3):

        self._app = _instance()

        self.n_dims = 5
        self.parameters = {}

        self.lambda_U = lambda_U
        self.lambda_V = lambda_V
        self.n_users = 10
        self.n_movies = 10
       
        self.train_set = nps.random.randn_sparse(10, 10) 
        self.test_set = nps.random.randn_sparse(3, 3) 

        self.R = self.train_set
        self.U = nps.zeros((self.n_dims, self.n_users), dtype=np.float64)
        self.V = nps.random.randn(self.n_dims, self.n_movies)
Ejemplo n.º 5
0
def sample(app: ArrayApplication, sample_size):
    X_train = nps.concatenate([
        nps.random.randn(sample_size // 2, 2),
        nps.random.randn(sample_size // 2, 2) + 2.0
    ],
                              axis=0)
    y_train = nps.concatenate([
        nps.zeros(shape=(sample_size // 2, ), dtype=nps.int),
        nps.ones(shape=(sample_size // 2, ), dtype=nps.int)
    ],
                              axis=0)
    # We augment X with 1s for intercept term.
    X_train = app.concatenate([
        X_train,
        app.ones(shape=(X_train.shape[0], 1),
                 block_shape=(X_train.block_shape[0], 1),
                 dtype=X_train.dtype)
    ],
                              axis=1,
                              axis_block_size=X_train.block_shape[1] + 1)
    return X_train, y_train
Ejemplo n.º 6
0
import nums
import nums.numpy as nps
from nums.models.glms import LogisticRegression

nums.init()

# Make dataset.

X1 = nps.random.randn(500, 1) + 5.0
y1 = nps.zeros(shape=(500, ), dtype=bool)

X2 = nps.random.randn(500, 1) + 10.0
y2 = nps.ones(shape=(500, ), dtype=bool)

X = nps.concatenate([X1, X2], axis=0)
y = nps.concatenate([y1, y2], axis=0)

# Train Logistic Regression Model.

model = LogisticRegression(solver="newton", tol=1e-8, max_iter=1)

model.fit(X, y)
y_pred = model.predict(X)

print("accuracy", (nps.sum(y == y_pred) / X.shape[0]).get())