• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python mnist.MNIST类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中mnist.MNIST的典型用法代码示例。如果您正苦于以下问题:Python MNIST类的具体用法?Python MNIST怎么用?Python MNIST使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了MNIST类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: __init__

	def __init__(self):
		#Load MNIST datset
		mnistData = MNIST('./mnistData')
		self.imgTrain,self.lblTrain=mnistData.load_training()
		#self.imgTrainSmpl=self.imgTrain[:50000]
		self.imgTrainSmpl = [[2.5,2.4],[0.5,0.7],[2.2,2.9],[1.9,2.2],[3.1,3.0],[2.3,2.7],[2,1.6],[1,1.1],[1.5,1.6],[1.1,0.9]]
		np.seterr(all='warn')
开发者ID:PseudoAj,项目名称:NeuralNetworks,代码行数:7,代码来源:testPCSMNIST.py


示例2: importMNIST

def importMNIST(folder,resolution,classes,amount,signals):
    print 'importing MNIST data...'
    if os.path.isfile('saved_DY.pkl'):
        print 'found file'
        f = open('saved_DY.pkl','r')
        D = pickle.load(f)
        D_labels = pickle.load(f)
        Y = pickle.load(f)
        Y_labels = pickle.load(f)

        return np.matrix(D),D_labels,np.matrix(Y),Y_labels

    mndata = MNIST(folder)
    train_ims,train_labels = mndata.load_training()
    print 'training loaded'
    test_ims,test_labels = mndata.load_testing()
    print 'testing loaded'

    training_samples = resize(np.array(train_ims),resolution)
    training_labels = np.array(train_labels)
    D,D_labels = organize(training_samples,training_labels,classes,amount)
    print 'dictionary, D, made'

    random_idx = np.array(np.random.permutation(10000))[0:signals] #10000 is total signals avail

    Y = (resize(np.array(test_ims),resolution))[:,random_idx]
    Y_labels = np.array(test_labels)[random_idx]
    print 'signals, Y, made'

    saveToFile(D,D_labels,Y,Y_labels)

    return np.matrix(D),D_labels,np.matrix(Y),Y_labels
开发者ID:S-Aleti,项目名称:Cloud-K-SVD-Implementation,代码行数:32,代码来源:MNIST_Loader.py


示例3: __init__

    def __init__(self,collect_gold_standard):
        n_neighbors = 15

        mndata = MNIST('/home/ggdhines/Databases/mnist')
        training = mndata.load_training()

        digits = range(0,10)

        training_dict = {d:[] for d in digits}

        for t_index,label in enumerate(training[1]):
            training_dict[label].append(training[0][t_index])

        weight = "distance"
        self.clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weight)

        pca = PCA(n_components=50)
        self.T = pca.fit(training[0])
        reduced_training = self.T.transform(training[0])
        # print sum(pca.explained_variance_ratio_)
        # clf.fit(training[0], training[1])
        self.clf.fit(reduced_training, training[1])

        self.transcribed_digits = {d:[] for d in digits}
        self.collect_gold_standard = collect_gold_standard

        self.cells_to_process = []
        self.completed_cells = []
开发者ID:CKrawczyk,项目名称:aggregation,代码行数:28,代码来源:learning.py


示例4: load_data

def load_data(dataset="mnist_small", center=False):
    '''
        @param dataset: The dataset to load
        @param random_state: random state to control random parameter

        Load a specified dataset currently only
        "mnist_small" and "mnist" are supported
    '''
    if (dataset == "mnist_small"):
        X_train = np.loadtxt("./mldata/mnist_small/X_train", delimiter=",").reshape(1540,64)
        X_test = np.loadtxt("./mldata/mnist_small/X_test", delimiter=",").reshape(257,64)
        y_train = np.loadtxt("./mldata/mnist_small/y_train", delimiter=",")
        y_test = np.loadtxt("./mldata/mnist_small/y_test", delimiter=",")
        X_train = X_train[:,:,np.newaxis]
        X_test = X_test[:,:,np.newaxis]
    elif dataset == "mnist":
        mndata = MNIST('./mldata/mnist')
        X_train, y_train = map(np.array, mndata.load_training())
        X_test, y_test = map(np.array, mndata.load_testing())
        X_train = X_train/255.0
        X_test = X_test/255.0
        X_train = X_train[:,:,np.newaxis]
        X_test = X_test[:,:,np.newaxis]
    elif dataset == "cifar":
        (X_train, y_train), (X_test, y_test) = load_cifar()

    else:
        raise Exception("Datset not found")

    return (X_train, y_train), (X_test, y_test)
