![](/img/trans.png)
[英]How to solve pandas.get_dummies Exception: Data must be 1-dimensional
[英]get_dummies(), Exception: Data must be 1-dimensional
我有這些數據
我想申請這個:
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)
海事組織, 文件應被更新,因為它說pd.get_dummies
接受的數據是陣列狀,和一個2 d numpy
陣列陣列像(盡管有陣列狀的沒有正式定義 )。 但是,它似乎不喜歡多維數組。
拿這個小例子:
>>> df
a b c
0 a 1 d
1 b 2 e
2 c 3 f
你不能在底層2D numpy
數組上得到假人:
>>> pd.get_dummies(df.values)
例外:數據必須是1維的
但是你可以在數據框本身上找到假人:
>>> 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
或者在單個列下面的1D陣列上:
>>> pd.get_dummies(df['a'].values)
a b c
0 1 0 0
1 0 1 0
2 0 0 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.