Ejemplo n.º 1
0
def main():
    args = kmeans_args()
    rand = np.random.randint(10000000)
    data_train, data_test = pre_processed_data(args, rand)
    label_train, label_test = pre_processed_label(args, rand)
    print('data loaded')
    switch_choice = {'k': lambda: 'k-means++', 'r': lambda: 'random',
                     'm': lambda: mean_image(label_train, data_train)}
    kmeans = KMeans(n_clusters=10, random_state=0,
                    init=switch_choice[args.init]()) \
            .fit(extract_col(data_train, args.columns))
    predicted = kmeans.predict(extract_col(data_test, args.columns))
    print('kmeans done')
    compare_class(predicted, label_test)
    if args.create_mean:
            create_images_from_rows('km', mean_image(predicted, data_test))
Ejemplo n.º 2
0
def main():
    args = arg.preprocess_args()
    rand = random.randint(0, 9999999)
    data, _ = helper.pre_processed_data(args, rand, dry=False)
    #  data = data.reshape(-1,48,48)
    if args.mean:
        label, _ = helper.pre_processed_label(args, rand, dry=False)
        data = preprocess.mean_image(label, data)
    elif args.split is not None or args.randomize:
        label, _ = helper.pre_processed_label(args, rand, dry=False)
        helper.write_data_to_file(
            (args.folder or helper.FOLDER) + (args.name or 'processed_data') +
            '_l' + helper.EXT,
            label,
            fmt='%d',
            h='0')
    # for i in range(4):
    #     for j in range(4):
    helper.write_data_to_file(
        (args.folder or helper.FOLDER) +
        (args.name or 'processed_data')  # + str(i) + str(j)
        + helper.EXT,
        data,
        #  [k[i*12:(i*12)+12,j*12:(j*12)+12].flatten() for k in data],
        h=', '.join(str(i) for i in range(len(data[0]))))
Ejemplo n.º 3
0
def spectralclustering(data_train, data_test, label_train, label_test, args):
    print('spectral clustering')
    SC = SpectralClustering(n_clusters=10, affinity='nearest_neighbors').fit(data_train)
    predict = SC.fit_predict(data_test)
    print('spectral clustering done')
    compare_class(predict, label_test)
    if args.create_mean:
        create_images_from_rows('SC', mean_image(SC.labels_, data_test))
Ejemplo n.º 4
0
def birch(data_train, data_test, label_train, label_test, args):
    print('birch')
    birch = Birch(n_clusters=10).fit(data_train)
    predict = birch.predict(data_test)
    print('birch done')
    compare_class(predict, label_test)
    if args.create_mean:
        create_images_from_rows('bi', mean_image(predict, data_test))
Ejemplo n.º 5
0
def main():
    args = arg.preprocess_args()
    rand = 0
    data, _ = helper.pre_processed_data_all(args, rand, dry=False)
    if args.mean:
        rand = random.randint(0, 9999999)
        label, _ = helper.pre_processed_label_all(args, rand, dry=False)
        data = preprocess.mean_image(label, data)
    helper.create_images_from_rows((args.name or 'img'), data)
Ejemplo n.º 6
0
def featureagglomeration(data_train, data_test, label_train, label_test, args):
    print('feature agglomeration')
    FA = FeatureAgglomeration(n_clusters=10).fit(data_train)
    transformation = FA.transform(data_test)
    agglomeration = find_highest(transformation)
    print('feature agglomeration done')
    compare_class(agglomeration, label_test)
    if args.create_mean:
        create_images_from_rows('fa', mean_image(agglomeration, data_test))
Ejemplo n.º 7
0
def minibatchkmeans(data_train, data_test, label_train, label_test, args):
    print('mini batch kmeans')
    switch_choice = {'k': lambda: 'k-means++', 'r': lambda: 'random',
                     'm': lambda: mean_image(label_train, data_train)}
    MKMeans = MiniBatchKMeans(n_clusters=10, init=switch_choice[args.init](), n_init=10).fit(data_train)
    predict = MKMeans.predict(data_test)
    print('mini batch kmeans done')
    compare_class(predict, label_test)
    if args.create_mean:
        create_images_from_rows('mbkm', MKMeans.cluster_centers_)
Ejemplo n.º 8
0
def kmeans(data_train, data_test, label_train, label_test, args):
    print('kmeans')
    switch_choice = {'k': lambda: 'k-means++', 'r': lambda: 'random',
                     'm': lambda: mean_image(label_train, data_train)}
    kmeans = KMeans(n_clusters=10, random_state=0, n_init=10,
                    init=switch_choice[args.init]()).fit(data_train)    
    predicted = kmeans.predict(data_test)
    print('kmeans done')
    compare_class(predicted, label_test)
    if args.create_mean:
        create_images_from_rows('km', kmeans.cluster_centers_)
Ejemplo n.º 9
0
def bayes(name_nb, fn_label, data_train, data_test, cm, col):
    label_train, label_test = fn_label()
    print('using all class')
    print('****%s****' % NB[name_nb]['name'])
    res = []
    res.append(
        sk_bayes(NB[name_nb]['fn'], data_train, label_train, data_test,
                 label_test, col))
    for i in range(10):
        print('=======')
        label_train, label_test = fn_label(sep=SEP, i=i)
        print('class %d' % i)
        res.append(
            sk_bayes(NB[name_nb]['fn'], data_train, label_train, data_test,
                     label_test, col))
    if cm:
        for i in range(len(res)):
            create_images_from_rows('%d_b' % i,
                                    mean_image(res[i][1], res[i][0]))
Ejemplo n.º 10
0
def main():
    args = arg.preprocess_args()
    rand = random.randint(0, 9999999)
    data, _ = helper.pre_processed_data_all(args, rand, dry=False)
    if args.mean:
        label, _ = helper.pre_processed_label_all(args, rand, dry=False)
        data = preprocess.mean_image(label, data)
    elif args.split is not None or args.randomize:
        label, _ = helper.pre_processed_label(args, rand, dry=False)
        helper.write_data_to_file(
            (args.folder or helper.FOLDER) + (args.name or 'processed_data') +
            '_l' + helper.EXT,
            label,
            fmt='%d',
            h='0')
    # for i in range(4):
    #     for j in range(4):
    helper.write_data_to_file((args.folder or helper.FOLDER) +
                              (args.name or 'processed_data') + helper.EXT,
                              data,
                              h=', '.join(str(i) for i in range(len(data[0]))))