开发者ID:Vaishaal,项目名称:ckm,代码行数:30,代码来源:ckm.py


示例5: load_dataset

def load_dataset():
    mndata = MNIST("./data/")
    X_train, labels_train = map(np.array, mndata.load_training())
    X_test, labels_test = map(np.array, mndata.load_testing())
    X_train = X_train / 255.0
    X_test = X_test / 255.0
    X_train = X_train[:, :, np.newaxis]
    X_test = X_test[:, :, np.newaxis]
    return (X_train, labels_train), (X_test, labels_test)
开发者ID:ssquinntran,项目名称:HDlanguageDetection,代码行数:9,代码来源:hw1.py


示例6: __init__

    def __init__(self):
        Classifier.__init__(self)

        self.classifier = svm.SVC(gamma=0.001,probability=True)

        mndata = MNIST('/home/ggdhines/Databases/mnist')
        training = mndata.load_training()

        self.classifier.fit(training[0], training[1])
开发者ID:lelou6666,项目名称:aggregation,代码行数:9,代码来源:learning.py


示例7: load_data

 def load_data(self):
     mn = MNIST('.')
     mn.test()
     data = mn.train_images
     data = np.array(data)
     
     data.astype(np.float32)
     data = data/255.0
     return data
开发者ID:whirlp00l,项目名称:ML,代码行数:9,代码来源:MLP.py


示例8: load

    def load(self,pixels_per_cell = (8,8),cells_per_block=(3,3),orientations=9):
        '''
        Generates a Data Set

        Parameters: None

        Returns:    train_set     - Training Set of 10000 images
                    train_labels  - Training Set Labels of corresponding images
                    test_set      - Test Set of 10000 images
                    test_labels   - Test Set Labels of corresponding images
        '''
        mn = MNIST('./data')
        train_raw = mn.load_training()
        test_raw = mn.load_testing()

        print "Loaded Raw images"

        learning_set = []
        Boom = {}
        for i in range(10):
            Boom[str(i)] = []
        for i in range(0,60000):
            Boom[str(train_raw[1][i])].append(train_raw[0][i])
        for i in range(0,10000):
            Boom[str(test_raw[1][i])].append(test_raw[0][i])
        t = datetime.now().microsecond
        random.seed(t)
        [random.shuffle(Boom[str(i)]) for i in range(10)]

        print "Choosing 20000 training images uniformly randomly"

        # Descriptor Generator
        for l in range(10):
            for i in range(0,2000):
                img =  np.array(Boom[str(l)][i])
                img.shape = (28,28)
                fd, hog_image = hog(img, orientations=orientations, pixels_per_cell=pixels_per_cell,cells_per_block=cells_per_block, visualise=True)
                learning_set.append([fd,l])

        print "Data Points now chosen and Generated HOG descriptors for them"

        t = datetime.now().microsecond
        random.seed(t)
        print "Shuffling Chosen Data Set"
        random.shuffle(learning_set)

        for i in range(20000):
            self.learning_set.append(learning_set[i][0])
            self.learning_set_labels.append(learning_set[i][1])

        print "Data Loading and Distribution Succesfully done"

        self.train_set = self.learning_set[:10000]
        self.train_labels = self.learning_set_labels[:10000]
        self.test_set = self.learning_set[10000:20000]
        self.test_labels = self.learning_set_labels[10000:20000]
开发者ID:manikantareddyd,项目名称:Ensemble-Classifiers-MNIST,代码行数:56,代码来源:data.py


示例9: load_data

def load_data(datadir, is_training=False):

    mn = MNIST(datadir)

    if is_training:
        img, label = mn.load_training()
    else:
        img, label = mn.load_testing()

    return img, label
开发者ID:tnycum,项目名称:CMSC611,代码行数:10,代码来源:naive_bayes.py


示例10: loaddata

