SAN*_*les 80 dataset pandas scikit-learn
如何将数据从Scikit-learn Bunch对象转换为Pandas DataFrame?
from sklearn.datasets import load_iris
import pandas as pd
data = load_iris()
print(type(data))
data1 = pd. # Is there a Pandas method to accomplish this?
Run Code Online (Sandbox Code Playgroud)
Tom*_*DLT 107
手动,您可以使用pd.DataFrame构造函数,给出一个numpy数组(data)和列的名称列表(columns).要将所有内容都放在一个DataFrame中,您可以将功能和目标连接成一个numpy数组np.c_[...](注意[]):
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
# save load_iris() sklearn dataset to iris
# if you'd like to check dataset type use: type(load_iris())
# if you'd like to view list of attributes use: dir(load_iris())
iris = load_iris()
# np.c_ is the numpy concatenate function
# which is used to concat iris['data'] and iris['target'] arrays
# for pandas column argument: concat iris['feature_names'] list
# and string list (in this case one string); you can make this anything you'd like..
# the original dataset would probably call this ['Species']
data1 = pd.DataFrame(data= np.c_[iris['data'], iris['target']],
columns= iris['feature_names'] + ['target'])
Run Code Online (Sandbox Code Playgroud)
小智 50
from sklearn.datasets import load_iris
import pandas as pd
data = load_iris()
df = pd.DataFrame(data.data, columns=data.feature_names)
df.head()
Run Code Online (Sandbox Code Playgroud)
本教程可能很有用:http://www.neural.cz/dataset-exploration-boston-house-pricing.html
Nil*_*osh 41
TOMDLt的解决方案对于scikit-learn中的所有数据集都不够通用.例如,它不适用于波士顿住房数据集.我提出了一个更普遍的不同解决方案.也不需要使用numpy.
from sklearn import datasets
import pandas as pd
boston_data = datasets.load_boston()
df_boston = pd.DataFrame(boston_data.data,columns=boston_data.feature_names)
df_boston['target'] = pd.Series(boston_data.target)
df_boston.head()
Run Code Online (Sandbox Code Playgroud)
作为一般功能:
def sklearn_to_df(sklearn_dataset):
df = pd.DataFrame(sklearn_dataset.data, columns=sklearn_dataset.feature_names)
df['target'] = pd.Series(sklearn_dataset.target)
return df
df_boston = sklearn_to_df(datasets.load_boston())
Run Code Online (Sandbox Code Playgroud)
Vic*_*ong 15
我花了 2 个小时才弄明白
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
##iris.keys()
df= pd.DataFrame(data= np.c_[iris['data'], iris['target']],
columns= iris['feature_names'] + ['target'])
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
Run Code Online (Sandbox Code Playgroud)
为我的熊猫取回物种
小智 8
作为一种替代方案,我可以更轻松地包裹我的头脑:
data = load_iris()
df = pd.DataFrame(data['data'], columns=data['feature_names'])
df['target'] = data['target']
df.head()
Run Code Online (Sandbox Code Playgroud)
基本上不是从get get连接,只需用特征矩阵创建一个数据框,然后只需添加数据['whatvername']的目标列,并从数据集中获取目标值
否则使用seaborn 数据集,它们是实际的熊猫数据框:
import seaborn
iris = seaborn.load_dataset("iris")
type(iris)
# <class 'pandas.core.frame.DataFrame'>
Run Code Online (Sandbox Code Playgroud)
与scikit学习数据集对比:
from sklearn import datasets
iris = datasets.load_iris()
type(iris)
# <class 'sklearn.utils.Bunch'>
dir(iris)
# ['DESCR', 'data', 'feature_names', 'filename', 'target', 'target_names']
Run Code Online (Sandbox Code Playgroud)
小智 8
这是对我有用的简单方法。
boston = load_boston()
boston_frame = pd.DataFrame(data=boston.data, columns=boston.feature_names)
boston_frame["target"] = boston.target
Run Code Online (Sandbox Code Playgroud)
但这也适用于 load_iris。
许多解决方案要么缺少列名称,要么缺少物种目标名称。该解决方案提供了 target_name 标签。
@Ankit-mathanker的解决方案有效,但是它迭代数据帧系列“target_names”以替换整数标识符的虹膜种类。
根据这句格言“如果不需要,就不要迭代 Dataframe ”,以下解决方案利用 pd.replace() 来更简洁地完成替换。
import pandas as pd
from sklearn.datasets import load_iris
iris = load_iris()
df = pd.DataFrame(iris['data'], columns = iris['feature_names'])
df['target'] = pd.Series(iris['target'], name = 'target_values')
df['target_name'] = df['target'].replace([0,1,2],
['iris-' + species for species in iris['target_names'].tolist()])
df.head(3)
Run Code Online (Sandbox Code Playgroud)
| 萼片长度(厘米) | 萼片宽度(厘米) | 花瓣长度(厘米) | 花瓣宽度(厘米) | 目标 | 目标名称 | |
|---|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | 0 | 鸢尾 |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | 0 | 鸢尾 |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | 0 | 鸢尾 |
小智 6
这对我有用。
dataFrame = pd.dataFrame(data = np.c_[ [iris['data'],iris['target'] ],
columns=iris['feature_names'].tolist() + ['target'])
Run Code Online (Sandbox Code Playgroud)
可以使用其他方式来组合特征和目标变量np.column_stack(详细信息)
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
data = load_iris()
df = pd.DataFrame(np.column_stack((data.data, data.target)), columns = data.feature_names+['target'])
print(df.head())
Run Code Online (Sandbox Code Playgroud)
结果:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
0 5.1 3.5 1.4 0.2 0.0
1 4.9 3.0 1.4 0.2 0.0
2 4.7 3.2 1.3 0.2 0.0
3 4.6 3.1 1.5 0.2 0.0
4 5.0 3.6 1.4 0.2 0.0
Run Code Online (Sandbox Code Playgroud)
如果您需要 的字符串标签target,则可以replace通过转换target_names为dictionary并添加新列来使用:
df['label'] = df.target.replace(dict(enumerate(data.target_names)))
print(df.head())
Run Code Online (Sandbox Code Playgroud)
结果:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target label
0 5.1 3.5 1.4 0.2 0.0 setosa
1 4.9 3.0 1.4 0.2 0.0 setosa
2 4.7 3.2 1.3 0.2 0.0 setosa
3 4.6 3.1 1.5 0.2 0.0 setosa
4 5.0 3.6 1.4 0.2 0.0 setosa
Run Code Online (Sandbox Code Playgroud)
您可以使用该参数as_frame=True来获取 Pandas 数据帧。
from sklearn import datasets
X,y = datasets.load_iris(return_X_y=True) # numpy arrays
dic_data = datasets.load_iris(as_frame=True)
print(dic_data.keys())
df = dic_data['frame'] # pandas dataframe data + target
df_X = dic_data['data'] # pandas dataframe data only
ser_y = dic_data['target'] # pandas series target only
dic_data['target_names'] # numpy array
Run Code Online (Sandbox Code Playgroud)
from sklearn import datasets
fnames = [ i for i in dir(datasets) if 'load_' in i]
print(fnames)
fname = 'load_boston'
loader = getattr(datasets,fname)()
df = pd.DataFrame(loader['data'],columns= loader['feature_names'])
df['target'] = loader['target']
df.head(2)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
61379 次 |
| 最近记录: |