Exemplo n.º 1
0
def load_model(kind, model_fdir):
    """
    Get FCM model from the model file
    :param kind:
    :param model_fdir:
    :return:
    """
    df = pd.read_csv(model_fdir,
                     delimiter='\t',
                     names=['property_uri', 'kind', 'features'])
    dfkind = df[df.kind == kind]
    # print(dfkind.columns.values)
    centroids = []
    #centroids_names = list(df['property_uri'])
    centroids_names = []
    # centroids_names = [p for p in df['property_uri']]
    for idx, row in dfkind.iterrows():
        #for r in dfkind['features']:
        # print(r)
        # print(type(r))

        r = row['features']
        centroids_names.append(row['property_uri'])
        centroid = [float(num) for num in r.split(',')]
        centroids.append(centroid)

    # This is to fix for categorical
    max_num_features = 0
    if kind == commons.CATEGORICAL:  # has variant num of features
        for c in centroids:
            dim = len(c)
            if dim > max_num_features:
                max_num_features = dim

        for c in centroids:
            num_feats = len(c)
            if num_feats < max_num_features:
                additionals = max_num_features - num_feats
                for i in range(additionals):
                    c.append(0)
        logger.debug("for categorical max_num_features: " +
                     str(max_num_features))
    if len(centroids) == 0:
        return None, None, None
    fcm = FCM(n_clusters=len(centroids))
    # print("centroids: "+str(centroids))
    # print("len: "+str(len(centroids)))
    fcm.fit(centroids, range(len(centroids)))
    logger.debug("centroids: " + str(centroids))
    logger.debug("fcm centroids: " + str(fcm.cluster_centers_))
    logger.debug("membership: ")
    logger.debug("\n" + str(fcm.u))
    return fcm, centroids_names, max_num_features
Exemplo n.º 2
0
def example():
    X = np.array([[1, 1], [1, 2], [2, 2], [9, 10], [10, 10], [10, 9], [9, 9],
                  [20, 20]])
    fcm = FCM(n_clusters=3)
    fcm.fit(X, [0, 0, 0, 1, 1, 1, 1, 2])
    # fcm.fit(X)
    testing_data = np.array([[0, 1.9], [5, 3], [4, 4], [8, 9], [9.5, 6.5],
                             [5, 5], [15, 15], [12, 12], [14, 14], [19, 10]])
    predicted_membership = fcm.predict(testing_data)
    print("\n\ntesting data")
    print(testing_data)
    print("predicted membership")
    print(predicted_membership)
    print("\n\n")
    draw_model_2d(fcm, data=testing_data, membership=predicted_membership)
Exemplo n.º 3
0
def test_2d():
    X = np.array([[1, 1], [1, 2], [2, 2], [9, 10], [10, 10], [10, 9], [9, 9]])
    fcm = FCM()
    fcm.fit(X, [0, 0, 0, 1, 1, 1, 1])
    assert len(fcm.cluster_centers_) == 2
    assert fcm.cluster_centers_[0][0] == pytest.approx(1.33333333, 0.1)
    assert fcm.cluster_centers_[0][1] == pytest.approx(1.66666667, 0.1)
    testing_data = np.array([[0, 1.9], [3, 3], [4, 4], [8, 9], [9.5, 6.5]])
    predicted_membership = fcm.predict(testing_data)
    actual_membership = np.array([[0.98777232, 0.01222768],
                                  [0.94884591, 0.05115409],
                                  [0.82813688, 0.17186312],
                                  [0.02482074, 0.97517926],
                                  [0.0908581, 0.9091419]])
    assert predicted_membership == pytest.approx(actual_membership, 0.01)
Exemplo n.º 4
0
def example():
    X = np.array([[1, 1], [1, 2], [2, 2], [9, 10], [10, 10], [10, 9], [9, 9],
                  [20, 20]])
    fcm = FCM(n_clusters=3)
    fcm.set_logger(tostdout=True, level=logging.DEBUG)
    fcm.fit(X, [0, 0, 0, 1, 1, 1, 1, 2])
    # fcm.fit(X)
    testing_data = np.array([[0, 1.9], [5, 3], [4, 4], [8, 9], [9.5, 6.5],
                             [5, 5], [15, 15], [12, 12], [14, 14], [19, 10]])
    predicted_membership = fcm.predict(testing_data)
    print "\n\ntesting data"
    print testing_data
    print "predicted membership"
    print predicted_membership
    print "\n\n"
    draw_model_2d(fcm, data=testing_data, membership=predicted_membership)
Exemplo n.º 5
0
def fit_from_centroids():
    data_test = [178.75, 5.97390157]
    data_height = [170., 186., 182., 177.]
    data_size = [40, 42, 45, 47, 50]
    data_injuries = [3, 4, 1, 0]
    centers = [[175., 4.08], [44.8, 3.54], [2., 1.58]]
    fcm = FCM(n_clusters=3, max_iter=1)
    fcm.cluster_centers_ = np.array(centers)
    fcm.fit(fcm.cluster_centers_, [0, 1, 2])
    membership_height = fcm.predict(np.array([data_test]))
    membership_height = fcm.predict(
        np.array([[np.average(data_height),
                   np.std(data_height)]]))
    membership_size = fcm.predict(
        np.array([[np.average(data_size),
                   np.std(data_size)]]))
    membership_injuries = fcm.predict(
        np.array([[np.average(data_injuries),
                   np.std(data_injuries)]]))
Exemplo n.º 6
0
def load_mlmodel_into_fcm(model):
    """
    :param model: MLModel
    :param data: matrix input as np array
    :return: FCM model
    """
    num_of_cols = len(model.cluster_set.all())
    centers = []
    for clus_model in model.cluster_set.all():
        logger.debug("load_mlmodel_into_fcm> getting clus %s" %
                     clus_model.name)
        p = clus_model.center.split(',')
        centers.append(p)
    centers_np = np.array(centers, dtype='f')
    # logger.debug("Centers: ")
    # logger.debug(centers_np)
    fcm = FCM(n_clusters=num_of_cols, max_iter=1, logger=logger)
    fcm.cluster_centers_ = centers_np
    data = centers_np
    logger.debug("load_mlmodel_into_fcm> will fit the data with %d clusters" %
                 num_of_cols)
    fcm.fit(data, range(num_of_cols))
    logger.info("generated the FCM model from the MLModel")
    return fcm
Exemplo n.º 7
0
def single_known_cluster():
    X = np.array([[1, 1], [1, 2], [2, 2]])
    fcm = FCM(n_clusters=3, max_iter=1)
    fcm.fit(X, [0, 0, 0])
Exemplo n.º 8
0
def example_single_known():
    X = np.array([[1, 1], [1, 2], [2, 2], [0, 0], [0, 0]])
    fcm = FCM(n_clusters=3, max_iter=1)
    fcm.fit(X, [0, 0, 0, 1, 2])
    draw_model_2d(fcm, data=X, membership=fcm.u)
    print fcm.u