python - 当数据框只有列时,shape命令不返回列号
问题描述
数据集如下所述。定义变量是考试。考试形状 = (29,2)
|Hours| |Pass|
0 |0.5 | |0|
1 |0.75| |0|
2 |1.00| |0|
3 |1.25| |0|
4 |1.50| |0|
截图中附有清晰的图像
X = exam.Hours
y = exam.Pass
X.shape = (29,) # The column number one is not mentioned
y.shape = (29,) # The column value is not mentioned
预期结果
X.shape = (29,1)
y.shape = (29,1)
解决方案
两个数组都是一维的,您希望添加一个额外的维度。您需要在阵列上取消挤压新轴。
>>> x = np.random.rand(29)
>>> x.shape
(29,)
要么使用索引:
>>> x = x[..., np.newaxis] # i.e. x[..., None]
>>> x.shape
(29, 1)
或使用np.expand_dims
实用程序:
>>> x = np.expand_dims(x, -1)
>>> x.shape
(29, 1)
推荐阅读
- r - 为什么 `pivot_wider` 不能在 `data.table` 上工作
- azure - 具有存储服务终结点策略的 VNET 中的 Azure Batch
- python - 使用 Selenium 和 Python 进行网页抓取
- bash - URLEncode 字符串中的字符以使用 bash 进行 curl 请求?
- mysql - 我想从 SQL Query 的两个表中获取总和
- nestjs - Nebular 中的 NbAuthJWTToken 什么时候过期?
- c# - WPF-退出代码-1代表什么
- ios - SwiftUI - 在 iPadOS 和 Catalyst 上使用带有鼠标/触控板的 onTapGesture 出现意外行为
- python - 删除文件中所有包含`a'的行并将其写入另一个文件
- excel - 如何将“枚举”转换为数组