5 python numpy machine-learning pandas
我有这些数据
我想申请这个:
one_hot = pd.get_dummies(df)
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
这是我的代码,直到那时:
# 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)
Run Code Online (Sandbox Code Playgroud)
IMO,应该更新文档pd.get_dummies
,因为它说接受类似数组的数据,并且二维numpy
数组是类似数组的(尽管事实上没有类似数组的正式定义)。不过,它似乎不喜欢多维数组。
举这个小例子:
>>> df
a b c
0 a 1 d
1 b 2 e
2 c 3 f
Run Code Online (Sandbox Code Playgroud)
您无法在底层 2Dnumpy
数组上获取虚拟对象:
>>> pd.get_dummies(df.values)
Run Code Online (Sandbox Code Playgroud)
例外:数据必须是一维的
但是您可以在数据框本身上获得虚拟数据:
>>> 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
Run Code Online (Sandbox Code Playgroud)
或者在单个列下面的一维数组上:
>>> pd.get_dummies(df['a'].values)
a b c
0 1 0 0
1 0 1 0
2 0 0 1
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1279 次 |
最近记录: |