下面的代碼將 mnist 資料集打開為 csv
import numpy as np
import csv
import matplotlib.pyplot as plt
with open('C:/Z_Uni/Individual_Project/Python_Projects/NeuralNet/MNIST_Dataset/mnist_train.csv/mnist_train.csv', 'r') as csv_file:
for data in csv.reader(csv_file):
# The first column is the label
label = data[0]
# The rest of columns are pixels
pixels = data[1:]
# Make those columns into a array of 8-bits pixels
# This array will be of 1D with length 784
# The pixel intensity values are integers from 0 to 255
pixels = np.array(pixels, dtype='uint8')
print(pixels.shape)
# Reshape the array into 28 x 28 array (2-dimensional array)
pixels = pixels.reshape((28, 28))
print(pixels.shape)
# Plot
plt.title('Label is {label}'.format(label=label))
plt.imshow(pixels, cmap='gray')
plt.show()
break # This stops the loop, I just want to see one
我從某人那里得到了上面的代碼,但無法讓它顯示 mnist 數字。
我得到錯誤:
回溯(最后一次呼叫):檔案“C:\Z_Uni\Individual_Project\Python_Projects\NeuralNet\Test_View_Mnist.py”,第 16 行,以像素為單位 = np.array(pixels, dtype='uint8') ValueError: invalid literal for int () 以 10 為底:'1x1'
當我洗掉 dtype='unit8' 我得到錯誤:
回溯(最近一次通話最后):檔案“C:\Z_Uni\Individual_Project\Python_Projects\NeuralNet\Test_View_Mnist.py”,第 24 行,在 plt.imshow(pixels, cmap='gray') 檔案“C:\Z_Uni\Individual_Project \Python_Projects\NeuralNet\source\lib\site-packages\matplotlib_api\deprecation.py”,第 456 行,在包裝回傳 func(*args, **kwargs) 檔案“C:\Z_Uni\Individual_Project\Python_Projects\NeuralNet\source\ lib\site-packages\matplotlib\pyplot.py",第 2640 行,在 imshow_ret = gca().imshow( 檔案 "C:\Z_Uni\Individual_Project\Python_Projects\NeuralNet\source\lib\site-packages\matplotlib_api\ deprecation.py”,第 456 行,在包裝器中回傳 func(*args, **kwargs) 檔案“C:\Z_Uni\Individual_Project\Python_Projects\NeuralNet\source\lib\site-packages\ matplotlib_init.py”,第 1412 行,內部回傳 func(ax, *map(sanitize_sequence, args), **kwargs) 檔案“C:\Z_Uni\Individual_Project\Python_Projects\NeuralNet\source\lib\site-packages\matplotlib\axes_axes .py”,第 5488 行,在 imshow 中
im.set_data(X)
檔案“C:\Z_Uni\Individual_Project\Python_Projects\NeuralNet\source\lib\site-packages\matplotlib\image.py”,第 706 行,在 set_data 中引發 TypeError("dtype {} 的影像資料無法轉換為" TypeError: dtype <U5的影像資料不能轉換為float
行程以退出代碼 1 結束
有人可以解釋為什么會發生這個錯誤以及如何解決它嗎?謝謝。
uj5u.com熱心網友回復:
這里有兩個問題。(1) 您需要跳過第一行,因為它們是標簽。(1x1)、(1x2) 等。 (2) 您需要 int64 資料型別。下面的代碼將解決這兩個問題。next(csvreader)跳過第一行。
import numpy as np
import csv
import matplotlib.pyplot as plt
with open('./mnist_test.csv', 'r') as csv_file:
csvreader = csv.reader(csv_file)
next(csvreader)
for data in csvreader:
# The first column is the label
label = data[0]
# The rest of columns are pixels
pixels = data[1:]
# Make those columns into a array of 8-bits pixels
# This array will be of 1D with length 784
# The pixel intensity values are integers from 0 to 255
pixels = np.array(pixels, dtype = 'int64')
print(pixels.shape)
# Reshape the array into 28 x 28 array (2-dimensional array)
pixels = pixels.reshape((28, 28))
print(pixels.shape)
# Plot
plt.title('Label is {label}'.format(label=label))
plt.imshow(pixels, cmap='gray')
plt.show()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/430107.html
