前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >numpy.squeeze()

numpy.squeeze()

作者头像
Steve Wang
发布2019-05-26 15:06:51
2.8K0
发布2019-05-26 15:06:51
举报
文章被收录于专栏:从流域到海域从流域到海域

numpy.squeeze()

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

代码语言:javascript
复制
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)的矩阵。

解决方案:

代码语言:javascript
复制
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)。

详情请参阅官方文档:

代码语言:javascript
复制
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

代码语言:javascript
复制
>>>
>>> x = np.array([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> np.squeeze(x).shape
(3,)
>>> np.squeeze(x, axis=(2,)).shape
(1, 3)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年12月01日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • numpy.squeeze()
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档