01
Seaborn自带数据集
在学习Pandas透视表的时候,大家应该注意到,我们使用的案例数据"泰坦尼克号"来自于seaborn自带的在线数据库,我们可以通过seaborn提供的函数load_dataset("数据集名称")
来获取线上相应的数据,返回给我们的是一个pandas的DataFrame对象。
import seaborn as sns
df = sns.load_dataset('titanic')
df.sample(5)
返回的DataFrame对象非常便于我们更加深入地了解数据,示例代码:
df = sns.load_dataset("tips")
print("\n[数据集基本信息]\n")
print(df.info())
print("\n[数值变量信息]\n")
print(df.describe())
print("\n[离散变量信息]\n")
for name in df.dtypes[(df.dtypes == "category") | (df.dtypes == "object")].index:
print("{} 特征值 : {}".format(name, str(df[name].unique())))
数据集描述信息如下:
[数据集基本信息]
RangeIndex: 244 entries, 0 to 243
Data columns (total 7 columns):
total_bill 244 non-null float64
tip 244 non-null float64
sex 244 non-null category
smoker 244 non-null category
day 244 non-null category
time 244 non-null category
size 244 non-null int64
dtypes: category(4), float64(2), int64(1)
[数值变量信息]
total_bill tip size
count 244.000000 244.000000 244.000000
mean 19.785943 2.998279 2.569672
std 8.902412 1.383638 0.951100
min 3.070000 1.000000 1.000000
25% 13.347500 2.000000 2.000000
50% 17.795000 2.900000 2.000000
75% 24.127500 3.562500 3.000000
max 50.810000 10.000000 6.000000
[离散变量信息]
sex 特征值 : [Female, Male]
smoker 特征值 : [No, Yes]
day 特征值 : [Sun, Sat, Thur, Fri]
time 特征值 : [Dinner, Lunch]
seaborn自带的全量数据集,如下所示:
seaborn示例数据集链接:https://github.com/mwaskom/seaborn-data
02
Sklearn自带数据集
数据加载、观察示例:
from sklearn import datasets
import pandas as pd, numpy as np
dataset = datasets.load_iris()
print("数据集包含的信息项:")
print(" ".join(dataset.keys()))
print("\n数据集描述信息:\n")
print(dataset["DESCR"])
data = dataset["data"]
target = dataset["target"]
df = pd.DataFrame(data, columns=dataset["feature_names"])
df["target"] = target
df.sample(10)
df.info()
df.describe()
sklearn小型数据集详细介绍:https://scikit-learn.org/stable/datasets/index.html#toy-datasets
20个新闻组数据集加载示例:
from sklearn.datasets import fetch_20newsgroups
from pprint import pprint
newsgroups_train = fetch_20newsgroups(subset='train')
pprint(list(newsgroups_train.targernames))
print(newsgroups_train.filenames.shape) # (11314,)
print(newsgroups_train.target.shape) # (11314,)
print(newsgroups_train.target[:10]) # [ 7 4 4 1 14 16 13 3 2 4]
print(newsgroups_train['data'][:2]) # 前三篇文章["From: lerxst@wam.umd.edu (where's my thin...
sklearn大型数据集详细介绍:https://scikit-learn.org/stable/datasets/index.html#real-world-datasets sklearn 新闻数据文本分类实战:https://www.jianshu.com/p/244180c064cf
03
其他数据源
链接:https://archive.ics.uci.edu/ml/index.php
链接:https://www.cs.waikato.ac.nz/ml/weka/datasets.html
链接:https://www.kdnuggets.com/datasets/index.html
链接:http://kdd.ics.uci.edu/