python 映像歸一化作業代碼代編程代寫圖python作業

來源:互聯網
上載者:User

標籤:工程   img   業務   error   nat   基本   向量   環境搭建   return   

python 映像歸一化作業代碼代編程代寫圖python作業
from PIL import Image
import os
import sys
import numpy as np
import time
from sklearn import svm


# 擷取指定路徑下的所有 .png 檔案
def get_file_list(path):
return [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".png")]


# 解析出 .png 圖件檔案的名稱
def get_img_name_str(imgPath):
return imgPath.split(os.path.sep)[-1]


# 將 20px * 20px 的映像資料轉換成 1*400 的 numpy 向量
# 參數:imgFile--映像名 如:0_1.png
# 返回:1*400 的 numpy 向量
def img2vector(imgFile):
#print("in img2vector func--para:{}".format(imgFile))
img = Image.open(imgFile).convert(‘L‘)
img_arr = np.array(img, ‘i‘) # 20px * 20px 灰階映像
img_normalization = np.round(img_arr/255) # 對灰階值進行歸一化
img_arr2 = np.reshape(img_normalization, (1,-1)) # 1 * 400 矩陣
return img_arr2

# 讀取一個類別的所有資料並轉換成矩陣
# 參數:
# basePath: 映像資料所在的基本路徑
# Mnist-image/train/
# Mnist-image/test/
# cla:類別名稱
# 0,1,2,...,9
# 返回:某一類別的所有資料----[樣本數量*(映像寬x映像高)] 矩陣
def read_and_convert(imgFileList):
dataLabel = [] # 存放類標籤
dataNum = len(imgFileList)
dataMat = np.zeros((dataNum, 400)) # dataNum * 400 的矩陣
for i in range(dataNum):
imgNameStr = imgFileList[i]
imgName = get_img_name_str(imgNameStr) # 得到 數字_執行個體編號.png
#print("imgName: {}".format(imgName))
classTag = imgName.split(".")[0].split("_")[0] # 得到 類標籤(數字)
#print("classTag: {}".format(classTag))
dataLabel.append(classTag)
dataMat[i,:] = img2vector(imgNameStr)
return dataMat, dataLabel


# 讀取訓練資料
def read_all_data():
cName = [‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘]
train_data_path = "Mnist-image\\train\\0"
flist = get_file_list(train_data_path)
dataMat, dataLabel = read_and_convert(flist)
for c in cName:
train_data_path_ = "Mnist-image\\train\\" + c
flist_ = get_file_list(train_data_path_)
dataMat_, dataLabel_ = read_and_convert(flist_)
dataMat = np.concatenate((dataMat, dataMat_), axis=0)
dataLabel = np.concatenate((dataLabel, dataLabel_), axis=0)
#print(dataMat.shape)
#print(len(dataLabel))
return dataMat, dataLabel


# create model
def create_svm(dataMat, dataLabel, decision=‘ovr‘):
clf = svm.SVC(decision_function_shape=decision)
clf.fit(dataMat, dataLabel)
return clf


#clf = svm.SVC(decision_function_shape=‘ovr‘)
st = time.clock()
clf = create_svm(dataMat, dataLabel, decision=‘ovr‘)
et = time.clock()
print("Training spent {:.4f}s.".format((et-st)))


# 對10個數字進行分類測試
def main():
tbasePath = "Mnist-image\\test\\"
tcName = [‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘]
tst = time.clock()
allErrCount = 0
allErrorRate = 0.0
allScore = 0.0
for tcn in tcName:
testPath = "Mnist-image\\test\\" + tcn
#print("class " + tcn + " path is: {}.".format(testPath))
tflist = get_file_list(testPath)
#tflist
tdataMat, tdataLabel = read_and_convert(tflist)
print("test dataMat shape: {0}, test dataLabel len: {1} ".format(tdataMat.shape, len(tdataLabel)))

#print("test dataLabel: {}".format(len(tdataLabel)))
pre_st = time.clock()
preResult = clf.predict(tdataMat)
pre_et = time.clock()
print("Recognition " + tcn + " spent {:.4f}s.".format((pre_et-pre_st)))
#print("predict result: {}".format(len(preResult)))
errCount = len([x for x in preResult if x!=tcn])
print("errorCount: {}.".format(errCount))
allErrCount += errCount
score_st = time.clock()
score = clf.score(tdataMat, tdataLabel)
score_et = time.clock()
print("computing score spent {:.6f}s.".format(score_et-score_st))
allScore += score
print("score: {:.6f}.".format(score))
print("error rate is {:.6f}.".format((1-score)))
print("---------------------------------------------------------")


tet = time.clock()
print("Testing All class total spent {:.6f}s.".format(tet-tst))
print("All error Count is: {}.".format(allErrCount))
avgAccuracy = allScore/10.0
print("Average accuracy is: {:.6f}.".format(avgAccuracy))
print("Average error rate is: {:.6f}.".format(1-avgScore))
http://www.6daixie.com/contents/3/1375.html

本團隊核心人員組成主要包括矽谷工程師、BAT一線工程師,國內Top5碩士、博士生,精通德英語!我們主要業務範圍是代做編程大作業、課程設計等等。

 

我們的方向領域:window編程 數值演算法 AI人工智慧 金融統計 計量分析 大資料 網路編程 WEB編程 通訊編程 遊戲編程多媒體linux 外掛編程 程式API影像處理 嵌入式/單片機 資料庫編程 控制台 進程與線程 網路安全  組合語言 硬體編程 軟體設計 工程標準規等。其中代寫代做程式設計語言或工具包括但不限於以下範圍:

C/C++/C#代寫

Java代寫

IT代寫

Python代寫

輔導編程作業

Matlab代寫

Haskell代寫

Processing代寫

Linux環境搭建

Rust代寫

Data Structure Assginment 資料結構代寫

MIPS代寫

Machine Learning 作業 代寫

Oracle/SQL/PostgreSQL/Pig 資料庫代寫/代做/輔導

Web開發、網站開發、網站作業

ASP.NET網站開發

Finance Insurace Statistics統計、迴歸、迭代

Prolog代寫

Computer Computational method代做

 

因為專業,所以值得信賴。如有需要,請加QQ:99515681 或郵箱:[email protected]

:codinghelp

python 映像歸一化作業代碼代編程代寫圖python作業

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.