def loaddata():
    #Loading mnist data using python-mnist library
    mnLoader = MNIST('asgndata/mnist')
    data1 = mnLoader.load_training() # train data
    data2 = mnLoader.load_testing()  # test data

    features = np.array(data1[0]+data2[0], 'int16')
    labels = np.array(data1[1]+data2[1], 'int')
    X_train, y_train, X_test, y_test = preprocessData(features, labels)
    return X_train, y_train, X_test, y_test 
开发者ID:submagr,项目名称:UGP,代码行数:10,代码来源:1_a.py


示例11: load_targets

 def load_targets(self):
     mn = MNIST('.')
     mn.test()
     targets = []
     for t in mn.train_labels:
         #print t
         out = np.zeros(self.output)
         out[t] = 1
         targets.append(out)
     targets = np.array(targets)
     return targets
开发者ID:whirlp00l,项目名称:ML,代码行数:11,代码来源:MLP.py


示例12: __init__

 def __init__(self,k):
     #Define k value
     self.k=k
     #Load MNIST datset
     mnistData=MNIST('./mnistData')
     self.imgTrain,self.lblTrain=mnistData.load_training()
     self.imgTest,self.lblTest=mnistData.load_testing()
     #Initialize the random centroids
     self.imgCen=[]
     for c in xrange(self.k):
         self.imgCen.append([random.randint(0,255) for d in xrange(784)])
开发者ID:PseudoAj,项目名称:NeuralNetworks,代码行数:11,代码来源:KMeansMNIST.py.py


示例13: train_rls

def train_rls():
    mndata = MNIST("./data")
    X_train, Y_train = mndata.load_training()
    X_test, Y_test = mndata.load_testing()
    X_train, X_test = np.array(X_train), np.array(X_test)
    #One-vs-all mapping
    Y_train = ova(Y_train)
    Y_test = ova(Y_test)
    #Train greedy RLS, select 50 features
    cb = Callback(X_test, Y_test)
    learner = GreedyRLS(X_train, Y_train, 50, callbackfun=cb)
    print("Selected features " +str(learner.selected))
开发者ID:aatapa,项目名称:RLScore,代码行数:12,代码来源:greedy_mnist.py


示例14: run

def run():
  TorchModel = PyTorchHelpers.load_lua_class('torch_model.lua', 'TorchModel')
  torchModel = TorchModel(backend, 28, 10)

  mndata = MNIST('../../data/mnist')
  imagesList, labelsList = mndata.load_training()
  labels = np.array(labelsList, dtype=np.uint8)
  images = np.array(imagesList, dtype=np.float32)
  labels += 1  # since torch/lua labels are 1-based
  N = labels.shape[0]
  print('loaded mnist training data')

  if numTrain > 0:
    N = min(N, numTrain)
  print('numExamples N', N)
  numBatches = N // batchSize
  for epoch in range(numEpochs):
    epochLoss = 0
    epochNumRight = 0
    for b in range(numBatches):
      res = torchModel.trainBatch(
        learningRate,
        images[b * batchSize:(b+1) * batchSize],
        labels[b * batchSize:(b+1) * batchSize])
#      print('res', res)
      numRight = res['numRight']
      loss = res['loss']
      epochNumRight += numRight
      epochLoss += loss
      print('epoch ' + str(epoch) + ' batch ' + str(b) + ' accuracy: ' + str(numRight * 100.0 / batchSize) + '%')
    print('epoch ' + str(epoch) + ' accuracy: ' + str(epochNumRight * 100.0 / N) + '%')

  print('finished training')
  print('loading test data...')
  imagesList, labelsList = mndata.load_testing()
  labels = np.array(labelsList, dtype=np.uint8)
  images = np.array(imagesList, dtype=np.float32)
  labels += 1  # since torch/lua labels are 1-based
  N = labels.shape[0]
  print('loaded mnist testing data')

  numBatches = N // batchSize
  epochLoss = 0
  epochNumRight = 0
  for b in range(numBatches):
    predictions = torchModel.predict(images[b * batchSize:(b+1) * batchSize]).asNumpyTensor().reshape(batchSize)
    labelsBatch = labels[b * batchSize:(b+1) * batchSize]
    numRight = (predictions == labelsBatch).sum()
    epochNumRight += numRight
  print('test results: accuracy: ' + str(epochNumRight * 100.0 / N) + '%')
