为了快速测试,调试,创建可移植示例和基准测试,R可以使用大量数据集(在Base R datasets包中).该命令library(help="datasets")在将R提示描述近100年前的数据集,其中的每一个具有相关联的描述和元数据.
Python有这样的东西吗?
Azi*_*lto 44
您可以使用rpy2包来从Python访问所有R数据集.
设置界面:
>>> from rpy2.robjects import r, pandas2ri
>>> def data(name):
... return pandas2ri.ri2py(r[name])
Run Code Online (Sandbox Code Playgroud)
然后data()使用任何数据集的可用数据集名称调用(就像在R)
>>> df = data('iris')
>>> df.describe()
Sepal.Length Sepal.Width Petal.Length Petal.Width
count 150.000000 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000 1.199333
std 0.828066 0.435866 1.765298 0.762238
min 4.300000 2.000000 1.000000 0.100000
25% 5.100000 2.800000 1.600000 0.300000
50% 5.800000 3.000000 4.350000 1.300000
75% 6.400000 3.300000 5.100000 1.800000
max 7.900000 4.400000 6.900000 2.500000
Run Code Online (Sandbox Code Playgroud)
要查看包含每个数据集的可用数据集的列表:
>>> print(r.data())
Run Code Online (Sandbox Code Playgroud)
注意:rpy2需要R使用设置R_HOME变量pandas进行安装,并且必须同时安装.
我刚刚创建了PyDataset,这是一个简单的模块,可以像Python一样轻松地从Python加载数据集R(并且它不需要R安装pandas).
要开始使用它,请安装模块:
$ pip install pydataset
然后只需加载您想要的任何数据集(目前大约有757个数据集可用):
from pydataset import data
titanic = data('titanic')
Run Code Online (Sandbox Code Playgroud)
tmt*_*prt 16
还有Scikit-Learn库中提供的数据集.
from sklearn import datasets
Run Code Online (Sandbox Code Playgroud)
此包中有多个数据集.一些玩具数据集是:
load_boston() Load and return the boston house-prices dataset (regression).
load_iris() Load and return the iris dataset (classification).
load_diabetes() Load and return the diabetes dataset (regression).
load_digits([n_class]) Load and return the digits dataset (classification).
load_linnerud() Load and return the linnerud dataset (multivariate regression).
Run Code Online (Sandbox Code Playgroud)
我最初将其发布在Pandas中的相关问题Sample Datasets上,但由于它与外部Pandas有关,因此我也将其包括在此处。
现在,有许多方法可用于访问Python中的示例数据集。就个人而言,我倾向于坚持使用我已经使用过的任何包装(通常是海豹或大熊猫)。如果您需要脱机访问,则使用Quilt安装数据集似乎是唯一的选择。
出色的绘图程序包seaborn具有多个内置样本数据集。
import seaborn as sns
iris = sns.load_dataset('iris')
iris.head()
Run Code Online (Sandbox Code Playgroud)
import seaborn as sns
iris = sns.load_dataset('iris')
iris.head()
Run Code Online (Sandbox Code Playgroud)
如果您不想导入seaborn,但仍想访问其样本数据集,则可以从其URL读取原始样本数据:
iris = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv')
Run Code Online (Sandbox Code Playgroud)
请注意,包含分类列的样本数据集的列类型被修改,通过sns.load_dataset()直接从url获取结果可能会导致结果不同。虹膜和笔尖示例数据集也可在此处的pandas github存储库中找到。
由于可以通过读取任何数据集pd.read_csv(),因此可以通过复制R数据集存储库中的URL来访问所有R的样本数据集。
加载R个样本数据集的其他方式包括
statsmodel
import statsmodels.api as sm
iris = sm.datasets.get_rdataset('iris').data
Run Code Online (Sandbox Code Playgroud)
from pydataset import data
iris = data('iris')
Run Code Online (Sandbox Code Playgroud)
scikit-learn 以numpy数组而不是pandas数据框的形式返回示例数据。
from sklearn.datasets import load_iris
iris = load_iris()
# `iris.data` holds the numerical values
# `iris.feature_names` holds the numerical column names
# `iris.target` holds the categorical (species) values (as ints)
# `iris.target_names` holds the unique categorical names
Run Code Online (Sandbox Code Playgroud)
Quilt是一个数据集管理器,旨在促进数据集管理。它包括许多共同的样品的数据集,如 几个从uciml样品库。在快速启动页面演示了如何安装并导入虹膜数据集:
# In your terminal
$ pip install quilt
$ quilt install uciml/iris
Run Code Online (Sandbox Code Playgroud)
安装数据集后,可以在本地访问它,因此,如果要脱机使用数据,这是最佳选择。
import quilt.data.uciml.iris as ir
iris = ir.tables.iris()
Run Code Online (Sandbox Code Playgroud)
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa
Run Code Online (Sandbox Code Playgroud)
被子还支持数据集版本控制,并包括每个数据集的简短描述。
| 归档时间: |
|
| 查看次数: |
38335 次 |
| 最近记录: |