【问题标题】:How to draw graph in python or convert python into java and Matlab?如何在python中绘制图形或将python转换为java和Matlab?
【发布时间】:2016-01-06 15:00:34
【问题描述】:

最近我需要使用来自 UCI https://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits 的数据进行数字识别项目 我想使用来自 https://github.com/khiner/Linear-Discrimination-with-Perceptrons 的源代码。但我不想只是复制他的代码。因为我不知道如何在 python 中绘制错误图,所以我希望用 java 和 Matlab 重新编写它。有谁知道如何在 python 中绘制错误图和其他统计图,或者我在哪里可以找到用 java 和 Matlab 重写 python 的教程?

源代码:

 from optparse import OptionParser
 import random

 class Perceptron(object):
    def __init__(self, trainfile_string, testfile_string):
    train_lines = open(trainfile_string, 'r').readlines()
    test_lines = open(testfile_string, 'r').readlines()
    # these vectors map a class number to a list of its corresponding
    # train/test instances (int vectors)
    self.train_vectors = {}
    self.test_vectors = {}
    self.num_features = len(train_lines[0].strip().split(',')) - 1
    self.weights = {}

    # initialize train_vectors and test_vectors.
    # initialize weight vectors to random values in (-1, 1)
    for n in xrange(10):
        if n != 8:
            self.train_vectors[n] = []
            self.test_vectors[n] = []
            self.weights[n] = [random.uniform(-1,1) for i in xrange(self.num_features)]
            self.weights[n].append(1.0) # input bias
            for line in train_lines:
                vector = [int(x) for x in line.strip().split(',')]
                if vector[-1] in (n, 8):
                    self.train_vectors[n].append(vector)
            for line in test_lines:
                vector = [int(x) for x in line.strip().split(',')]
                if vector[-1] in (n, 8):
                    self.test_vectors[n].append(vector)

 def run(self, max_epochs, rate, over_train=False, verbose=False):
    """For each class, train until there is no more improvement
    (or accuracy is a perfect 1.0), then test the class using the
    test file"""

    for cls in self.weights.keys():
        improvement = 1
        epoch = 0
        train_accuracy = 0
        over_train_epochs = max_epochs
        print '___________%dv%d____________' % (cls, 8)
        if verbose:
            print 'Training:'
        epochs_set = False
        while (not over_train and train_accuracy < 1.0 and \
               improvement > 0.0 and epoch < max_epochs) \
               or (over_train and epoch < over_train_epochs):
            if not epochs_set and (improvement <= 0.0 or train_accuracy >= 1.0):
                # if we're overtraining, train for twice as long as normal
                over_train_epochs = 2*epoch
                epochs_set = True
                print 'over-training. improvement stopped at %d' % epoch
            epoch = epoch + 1
            self.train(cls, rate)
            (c1, i1, c2, i2) = self.test(cls, train=True)
            (old_accuracy, train_accuracy) = (train_accuracy, float(c1 + c2)/float(c1+ c2 + i1 + i2))
            improvement = train_accuracy - old_accuracy
            if verbose:
                print 'Epoch %d, accuracy: %f, improvement: %f\n\n%s' % \
                (epoch, train_accuracy, improvement, confusionMatrix(cls, c1, i1, c2, i2))
        # done training.  now test.
        (c1, i1, c2, i2) = self.test(cls, train=False)
        test_accuracy = float(c1 + c2)/float(c1+ c2 + i1 + i2)
        print "Epochs: %d\nTraining accuracy: %f\nTest accuracy: %f\n\n%s" \
        % (epoch, train_accuracy, test_accuracy, confusionMatrix(cls, c1, i1, c2, i2))

def train(self, cls, learning_rate):
    """Train perceptron to differentiate between cls and 8,
    and return the trained weights weights"""

    for vector in self.train_vectors[cls]:
        (o, t) = self.getOandT(vector, cls)
        if o == None or t == None:
            continue
        # adjust the weights (+1 for bias)
        for i in xrange(self.num_features + 1):
            self.weights[cls][i] += learning_rate*(t - o)*vector[i]

