def test_multiclass_encoding(): #X, y = make_classification(n_samples=100, n_features=20, n_samples = 10 n_classes = 3 X, y = make_classification(n_samples=n_samples, n_features=10, n_informative=6, n_redundant=2, n_repeated=0, n_classes=n_classes, n_clusters_per_class=2, weights=None, flip_y=0.01, class_sep=1.0, hypercube=True, shift=0.0, scale=1.0, shuffle=True, random_state=None) rows = np.arange(len(y)) cols = y values = np.ones_like(y, dtype=np.float64) y_one_hot = sp.coo_matrix((values, (rows, cols)), shape=(n_samples, n_classes), dtype=np.float64) y_one_hot = y_one_hot.tocsr() X = sp.csr_matrix(X) X_ext, compars = multiclass_to_ranking(X, y_one_hot) fm = FMRecommender(n_iter=2000, init_stdev=0.01, l2_reg_w=.5, l2_reg_V=.5, rank=2, step_size=.002, random_state=11) X_train = X_ext.tocsc() #assert False fm.fit(X_train, compars)
def test_multiclass_to_ranking(): X = sp.csr_matrix(np.arange(6).reshape((3,2))) y = sp.csr_matrix((3, 5)) y[0, 0] = 1 y[1, [2, 3]] = 1 y[2, [0, 4]] = 1 n_classes = y.shape[1] n_samples = X.shape[0] n_features = X.shape[1] X_ext, compars = multiclass_to_ranking(X, y) assert X_ext.shape[0] == n_classes * n_samples assert X_ext.shape[1] == n_classes + n_features # test that features are replicated assert_array_equal(X_ext.tocsc()[:, n_classes:].sum(axis=0), X.sum(axis=0) * n_classes) # test class labels encoding structure assert X_ext.tocsr()[:n_classes, :].sum(axis=0)[0, 0] == n_samples assert X_ext.tocsr()[:, :n_classes].sum() == n_samples * n_classes #assert_array_equal(X_ext.tocsc()[:, n_classes:].sum(axis=0), print X_ext.todense() print y.todense() print compars
def test_multiclass_to_ranking(): X = sp.csr_matrix(np.arange(6).reshape((3, 2))) y = sp.csr_matrix((3, 5)) y[0, 0] = 1 y[1, [2, 3]] = 1 y[2, [0, 4]] = 1 n_classes = y.shape[1] n_samples = X.shape[0] n_features = X.shape[1] X_ext, compars = multiclass_to_ranking(X, y) assert X_ext.shape[0] == n_classes * n_samples assert X_ext.shape[1] == n_classes + n_features # test that features are replicated assert_array_equal(X_ext.tocsc()[:, n_classes:].sum(axis=0), X.sum(axis=0) * n_classes) # test class labels encoding structure assert X_ext.tocsr()[:n_classes, :].sum(axis=0)[0, 0] == n_samples assert X_ext.tocsr()[:, :n_classes].sum() == n_samples * n_classes #assert_array_equal(X_ext.tocsc()[:, n_classes:].sum(axis=0), print X_ext.todense() print y.todense() print compars
def test_multiclass_encoding(): #X, y = make_classification(n_samples=100, n_features=20, n_samples = 10 n_classes = 3 X, y = make_classification(n_samples=n_samples, n_features=10, n_informative=6, n_redundant=2, n_repeated=0, n_classes=n_classes, n_clusters_per_class=2, weights=None, flip_y=0.01, class_sep=1.0, hypercube=True, shift=0.0, scale=1.0, shuffle=True, random_state=None) rows = np.arange(len(y)) cols = y values = np.ones_like(y, dtype=np.float64) y_one_hot = sp.coo_matrix((values, (rows, cols)), shape=(n_samples, n_classes), dtype=np.float64) y_one_hot = y_one_hot.tocsr() X = sp.csr_matrix(X) X_ext, compars = multiclass_to_ranking(X, y_one_hot) fm = FMRecommender(n_iter=2000, init_stdev=0.01, l2_reg_w=.5, l2_reg_V=.5, rank=2, step_size=.002, random_state=11) X_train = X_ext.tocsc() #assert False fm.fit(X_train, compars)