开发者ID:amoliu,项目名称:pytorch,代码行数:50,代码来源:runner.py


示例15: run

def run(args):

    nn_args = {}

    if args.output_activation:
        activation_class = getattr(activation_functions, args.output_activation)
        nn_args['output_activation'] = activation_class()

    nn = Network(args.shape, seed=42, **nn_args)

    print "Loading the training data"
    mndata = MNIST(args.training_data)
    training_data, training_labels = mndata.load_training()

    training_data = convert_training_data(training_data)
    training_labels = convert_number_labels_to_vectors(training_labels)

    fitted, epochs = nn.SGD(training_data, training_labels,
                            epochs=args.epochs,
                            mini_batch_size=args.mini_batch_size,
                            eta=args.eta,
                            save_history=args.save_epochs)

    if args.testing_data:
        print "Testing data"
        test_data, test_labels = mndata.load_testing()
        test_data = convert_training_data(test_data)
        # For evaluation, we put the index of the label
        # with the argmax
        evaluation = fitted.evaluate(test_data, test_labels,
                                     evaluator=np.argmax)
        print evaluation

    if args.save:

        label_dir = mkdir_or_temp(args.save)

        fitted_path = "{}/nn.pkl".format(label_dir)

        with open(fitted_path, 'wb') as handle:
            pickle.dump(fitted, handle)

        if epochs is not None:
            for i, epoch in enumerate(epochs):
                epoch_path = '{}/nn_epoch_{}.pkl'.format(label_dir, i)
                with open(epoch_path, 'wb') as handle:
                    pickle.dump(epoch, handle)
                    print "Saved epoch {} to {}".format(i, epoch_path)
开发者ID:ghl3,项目名称:brain,代码行数:48,代码来源:train.py


示例16: query_digit

def query_digit(digit):
    host, port = "localhost", 4567
    con = httplib.HTTPConnection(host, port)
    params = json.dumps({"data": digit})
    con.request("POST", "/digit", params)
    response = con.getresponse()
    print "For digit:%s\nReceived prediction response [%s]\n" % (MNIST.display(digit), response.read())
开发者ID:jclavijo,项目名称:pmml-mnist-demo,代码行数:7,代码来源:train_and_export.py


示例17: main

def main(kernel):
  print "Loading the data"

  mn = MNIST(DATA_PATH)

  test_img, test_label = mn.load_testing()
  train_img, train_label = mn.load_training()
  
  train_img = np.array(train_img[:SIZE_TRAIN])
  train_label = np.array(train_label[:SIZE_TRAIN])
  test_img = np.array(test_img[:SIZE_TEST])
  test_label = np.array(test_label[:SIZE_TEST])

  print "Finished loading the data"

  # Create a classifier: a support vector classifier
  if kernel == 'rbf':
    print "Training with RBF kernel - Might take a few minutes"
    classifier = svm.SVC(C=10, gamma=5e-7, kernel='rbf') 
  elif kernel == 'linear':
    print "Training with Linear kernel - Might take a few minutes"
    classifier = svm.SVC(C=1e-6, kernel='linear')
  elif kernel == 'poly':
    print "Training with Polynomial kernel - Might take a few minutes"
    #classifier = svm.SVC(C=10, gamma=1e-7, kernel='poly', degree=2)
    #classifier = svm.SVC(C=10, gamma=1e-6, kernel='poly', degree=3)
    classifier = svm.SVC(C=10, gamma=1e-6, kernel='poly', degree=4)

  # We learn the digits on the first half of the digits
  classifier.fit(train_img, train_label)

  print "Classifying - Might take a few minutes"

  predicted = classifier.predict(test_img)
  print predicted

  cm = metrics.confusion_matrix(test_label, predicted)

  print("Classification report for classifier %s:\n%s\n"% (classifier, metrics.classification_report(test_label, predicted)))
  print("Confusion matrix:\n%s" % cm)

  cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]

  plt.figure()
  plot_confusion_matrix(cm_normalized, title='Normalized confusion matrix')

  print "Result: %s"%(np.trace(cm_normalized)/10)
