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
Exemple #2
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
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())