在使用seaborn加载内置数据集时,出现以下错误:
dataset = sns.load_dataset("iris")dataset.head()
解决方案:
一、原因需要连接外网! 挂上VPN就解决了。
二、查看源码。
def load_dataset(name, cache=True, data_home=None, **kws):"""Load an example dataset from the online repository (requires internet).This function provides quick access to a small number of example datasetsthat are useful for documenting seaborn or generating reproducible examplesfor bug reports. It is not necessary for normal usage.Note that some of the datasets have a small amount of preprocessing appliedto define a proper ordering for categorical variables.Use :func:`get_dataset_names` to see a list of available datasets.Parameters----------name : strName of the dataset (``{name}.csv`` on/mwaskom/seaborn-data).cache : boolean, optionalIf True, try to load from the local cache first, and save to the cacheif a download is required.data_home : string, optionalThe directory in which to cache data; see :func:`get_data_home`.kws : keys and values, optionalAdditional keyword arguments are passed to passed through to:func:`pandas.read_csv`.Returnsdf : :class:`pandas.DataFrame`Tabular data, possibly with some preprocessing applied."""path = ("/""mwaskom/seaborn-data/master/{}.csv")full_path = path.format(name)if cache:cache_path = os.path.join(get_data_home(data_home),os.path.basename(full_path))if not os.path.exists(cache_path):if name not in get_dataset_names():raise ValueError(f"'{name}' is not one of the example datasets.")urlretrieve(full_path, cache_path)full_path = cache_path"""
get_data_home()函数
def get_data_home(data_home=None):"""Return a path to the cache directory for example datasets.This directory is then used by :func:`load_dataset`.If the ``data_home`` argument is not specified, it tries to read from the``SEABORN_DATA`` environment variable and defaults to ``~/seaborn-data``."""if data_home is None:data_home = os.environ.get('SEABORN_DATA',os.path.join('~', 'seaborn-data'))data_home = os.path.expanduser(data_home)if not os.path.exists(data_home):os.makedirs(data_home)return data_home
def load_dataset(name, cache=True, data_home=None, **kws):
我们会发现,load_dataset()中的几个参数:
name:数据集的名称
cache==True时,
就会根据data_home的值来加载数据集,
此时需要下载seaborn-data数据集到本地(/mwaskom/seaborn-data)
然后data_home 设置为seaborn-data的本地路径就可以运行了。
dataset = sns.load_dataset(name="iris",cache=True,data_home="./seaborn-data")dataset.head()
note:当cache为True时,home_data默认路径是 C:\Users