示例#1
0
def main():
    fnm = 'prob5.data'
    patterns = md.read_data(fnm).T
    patterns = km.augment_patterns(patterns)
    k = 4 # Number of clusters. Figure out where to put this
    current_centers = km.init_cluster_centers(patterns, k)

    iteration_count = 0
    errors = np.matrix((1, k))
    while True:
        cluster_assignments = km.cluster_patterns(
            patterns, current_centers)
        patterns[0,] = cluster_assignments
        km.plot_patterns_and_centers(patterns, current_centers)
        new_means = km.calculate_new_means(patterns, k)
        mean_diff = current_centers[1:,1:] - new_means[1:,1:]
        errors = (mean_diff.T * mean_diff).diagonal()
        iteration_count += 1
        if (errors > .1).any():
            current_centers = new_means
        else:
            break
    print(current_centers[0,:])
    print(errors)
    print('Converged in', iteration_count,'iterations.')
示例#2
0
def main():
    fnm = 'prob3.data'
    data = md.read_data(fnm)
    D1 = data[0:8,].T
    D2 = data[8:,].T

    u1 = np.matrix((np.mean(D1[0,:]), np.mean(D1[1,:]))).T
    u2 = np.matrix((np.mean(D2[0,:]), np.mean(D2[1,:]))).T

    sigma1 = np.asmatrix(np.cov(D1, bias=1))
    sigma2 = np.asmatrix(np.cov(D1, bias=1))

    g1 = discrim_func(u1, sigma1)
    g2 = discrim_func(u2, sigma2)

    steps = 100
    x = np.linspace(-2,2,steps)
    y = np.linspace(-6,6,steps)

    X,Y = np.meshgrid(x,y)
    z = [g1(X[r,c], Y[r,c]) - g2(X[r,c], Y[r,c])
         for r in range(0,steps) for c in range(0,steps)]
    Z = np.array(z)
    px = X.ravel()
    py = Y.ravel()
    pz = Z.ravel()
    gridsize = 50
    plot = plt.subplot(111)
    plt.hexbin(px,py,C=pz, gridsize=gridsize, cmap=cm.jet, bins=None)
    cb = plt.colorbar()
    cb.set_label('g1 minus g2')
    return plot
示例#3
0
def main():
    p1a_fnm = 'prob1a.data'
    p1b_fnm = 'prob1b.data'

    # Separate omega 1 and 2
    p1a_data = md.read_data(p1a_fnm)
    (tc, w) = bp.separate(p1a_data)
    print("Separated omega 1 and omega 2 in", tc, "iterations")
    bp.plot_solution(w, p1a_data, "Problem 1a",
                     dict(mincat="Omega 1", maxcat="Omega 2"))

    # Separate omega 2 and 3
    p1b_data = md.read_data(p1b_fnm)
    (tc, w) = bp.separate(p1b_data)
    print("Separated omega 2 and omega 3 in", tc, "iterations")
    bp.plot_solution(w, p1b_data, "Problem 1b",
                     dict(mincat="Omega 2", maxcat="Omega 3"))
示例#4
0
def main():
    p1a_fnm = 'prob1a.data'
    p1b_fnm = 'prob1b.data'

    # Separate omega 1 and 2
    p1a_data = md.read_data(p1a_fnm)
    (tc, w) = bp.separate(p1a_data)
    print("Separated omega 1 and omega 2 in", tc, "iterations")
    bp.plot_solution(w, p1a_data, "Problem 1a",
                     dict(mincat="Omega 1", maxcat="Omega 2"))

    # Separate omega 2 and 3
    p1b_data = md.read_data(p1b_fnm)
    (tc, w) = bp.separate(p1b_data)
    print("Separated omega 2 and omega 3 in", tc, "iterations")
    bp.plot_solution(w, p1b_data, "Problem 1b",
                     dict(mincat="Omega 2", maxcat="Omega 3"))
示例#5
0
def make_data():
    datax, datay = read_data('data.xlsx')
    #[]变为([]) 将序列型对象转化为数组
    datax = np.array(datax)
    datay = np.array(datay)
    random_index = random.sample(list(range(datax.shape[0])), datax.shape[0])
    #shape[0]数组行数,生成指定列表            从0到行数的列表,list将元组转化为列表
    train_size = int(datax.shape[0] * 0.6)#转化为整型
    train_index = random_index[:train_size] #索引范围
    test_index = list(set(random_index).difference(set(train_index)))
    train_feature = datax[train_index]
    train_label = datay[train_index]
    test_feature = datax[test_index]
    test_label = datay[test_index]
    return train_feature, train_label, test_feature, test_label
示例#6
0
def main():
    fnm = 'p4.data'
    # Read the data into a one-dimensional array
    data = np.array(md.read_data(fnm)).squeeze()
    h = 1 # Window size

    # Create plots for the zero-one window function
    phi = pw.zero_one(h)
    print("Zero-one window function...")
    plot_data(phi, data, "Zero-One Window", "images/zero-one.eps")

    # Create plots for the Gauss window function
    phi =  pw.gauss(h)
    print("Gauss window function...")
    plot_data(phi, data, "Gauss Window", "images/gauss.eps")
示例#7
0
def main():
    training_data = md.circle_data(40)
    training_data = md.convert_data(training_data)

    # Make a test data set
    test_data = md.read_data('homework1_data.csv')
    test_data = md.convert_data(test_data)

    alpha = 0.5
    theta = .0001
    iter_limit = 50000
    mynet = nn.NeuralNet((2, 2, 1), sf.bipolar, sf.bipolar_prime, alpha)
    n = training_data.shape[0]
    total_count = 0
    k = -1
    plot_data = np.matrix("0 0")
    fh = open('nn_quad.csv', 'w')
    while True:
        total_count += 1
        k = (k + 1) % n
        #k = np.random.randint(0, n)
        pattern = np.asarray(training_data[k, 1:]).squeeze()
        teacher = [training_data[k, 0]]
        error = mynet.train(pattern, teacher, theta)
        if (total_count % 10) == 0:
            plot_data = np.vstack((plot_data, np.matrix((total_count, error))))
            msg = ','.join([str(total_count), str(error)])
            fh.write(msg)
            fh.write('\n')
        if (total_count > iter_limit) or (error < theta):
            print("Stopped at iter count ", total_count,
                  "with a final error of ", error)
            break

    mynet.dt.tofile('bub_quad.txt')
    fh.close()
    plt.plot(plot_data[0:, 0], plot_data[0:, 1])
    plt.show()

    counter = check_net(test_data, mynet.fwd)
    print('Assigned', counter, 'patterns of', test_data.shape[0], 'correctly.')
示例#8
0
文件: nn_quad.py 项目: nail82/bpnn
def main():
    training_data = md.circle_data(40)
    training_data = md.convert_data(training_data)

    # Make a test data set
    test_data = md.read_data('homework1_data.csv')
    test_data = md.convert_data(test_data)

    alpha = 0.5
    theta = .0001
    iter_limit = 50000
    mynet = nn.NeuralNet((2,2,1), sf.bipolar, sf.bipolar_prime, alpha)
    n = training_data.shape[0]
    total_count = 0
    k = -1
    plot_data = np.matrix("0 0")
    fh = open('nn_quad.csv', 'w')
    while True:
        total_count += 1
        k = (k+1) % n
        #k = np.random.randint(0, n)
        pattern = np.asarray(training_data[k,1:]).squeeze()
        teacher = [training_data[k,0]]
        error = mynet.train(pattern, teacher, theta)
        if (total_count % 10) == 0:
            plot_data = np.vstack((plot_data, np.matrix((total_count, error))))
            msg = ','.join([str(total_count), str(error)])
            fh.write(msg)
            fh.write('\n')
        if (total_count > iter_limit) or (error < theta):
            print("Stopped at iter count ",
                  total_count, "with a final error of ", error)
            break

    mynet.dt.tofile('bub_quad.txt')
    fh.close()
    plt.plot(plot_data[0:,0], plot_data[0:,1])
    plt.show()

    counter = check_net(test_data, mynet.fwd)
    print('Assigned', counter, 'patterns of', test_data.shape[0],'correctly.')
示例#9
0
def main():
    fnm = 'prob5.data'
    patterns = md.read_data(fnm).T
    patterns = km.augment_patterns(patterns)
    k = 4  # Number of clusters. Figure out where to put this
    current_centers = km.init_cluster_centers(patterns, k)

    iteration_count = 0
    errors = np.matrix((1, k))
    while True:
        cluster_assignments = km.cluster_patterns(patterns, current_centers)
        patterns[0, ] = cluster_assignments
        km.plot_patterns_and_centers(patterns, current_centers)
        new_means = km.calculate_new_means(patterns, k)
        mean_diff = current_centers[1:, 1:] - new_means[1:, 1:]
        errors = (mean_diff.T * mean_diff).diagonal()
        iteration_count += 1
        if (errors > .1).any():
            current_centers = new_means
        else:
            break
    print(current_centers[0, :])
    print(errors)
    print('Converged in', iteration_count, 'iterations.')