def test(self, cls, train):
    """Use the provided test data to test the trained weights
    for a given class number vs. 8.
    Returns a tuple representing a confusion matrix
    eg: (8-correct, 8-incorrect, cls-correct, cls-incorrect)."""

    if train:
        vectors = self.train_vectors[cls]
    else:
        vectors = self.test_vectors[cls]

    c1 = 0; # num examples classified correctly for 8
    i1 = 0; # ''  ''       ''         incorrectly for 8            
    c2 = 0; # ''  ''       ''         correctly for cls
    i2 = 0; # ''  ''       ''         incorrectly for cls
    for vector in vectors:
        (o, t) = self.getOandT(vector, cls)
        if o == None or t == None:
            continue
        elif o == 1 and t == 1:
            c1 = c1 + 1
        elif o == -1 and t == -1:
            c2 = c2 + 1
        elif o == -1 and t == 1:
            i1 = i1 + 1                
        elif o == 1 and t == -1:
            i2 = i2 + 1

    return (c1, i1, c2, i2)

def getOandT(self, vector, cls):
    """Returns a tuple of o and t values, comparing cls to 8
    -1 = cls, 1 = 8
    None = provided instance vector is not 8 or the provided class (cls)"""    
    if vector[-1] == 8:
        t = 1
    elif vector[-1] == cls:
        t = -1
    else:
        return (None, None)

    total = 0.0
    for i in xrange(self.num_features + 1):
        total += self.weights[cls][i]*vector[i]
    o = sgn(total)

    return (o, t)

 def confusionMatrix(cls, c1, i1, c2, i2):
return "Class\tCorrect\tIncorrect\n%d\t%d\t%d\n%d\t%d\t%d" %\
       (cls, c2, i2, 8, c1, i1)

def sgn(val):
"""Returns 1 for val > 0 and -1 for val <= 0"""
if val > 0:
    return 1
else:
    return -1

 if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-n", "--train", dest="train_file", default="data/optdigits.tra", help="file with training data. default: 'data/optdigits.tra'")
parser.add_option("-t", "--test", dest="test_file", default="data/optdigits.tes", help="file with test data. default: 'data/optdigits.tes'")
parser.add_option("-e", "--epochs", dest="max_epochs", default=30, help="maximum number of epochs.  default: 30")
parser.add_option("-r", "--rate", dest="rate", default=.2, help="learning rate.  default: 0.2")
parser.add_option("-v", "--verbose", dest="verbose", default=False, help="would you like to print more detailed output?")
parser.add_option("-o", "--over-train", dest="over_train", default=False, help="if set to true, training will go for twice as many epochs as it takes to stop improving")
(options, args) = parser.parse_args()
verbose = options.verbose
perceptron = Perceptron(options.train_file, options.test_file)
perceptron.run(max_epochs=int(options.max_epochs), \
               rate=float(options.rate), over_train=options.over_train, verbose=options.verbose)

【问题讨论】:

标签: java python matlab


【解决方案1】:

当我在 python 中编写错误栏时,我使用matplotlib 资源。 我的代码如下所示:

x = range(80)
ax.errorbar(x, array_of_data, xerr=None,yerr=array_of_error_values) 
#plots the graph, x versus y. "xerr" stands for the errorgraph for 
#horizontal errorbars, and yerr for vertical errorbars.

ax.set_title('Mean group size', fontsize = 10) #set title for axis
fig.savefig('analysis/alltreatments.png', dpi=300) #save figure

基本上我将数据存储在一个数组中。该数组的每个值已经是其他值的平均值。我计算每个元素的标准偏差和标准误差,然后从那里计算置信区间。这些被存储在置信区间数组中。

Array_data : {2, 3, 5}
Array_ConfidenceInterval: {0.3, 0.2, 0.3} #each element corresponds to Array_data elements

matplotlib 绘制两个数组。 输出看起来像这样:

【讨论】:

  • @HaoWang +1:如果你仍然使用 python,但你熟悉 matlab,matplotlib 可以让你做任何 matlab 可以做的绘图,在很多情况下,函数名是相同的,因此很容易读/写代码(对于 matlab 用户)。
猜你喜欢
  • 2011-01-04
  • 2013-08-14
  • 1970-01-01
  • 2018-10-16
  • 2022-01-25
  • 2017-03-30
  • 2016-06-08
  • 1970-01-01
  • 2016-07-06
相关资源
最近更新 更多