开发者ID:corentintallec,项目名称:epfl_project,代码行数:47,代码来源:svm.py


示例18: load_mnist

def load_mnist():
	download_mnist()
	mnist_data = MNIST(MNIST_PATH)
	train_imgs, train_labels = mnist_data.load_training()
	test_imgs, test_labels = mnist_data.load_testing()
	data = {}

	for i in range(len(train_imgs)):
		square = []
		for j in range(1024):
			row = j / 32
			col = j % 32
			if (row < 2 or col < 2 or row > 29 or col > 29):
				square.append(0)
			else:
				val = train_imgs[i][(row - 2) * 28 + col - 2]
				square.append(val)
		train_imgs[i] = square

	for i in range(len(test_imgs)):
		square = []
		for j in range(1024):
			row = j / 32
			col = j % 32
			if (row < 2 or col < 2 or row > 29 or col > 29):
				square.append(0)
			else:
				val = test_imgs[i][(row - 2) * 28 + col - 2]
				square.append(val)
		test_imgs[i] = square

	data["train_imgs"] = np.array(train_imgs, dtype="f").reshape(60000, 1, 32, 32)
	data["test_imgs"] = np.array(test_imgs, dtype="f").reshape(10000, 1, 32, 32)
	data["train_labels"] = np.array(train_labels)
	data["test_labels"] = np.array(test_labels)

	preprocess(data["train_imgs"], data["test_imgs"])

	data["train_no"] = 60000
	data["test_no"] = 10000

	return data
开发者ID:HuleaAlexandru,项目名称:Projects,代码行数:42,代码来源:data_loader_mnist.py


示例19: main

def main():
    data = MNIST('./data')

    def transform(x):
        return x / 255.

    # 60,000 train samples of 28x28 grid, domain 0-255
    mnist_train_data, mnist_train_label = data.load_training()
    mnist_train_data_norm = np.array([transform(np.array(x)) for x in mnist_train_data])

    mlp_config = {'hidden_layer_sizes': (1000,),
                  'activation': 'relu',
                  'algorithm': 'adam',
                  'max_iter': 20,
                  'early_stopping': True,
                  'validation_fraction': 0.1,
                  'verbose': True
                  }
    mnist_classifier = nn.MLPClassifier(**mlp_config)
    mnist_classifier.fit(X=mnist_train_data_norm, y=mnist_train_label)

    # 10,000 test samples
    mnist_test_data, mnist_test_label = data.load_testing()
    mnist_test_data_norm = np.array([transform(np.array(x)) for x in mnist_test_data])

    prediction = mnist_classifier.predict_proba(mnist_test_data_norm)
    truth_array = [prediction[idx].argmax() == mnist_test_label[idx] for idx in range(len(prediction))]
    accuracy = float(sum(truth_array)) / float(len(truth_array))
    print "out of sample model accuracy [%s]" % accuracy

    print "serializing to pmml without transform (User defined transform not yet supported"
    pmml_path = "./model_pmml"
    if not os.path.exists(pmml_path):
        os.mkdir(pmml_path)
    sklearn2pmml(mnist_classifier, None, pmml_path + "/MLP_MNIST.pmml", with_repr=True)

    print "serializing with joblib for import in python"
    # KJS TODO: Serialize transform with the model
    pickle_path = "./model_pickle"
    if not os.path.exists(pickle_path):
        os.mkdir(pickle_path)
    joblib.dump(mnist_classifier, pickle_path + "/MLP_MNIST.pkl")
开发者ID:PyClass,项目名称:PyClassLessons,代码行数:42,代码来源:train_and_export.py


示例20: MNISTDataset

class MNISTDataset(Dataset):
    def __init__(self, path):
        self.mndata = MNIST(path)
        self.images, self.labels = self.mndata.load_training()

    def nth_case(self, n):
        return self.images[n], bitvec(self.labels[n])

    @property
    def size(self):
        return len(self.images)
开发者ID:regiontog,项目名称:anns,代码行数:11,代码来源:dataset.py



注:本文中的mnist.MNIST类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python mnist_loader.load_data_wrapper函数代码示例发布时间:2022-05-27
下一篇:
Python utils.expand_path函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap