最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的示例代碼

 更新時(shí)間:2024年04月09日 09:48:35   作者:深色風(fēng)信子  
這篇文章主要介紹了Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的的相關(guān)示例,文中有詳細(xì)的代碼示例供大家參考,對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下

1 Python TensorFlow 2.6 獲取 MNIST 數(shù)據(jù)

1.1 獲取 MNIST 數(shù)據(jù)

獲取 MNIST 數(shù)據(jù)

import numpy as np
import tensorflow as tf

from tensorflow.keras import datasets

print(tf.__version__)

(train_data, train_label), (test_data, test_label) = datasets.mnist.load_data()
np.savez('D:\\OneDrive\\桌面\\mnist.npz', train_data = train_data, train_label = train_label, test_data = test_data,
         test_label = test_label)
C:\ProgramData\Anaconda3\envs\tensorflow\python.exe E:/SourceCode/PyCharm/Test/study/exam.py
2.6.0

Process finished with exit code 0

1.2 檢查 MNIST 數(shù)據(jù)

import matplotlib.pyplot as plt
import numpy as np

data = np.load('D:\\OneDrive\\桌面\\mnist.npz')
print(data.files)

image = data['train_data'][0:100]
label = data['train_label'].reshape(-1, )
print(label)
plt.figure(figsize = (10, 10))
for i in range(100):
    print('%f, %f' % (i, label[i]))
    plt.subplot(10, 10, i + 1)
    plt.imshow(image[i])
plt.show()

在這里插入圖片描述

2 Python 將npz數(shù)據(jù)保存為txt

import numpy as np

# 加載mnist數(shù)據(jù)
data = np.load('D:\\學(xué)習(xí)\\mnist.npz')
# 獲取 訓(xùn)練數(shù)據(jù)
train_image = data['x_test']
train_label = data['y_test']
train_image = train_image.reshape(train_image.shape[0], -1)
train_image = train_image.astype(np.int32)
train_label = train_label.astype(np.int32)
train_label = train_label.reshape(-1, 1)
index = 0
file = open('D:\\OneDrive\\桌面\\predict.txt', 'w+')
for arr in train_image:
    file.write('{0}->{1}\n'.format(train_label[index][0], ','.join(str(i) for i in arr)))
    index = index + 1
file.close()

在這里插入圖片描述

3 Java 獲取數(shù)據(jù)并使用SVM訓(xùn)練

package com.xu.opencv;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.TermCriteria;
import org.opencv.ml.Ml;
import org.opencv.ml.SVM;

/**
 * @author Administrator
 */
public class Train {

    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args) throws Exception {
        predict();
    }

    public static void predict() throws Exception {
        SVM svm = SVM.load("D:\\OneDrive\\桌面\\ai.xml");
        BufferedReader reader = new BufferedReader(new FileReader("D:\\OneDrive\\桌面\\predict.txt"));
        Mat train = new Mat(6, 28 * 28, CvType.CV_32FC1);
        Mat label = new Mat(1, 6, CvType.CV_32SC1);
        Map<String, Mat> map = new HashMap<>(2);
        int index = 0;
        String line = null;
        while ((line = reader.readLine()) != null) {
            int[] data = Arrays.asList(line.split("->")[1].split(",")).stream().mapToInt(Integer::parseInt).toArray();
            for (int i = 0; i < 28 * 28; i++) {
                train.put(index, i, data[i]);
            }
            label.put(index, 0, Integer.parseInt(line.split("->")[0]));
            index++;
            if (index >= 6) {
                break;
            }
        }
        Mat response = new Mat();
        svm.predict(train, response);
        for (int i = 0; i < response.height(); i++) {
            System.out.println(response.get(i, 0)[0]);
        }
    }

    public static void train() throws Exception {
        SVM svm = SVM.create();
        svm.setC(1);
        svm.setP(0);
        svm.setNu(0);
        svm.setCoef0(0);
        svm.setGamma(1);
        svm.setDegree(0);
        svm.setType(SVM.C_SVC);
        svm.setKernel(SVM.LINEAR);
        svm.setTermCriteria(new TermCriteria(TermCriteria.EPS + TermCriteria.MAX_ITER, 1000, 0));
        Map<String, Mat> map = read("D:\\OneDrive\\桌面\\data.txt");
        svm.train(map.get("train"), Ml.ROW_SAMPLE, map.get("label"));
        svm.save("D:\\OneDrive\\桌面\\ai.xml");
    }

    public static Map<String, Mat> read(String path) throws Exception {
        BufferedReader reader = new BufferedReader(new FileReader(path));
        String line = null;
        Mat train = new Mat(60000, 28 * 28, CvType.CV_32FC1);
        Mat label = new Mat(1, 60000, CvType.CV_32SC1);
        Map<String, Mat> map = new HashMap<>(2);
        int index = 0;
        while ((line = reader.readLine()) != null) {
            int[] data = Arrays.asList(line.split("->")[1].split(",")).stream().mapToInt(Integer::parseInt).toArray();
            for (int i = 0; i < 28 * 28; i++) {
                train.put(index, i, data[i]);
            }
            label.put(index, 0, Integer.parseInt(line.split("->")[0]));
            index++;
        }
        map.put("train", train);
        map.put("label", label);
        reader.close();
        return map;
    }

}

4 Python 測(cè)試SVM準(zhǔn)確度

9.8% 求幫助

import cv2 as cv
import numpy as np

# 加載預(yù)測(cè)數(shù)據(jù)
data = np.load('D:\\學(xué)習(xí)\\mnist.npz')
print(data.files)

