您好,欢迎来到尔游网。
搜索
您的当前位置:首页numpy.squeeze()

numpy.squeeze()

来源:尔游网

numpy.squeeze()

在吴恩达深度学习视频的作业里,经常遇到这样的情况。

ValueError: c of shape (1, 300) not acceptable as a color sequence for x with size 300, y with size 300

这是因为matplotlib.pyplot接收的color sequence 参数c应该是单个值,直接传Y或者Y.train传的是shape(1, size)的矩阵。

解决方案:

import numpy as np

np.squeeze(Y)
np.squeeze(Y.train)

np.squeeze这个函数的作用是去掉矩阵里维度为1的维度。

例:(1, 300)的矩阵经由np.squeeze处理后变成300; (200, 1, 300)的矩阵经由np.squeeze处理后变成(200, 300)。

详情请参阅官方文档:

numpy.squeeze
numpy.squeeze(a, axis=None)[source]
Remove single-dimensional entries from the shape of an array.

Parameters:	
a : array_like

Input data.

axis : None or int or tuple of ints, optional

New in version 1.7.0.

Selects a subset of the single-dimensional entries in the shape. If an axis is selected with shape entry greater than one, an error is raised.

Returns:	
squeezed : ndarray

The input array, but with all or a subset of the dimensions of length 1 removed. This is always a itself or a view into a.

Examples

>>>
>>> x = np.array([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> np.squeeze(x).shape
(3,)
>>> np.squeeze(x, axis=(2,)).shape
(1, 3)

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- axer.cn 版权所有 湘ICP备2023022495号-12

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务