每次在进行数据分析编写代码时,一个令人比较头疼的问题就是需要引入一部分第三方库,虽然这并不是一个困难的事情,但重复进行这样一个步骤总是让我们感到困难,有些时候我们因为疏忽大意,甚至也会忘记一些库的引用.
所以,接下来引入一个非常好用的第三方库----pyforest.
pyforest支持大部分流行的数据科学库,比如pandas,numpy,matplotlib,seaborn,sklearn,tensorflow等等,以及常用的辅助库如os,sys,re,pickle等.
他会根据我们的使用自动地导入相应的第三方库,在Jupyter当中会将导入语句默认添加到第一个单元当中,前提是我们需要提前安装好其他的库,而如果我们不使用其他的库,则它们将不会被导入.
pyforest体积很小,安装起来很方便也很快捷.
在终端当中输入下面这个语句即可.
pip install pyforest
windows系统的IDLE环境可以在cmd命令行里安装:
!pip insatll pyforest
如果已经安装过,会有如下的显示:
请注意,此命令还会将pyforest添加到你的IPython的默认启动设置中.
这样我们就准备好了,接下来就可以使用了!
在python当中直接输入:
from pyforest import *
我们便可直接使用,根据代码自动地导入Python数据科学库。
如果你使用的是Jupyter或IPython,你甚至可以跳过这一行,因为pyforest会将自己添加到自动启动中。
同样,如果我们想要知道我们在使用过程中使用了哪些第三方库,我们可以输入active_imports(),它将为我们自动显示自动库的导入情况
示例:
a=np.array([1,2])
b=np.array([2,4])
plt.plot(a,b)
active_imports()
得到结果:
[‘import numpy as np’,
‘import matplotlib.pyplot as plt’]
lazy_imports()
[‘import plotly as py’,
‘from sklearn.linear_model import Lasso’,
‘from sklearn import svm’,
‘import pandas as pd’,
‘from dask import dataframe as dd’,
‘from sklearn.model_selection import RandomizedSearchCV’,
‘import imutils’,
‘from scipy import stats’,
‘from sklearn.linear_model import ElasticNet’,
‘from sklearn.linear_model import RidgeCV’,
‘import statsmodels.api as sm’,
‘from sklearn.preprocessing import StandardScaler’,
‘from pathlib import Path’,
‘import torch’,
‘import bokeh’,
‘import cv2’,
‘from sklearn.linear_model import LogisticRegression’,
‘from openpyxl import load_workbook’,
‘import pydot’,
‘import sklearn’,
‘from fbprophet import Prophet’,
‘import textblob’,
‘import statistics’,
‘from sklearn.linear_model import Ridge’,
‘import os’,
‘from sklearn.ensemble import RandomForestRegressor’,
‘from PIL import Image’,
‘from statsmodels.tsa.arima_model import ARIMA’,
‘from sklearn.model_selection import StratifiedKFold’,
‘from sklearn.model_selection import cross_val_score’,
‘from sklearn.preprocessing import OneHotEncoder’,
‘from sklearn.linear_model import LassoCV’,
‘from sklearn import metrics’,
‘from sklearn.feature_extraction.text import CountVectorizer’,
‘import seaborn as sns’,
‘from sklearn.ensemble import GradientBoostingRegressor’,
‘from sklearn.preprocessing import PolynomialFeatures’,
‘from sklearn.cluster import KMeans’,
‘import awswrangler as wr’,
‘import nltk’,
‘import glob’,
‘import tqdm’,
‘import keras’,
‘from sklearn.preprocessing import MinMaxScaler’,
‘import datetime as dt’,
‘import plotly.express as px’,
‘from sklearn.preprocessing import RobustScaler’,
‘from xlrd import open_workbook’,
‘from sklearn.preprocessing import LabelEncoder’,
‘from sklearn.ensemble import RandomForestClassifier’,
‘import gensim’,
‘from sklearn.impute import SimpleImputer’,
‘import skimage’,
‘from pyspark import SparkContext’,
‘import plotly.graph_objs as go’,
‘import fbprophet’,
‘from sklearn.decomposition import PCA’,
‘import xgboost as xgb’,
‘import lightgbm as lgb’,
‘from sklearn.model_selection import GridSearchCV’,
‘import dash’,
‘from sklearn.manifold import TSNE’,
‘from sklearn.model_selection import KFold’,
‘import fastai’,
‘from scipy import signal as sg’,
‘import sys’,
‘from sklearn.linear_model import LinearRegression’,
‘from sklearn.model_selection import train_test_split’,
‘from sklearn.feature_extraction.text import TfidfVectorizer’,
‘from sklearn.ensemble import GradientBoostingClassifier’,
‘import spacy’,
‘import matplotlib as mpl’,
‘import re’,
‘import pickle’,
‘from sklearn.linear_model import ElasticNetCV’,
‘import altair as alt’]
如果pyforest真的不包含指定的库也没有关系,pyforest支持向其中添加库。操作方法也很简单,找到pyforest库的user_imports.py文件,然后添加一个语句就好了,比如像下面这样:
只要在_imports.py文件当中找到下列语句,在这个语句上面添加相应的库就可以了
#############################
### User-specific imports ###
#############################
# You can save your own imports in ~/.pyforest/user_imports.py
# Please note: imports in ~/.pyforest/user_imports.py take precedence over the
# imports above.
如图,如如果我们添加xxx库,只要输入就可以.
xxx=LazyImport("import xxx")