【发布时间】:2017-12-07 21:06:15
【问题描述】:
我正在尝试下载 freyface 数据集并展示一些示例。
这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
# configure matplotlib
plt.rcParams['figure.figsize'] = (13.5, 13.5) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
import os
from urllib.request import urlopen, URLError, HTTPError
from scipy.io import loadmat
def fetch_file(url):
"""Downloads a file from a URL.
"""
try:
f = urlopen(url)
print
("Downloading data file " + url + " ...")
# Open our local file for writing
with open(os.path.basename(url), "wb") as local_file:
local_file.write(f.read())
print
("Done.")
# handle errors
except HTTPError as e:
print("HTTP Error:", e.code, url)
except URLError as e:
print
("URL Error:", e.reason, url)
url = "http://www.cs.nyu.edu/~roweis/data/frey_rawface.mat"
data_filename = os.path.basename(url)
if not os.path.exists(data_filename):
fetch_file(url)
else:
print
("Data file %s exists." % data_filename)
# reshape data for later convenience
img_rows, img_cols = 28, 20
ff = loadmat(data_filename, squeeze_me=True, struct_as_record=False)
ff = ff["ff"].T.reshape((-1, img_rows, img_cols))
np.random.seed(42)
n_pixels = img_rows * img_cols
X_train = ff[:1800]
X_val = ff[1800:1900]
X_train = X_train.astype('float32') / 255.
X_val = X_val.astype('float32') / 255.
X_train = X_train.reshape((len(X_train), n_pixels))
X_val = X_val.reshape((len(X_val), n_pixels))
#visualization
def show_examples(data, n=None, n_cols=20, thumbnail_cb=None):
if n is None:
n = len(data)
n_rows = int(np.ceil(n / float(n_cols)))
figure = np.zeros((img_rows * n_rows, img_cols * n_cols))
for k, x in enumerate(data[:n]):
r = k // n_cols
c = k % n_cols
figure[r * img_rows: (r + 1) * img_rows,
c * img_cols: (c + 1) * img_cols] = x
if thumbnail_cb is not None:
thumbnail_cb(locals())
plt.figure(figsize=(12, 10))
plt.imshow(figure)
plt.axis("off")
plt.tight_layout()
show_examples(ff, n=200, n_cols=25)
但我收到了这个奇怪的错误:
TypeError: remove() 缺少 1 个必需的位置参数:'wr' 异常被忽略:.remove at 0x000001EB6199D048>
TypeError: remove() 缺少 1 个必需的位置参数:'wr' 异常被忽略:.remove at 0x000001EB61992EA0>
TypeError: remove() 缺少 1 个必需的位置参数:'wr' 异常被忽略:.remove at 0x000001EB61992F28> 类型错误:remove() 缺少 1 个必需的位置参数:'wr'
【问题讨论】:
标签: python python-3.x machine-learning dataset pycharm