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)]]))
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)
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)
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)