我要和import说再见了!这个Python库能让我偷偷懒
我要和import说再见了!这个Python库能让我偷偷懒
不知道大家有没有遇到过这样一个问题,每次重新开启一个建模流程或者分析过程时,会重新敲一遍import或者将之前的import代码copy进去。虽然已经用的滚花烂熟了,但是确耗费不必要的时间。
本篇给大家介绍一个可以偷懒的python库:pyforest
一个示例
看到了吧,开头什么都没import,依然可以正常使用常用库。
安装
需要安装Python3.6以上的版本,然后终端运行:
1 |
<span class="hljs-attribute">pip</span> install pyforest |
使用方法
非常简单!!!
一行代码就可以解决。
1 |
<span class="hljs-keyword">from</span> pyforest <span class="hljs-keyword">import</span> * |
如果使用Jupyter或IPython,甚至可以跳过此行,因为pyforest会将其自身添加到自动启动中。
而且,完成脚本后,可以通过以下方式导出所有导入语句:
1 |
<span class="hljs-selector-tag">active_imports</span>() |
通过这个指令,我们就可以看到脚本所涉及到的全部已经被我省略的库。
好了,你可能会问,如果需要的库不在pyforest中怎么办?
首先,pyforest支持大部分流行的数据科学库,比如pandas,numpy,matplotlib,seaborn,sklearn,tensorflow等等,以及常用的辅助库如os,sys,re,pickle等。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
### Data Wrangling pd = LazyImport(<span class="hljs-string">"import pandas as pd"</span>) np = LazyImport(<span class="hljs-string">"import numpy as np"</span>) dd = LazyImport(<span class="hljs-string">"from dask import dataframe as dd"</span>) SparkContext = LazyImport(<span class="hljs-string">"from pyspark import SparkContext"</span>) load_workbook = LazyImport(<span class="hljs-string">"from openpyxl import load_workbook"</span>) ### Data Visualization and Plottingmpl = LazyImport(<span class="hljs-string">"import matplotlib as mpl"</span>) plt = LazyImport(<span class="hljs-string">"import matplotlib.pyplot as plt"</span>) sns = LazyImport(<span class="hljs-string">"import seaborn as sns"</span>) py = LazyImport(<span class="hljs-string">"import plotly as py"</span>) <span class="hljs-keyword">go</span> = LazyImport(<span class="hljs-string">"import plotly.graph_objs as go"</span>) px = LazyImport(<span class="hljs-string">"import plotly.express as px"</span>) dash = LazyImport(<span class="hljs-string">"import dash"</span>) bokeh = LazyImport(<span class="hljs-string">"import bokeh"</span>) alt = LazyImport(<span class="hljs-string">"import altair as alt"</span>) pydot = LazyImport(<span class="hljs-string">"import pydot"</span>) # statisticsstatistics = LazyImport(<span class="hljs-string">"import statistics"</span>) ### Machine Learningsklearn = LazyImport(<span class="hljs-string">"import sklearn"</span>) OneHotEncoder = LazyImport(<span class="hljs-string">"from sklearn.preprocessing import OneHotEncoder"</span>) TSNE = LazyImport(<span class="hljs-string">"from sklearn.manifold import TSNE"</span>) train_test_split = LazyImport(<span class="hljs-string">"from sklearn.model_selection import train_test_split"</span>) svm = LazyImport(<span class="hljs-string">"from sklearn import svm"</span>) GradientBoostingClassifier = LazyImport( <span class="hljs-string">"from sklearn.ensemble import GradientBoostingClassifier"</span> )GradientBoostingRegressor = LazyImport( <span class="hljs-string">"from sklearn.ensemble import GradientBoostingRegressor"</span> )RandomForestClassifier = LazyImport( <span class="hljs-string">"from sklearn.ensemble import RandomForestClassifier"</span> )RandomForestRegressor = LazyImport(<span class="hljs-string">"from sklearn.ensemble import RandomForestRegressor"</span>) TfidfVectorizer = LazyImport( <span class="hljs-string">"from sklearn.feature_extraction.text import TfidfVectorizer"</span> )# TODO: add all the other most important sklearn objects# TODO: add separate sections within machine learning viz. Classification, Regression, Error Functions, Clustering# Deep Learningtf = LazyImport(<span class="hljs-string">"import tensorflow as tf"</span>) keras = LazyImport(<span class="hljs-string">"import keras"</span>) # NLPnltk = LazyImport(<span class="hljs-string">"import nltk"</span>) gensim = LazyImport(<span class="hljs-string">"import gensim"</span>) spacy = LazyImport(<span class="hljs-string">"import spacy"</span>) re = LazyImport(<span class="hljs-string">"import re"</span>) ### Helpersys = LazyImport(<span class="hljs-string">"import sys"</span>) os = LazyImport(<span class="hljs-string">"import os"</span>) re = LazyImport(<span class="hljs-string">"import re"</span>) glob = LazyImport(<span class="hljs-string">"import glob"</span>) Path = LazyImport(<span class="hljs-string">"from pathlib import Path"</span>) pickle = LazyImport(<span class="hljs-string">"import pickle"</span>) dt = LazyImport(<span class="hljs-string">"import datetime as dt"</span>) tqdm = LazyImport(<span class="hljs-string">"import tqdm"</span>) |
其次,如果真的没有也没关系,pyforest支持向其中添加库。操作方法也很简单,找到pyforest库的user_imports.py文件,然后添加一个语句就好了,比如像下面这样:
1 2 3 4 5 6 |
<span class="hljs-comment">######</span><span class="hljs-comment">######</span><span class="hljs-comment">######</span><span class="hljs-comment">######</span><span class="hljs-comment">#####</span> <span class="hljs-comment">#</span><span class="hljs-comment">## User-specific imports ###</span> <span class="hljs-comment">#############################</span> <span class="hljs-comment"># You can save your own imports in ~/.pyforest/user_imports.py</span> <span class="hljs-comment"># Please note: imports in ~/.pyforest/user_imports.py take precedence over the</span> <span class="hljs-comment"># imports above.</span> |
所以我们可以根据平时自己的使用习惯添加一套属于自己的万能import,爽歪歪了。
有的同学可能还会问,所有的库添加进入运行速度会不会变慢?
答案是不会,因为只有你后面真正使用了到了pyforest里的包含的库,程序才会真正import,否则不会。
以上就是偷懒神器的介绍和使用方法,有兴趣的伙伴可以试试了!
https://m.toutiaocdn.com/i6779111283670123012/?app=news_article×tamp=1591053021&use_new_style=1&req_id=2020060207102101002004701530FEBCF4&group_id=6779111283670123012&tt_from=android_share&utm_medium=toutiao_android&utm_campaign=client_share