- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有这个数据
我正在尝试应用这个:
one_hot = pd.get_dummies(df)
但是我得到这个错误:
这是我之前的代码:
# Import modules
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn import tree
df = pd.read_csv('AllMSAData.csv')
df.head()
corr_matrix = df.corr()
corr_matrix
df.describe()
# Get featurs and targets
labels = np.array(df['CurAV'])
# Remove the labels from the features
# axis 1 refers to the columns
df = df.drop('CurAV', axis = 1)
# Saving feature names for later use
feature_list = list(df.columns)
# Convert to numpy array
df = np.array(df)
最佳答案
国际海事组织,documentation应该更新,因为它说 pd.get_dummies
接受类似数组的数据,而二维 numpy
数组是数组(尽管 there is no formal definition of array-like )。但是,它似乎不喜欢多维数组。
以这个小例子为例:
>>> df
a b c
0 a 1 d
1 b 2 e
2 c 3 f
你不能在底层的 2D numpy
数组上得到假人:
>>> pd.get_dummies(df.values)
Exception: Data must be 1-dimensional
但是你可以在数据框本身上得到假人:
>>> pd.get_dummies(df)
b a_a a_b a_c c_d c_e c_f
0 1 1 0 0 1 0 0
1 2 0 1 0 0 1 0
2 3 0 0 1 0 0 1
或者在单个列下面的一维数组上:
>>> pd.get_dummies(df['a'].values)
a b c
0 1 0 0
1 0 1 0
2 0 0 1
关于python - get_dummies(),异常 : Data must be 1-dimensional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53566735/
我正在尝试使用预先训练的模型。这就是问题发生的地方 模型不是应该接受简单的彩色图像吗?为什么它需要 4 维输入? RuntimeError T
我正在尝试实现自定义用户控件。 让我们考虑 ViewModels: public class FileViewModel { public string Name { get; set; }
我承认,我脑子有问题。我已经走得够远了,几乎可以看到隧道尽头的曙光,但我不确定要采取的下一步。 我创建了一个 SQLfiddle example here 这是 SQL 数据透视表: SET @sql
我收到 SSAS 错误“函数中指定的两个集合具有不同的维度”。 我实际上使用相同的维度,相同的层次结构(隐式),在一组中我使用“全部”成员,而在另一组中我使用叶成员。如果我更改成员标识符以显式引用(单
我有以下值,每个值都是 double 类型的标量:a1, a2, a3, a4, a5 . 我尝试使用 Numpy 连接它们,如下所示: f = np.concatenate((a1,a2,a3,a4
我有一个功能 findMaxEval我以以下方式调用:eMax0,var0=findMaxEval(np.diag(eVal0),q,bWidth=.01)哪里np.diag(eVal0)是一个形状为
我复制了spline example具有以下点(而不是随机点): var points = [[75, 15], [104, 80], [275, 80], [300, 15]]; 它工作完美。现在我
我正在尝试将值传递给后台 worker 。我之前发帖询问如何做到here 。一个答案将我引向了这段代码,但我在尝试使用它时遇到了问题。这就是我正在做的事情: Class MyParameters
问题是这样的:程序的输出是什么? #include int main() { int A[2][10]={{1,2,3,4,5,6,7,8,9,10},{11,12,13,14,15,16,17,1
我可以像这样内联创建一个 jquery 对象(这段代码有效) $('#tip').qtip({ content: el.REASON, position
我正在尝试模拟一维伊辛模型。该模型包含自旋链(100 次自旋),并使用 Mont Carlo - Metropolis 在系统能量(单一)下降或小于随机数时接受自旋翻转。在正确的程序中,能量和磁化强度
我试图从 Ruby 调用的 C 函数是这样的: void foo(double *in_array, double *out_array) 哪里: in_array 是一组数组,“foo”将使用这些数
我正在尝试校准加速度计,但我无法获得校准所需的 6 个不同加速度读数的 6 个样本值。 PreliminaryW 是一个 double[6][3] 数组,用于填充这些样本值。它是 6 x 3,因为每个
我很难想出一种有效的方法来找到二维容器中给定值的所有相邻方 block 。假设我有一个容器表示为: . . . . . . G . . . . . . . . . . . . . . . . . G
例如:一个二维数组可以想象成一堵方砖砌成的砖墙,其中每 block 砖代表我们数组中的一个坐标。 3 维数组可以同样的方式可视化为一个盒子或立方体。 但是,这是棘手的部分,您如何可视化具有多个(超过
我有以下代码通过 seaborn 创建一个表格和一个条形图。 #Building a dataframe grouped by the # of Engagement Types sales_type
我在 excel 中有一系列单元格,左列有人名,顶行有某些任务(作为数字),比如两个暗淡的数组。我想做的就是能够搜索名称和编号的任务,以查看该单元格在 VBA 中是否为空白。 在 excel 表中,我
问题总结:自定义函数的backward pass中如何处理输入和输出的维度? 根据manual ,自定义函数的基本结构如下: class MyFunc(torch.autograd.Function)
我一直在用 Javascript 开发一个项目,在达到一定的开发阶段后,代码停止工作。我已将问题范围缩小到在 Javascript 中创建和索引“多维”数组。我包含的代码只是为了测试创建数组的数组、为
package arrays; import java.util.Arrays; public class Route { int cityindex; int stadtwahl; String[]
我是一名优秀的程序员,十分优秀!