# 預(yù)測(cè)數(shù)據(jù) 處理
test_image = data['x_test']
test_label = data['y_test']

test_image = test_image.reshape(test_image.shape[0], -1)
test_image = test_image.astype(np.float32)
test_label = test_label.astype(np.float32)
test_label = test_label.reshape(-1, 1)

svm = cv.ml.SVM_load('D:\\OneDrive\\桌面\\ai.xml')

predict = svm.predict(test_image)
predict = predict[1].reshape(-1, 1).astype(np.int32)
result = (predict == test_label.astype(np.int32))
print('{0}%'.format(str(result.mean() * 100)))
C:\ProgramData\Anaconda3\envs\opencv\python.exe E:/SourceCode/PyCharm/OpenCV/svm/predict.py
['x_train', 'y_train', 'x_test', 'y_test']
9.8%

Process finished with exit code 0

以上就是Python TensorFlow 2.6獲取MNIST數(shù)據(jù)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python TensorFlow獲取MNIST的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù)

    Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù)

    這篇文章主要介紹了Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Python使用wxPython和PyMuPDF實(shí)現(xiàn)合并PDF文檔

    Python使用wxPython和PyMuPDF實(shí)現(xiàn)合并PDF文檔

    處理大量的PDF文檔可能會(huì)變得復(fù)雜和耗時(shí),但是,使用Python編程和一些強(qiáng)大的庫(kù),可以使這個(gè)任務(wù)變得簡(jiǎn)單而高效,下面我們就來(lái)看看Python如何使用wxPython和PyMuPDF合并PDF文檔并自動(dòng)復(fù)制到剪貼板吧
    2023-11-11
  • 增大python字體的方法步驟

    增大python字體的方法步驟

    在本篇文章里小編給大家整理了關(guān)于增大python字體的方法步驟,需要的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • python生成隨機(jī)數(shù)、隨機(jī)字符、隨機(jī)字符串的方法示例

    python生成隨機(jī)數(shù)、隨機(jī)字符、隨機(jī)字符串的方法示例

    這篇文章主要介紹了python生成隨機(jī)數(shù)、隨機(jī)字符、隨機(jī)字符串的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • 使用Pandas實(shí)現(xiàn)高效讀取篩選csv數(shù)據(jù)

    使用Pandas實(shí)現(xiàn)高效讀取篩選csv數(shù)據(jù)

    在數(shù)據(jù)分析和數(shù)據(jù)科學(xué)領(lǐng)域中,Pandas?是?Python?中最常用的庫(kù)之一,本文將介紹如何使用?Pandas?來(lái)讀取和處理?CSV?格式的數(shù)據(jù)文件,希望對(duì)大家有所幫助
    2024-04-04
  • 一文詳解NumPy數(shù)組迭代與合并

    一文詳解NumPy數(shù)組迭代與合并

    NumPy?數(shù)組迭代是訪問和處理數(shù)組元素的重要方法,它允許您逐個(gè)或成組地遍歷數(shù)組元素,NumPy?提供了多種函數(shù)來(lái)合并數(shù)組,用于將多個(gè)數(shù)組的內(nèi)容連接成一個(gè)新數(shù)組,本文給大家詳細(xì)介紹了NumPy數(shù)組迭代與合并,需要的朋友可以參考下
    2024-05-05
  • Python實(shí)現(xiàn)的基數(shù)排序算法原理與用法實(shí)例分析

    Python實(shí)現(xiàn)的基數(shù)排序算法原理與用法實(shí)例分析

    這篇文章主要介紹了Python實(shí)現(xiàn)的基數(shù)排序算法,簡(jiǎn)單說明了基數(shù)排序的原理并結(jié)合實(shí)例形式分析了Python實(shí)現(xiàn)與使用基數(shù)排序的具體操作技巧,需要的朋友可以參考下
    2017-11-11
  • 如何利用Python快速繪制海報(bào)級(jí)別地圖詳解

    如何利用Python快速繪制海報(bào)級(jí)別地圖詳解

    Python之所以這么流行,是因?yàn)樗粌H能夠應(yīng)用于科技領(lǐng)域,還能用來(lái)做許多其他學(xué)科的研究工具,最常見的便是繪制地圖,這篇文章主要給大家介紹了關(guān)于如何利用Python快速繪制海報(bào)級(jí)別地圖的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • 基于python實(shí)現(xiàn)把圖片轉(zhuǎn)換成素描

    基于python實(shí)現(xiàn)把圖片轉(zhuǎn)換成素描

    這篇文章主要介紹了基于python實(shí)現(xiàn)把圖片轉(zhuǎn)換成素描,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python基礎(chǔ)之文件讀取的講解

    Python基礎(chǔ)之文件讀取的講解

    今天小編就為大家分享一篇關(guān)于Python基礎(chǔ)之文件讀取的講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-02-02

最新評(píng)論

宜城市| 岐山县| 福州市| 称多县| 朝阳县| 蒙山县| 介休市| 嵊州市| 烟台市| 许昌县| 东丽区| 庆城县| 新闻| 达拉特旗| 扶余县| 盖州市| 健康| 清远市| 华宁县| 苗栗县| 乌拉特后旗| 高碑店市| 阆中市| 丰台区| 宾川县| 安丘市| 衡水市| 布尔津县| 尼勒克县| 佛冈县| 额敏县| 滨州市| 新乐市| 东丰县| 上饶县| 浦县| 绵阳市| 澄城县| 灌南县| 通榆县| 屏东县|