使用 AST(抽象语法树)模块从 python 语法中提取库数据

问题描述 投票:0回答:1
"""
import sklearn as sk
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.model_selection import train_test_split
selection = ExtraTreesRegressor()
selection.fit(X,y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
from sklearn.ensemble import RandomForestRegressor
rfr = RandomForestRegressor()
rfr.fit(X_train, y_train)
from sklearn.model_selection import RandomizedSearchCV
rfrandom = RandomizedSearchCV(estimator = rfr, param_distributions = random_grid, scoring = 'neg_mean_squared_error', n_iter = 10, cv = 5, verbose = 2, n_jobs = 1)
rfrandom.fit(X_train, y_train)
"""

我想使用 ast(抽象语法树模块)获得如下所示的输出。

输出: import_inforamtion = {{library_name:sklearn},{别名: sk}, {模块:[合奏,model_selection]}, {类:[ExtraTreesRegressor,RandomForestRegressor,RandomizedSearchCV]},{Class_functionss:[fit]}, {Class_params:[X,y,X_train,y_train,估计器,param_distributions,评分,n_iter,cv,详细,n_jobs]}, {功能:[train_test_split]}, {Function_params:[X,y,test_size,random_state]},{Function_outputs:[X_train,X_test,y_train,y_test]} }

python json scikit-learn extract abstract-syntax-tree
1个回答
0
投票

我不太确定你在寻找什么,但也许是这样的:

$ ipython
Python 3.8.10 (default, May  4 2021, 00:00:00) 
Type 'copyright', 'credits' or 'license' for more information
IPython 8.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import ast

In [2]: src = """import sklearn as sk
   ...: from sklearn.ensemble import ExtraTreesRegressor
   ...: from sklearn.model_selection import train_test_split
   ...: selection = ExtraTreesRegressor()
   ...: selection.fit(X,y)
   ...: X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42)
   ...: from sklearn.ensemble import RandomForestRegressor
   ...: rfr = RandomForestRegressor()
   ...: rfr.fit(X_train, y_train)
   ...: from sklearn.model_selection import RandomizedSearchCV
   ...: rfrandom = RandomizedSearchCV(estimator = rfr, param_distributions = random_grid, scoring = 'neg_mean_squared_
   ...: error', n_iter = 10, cv = 5, verbose = 2, n_jobs = 1)
   ...: rfrandom.fit(X_train, y_train)
   ...: """

In [3]: ast.dump(ast.parse(src))
Out[3]: "Module(body=[Import(names=[alias(name='sklearn', asname='sk')]), ImportFrom(module='sklearn.ensemble', names=[alias(name='ExtraTreesRegressor', asname=None)], level=0), ImportFrom(module='sklearn.model_selection', names=[alias(name='train_test_split', asname=None)], level=0), Assign(targets=[Name(id='selection', ctx=Store())], value=Call(func=Name(id='ExtraTreesRegressor', ctx=Load()), args=[], keywords=[]), type_comment=None), Expr(value=Call(func=Attribute(value=Name(id='selection', ctx=Load()), attr='fit', ctx=Load()), args=[Name(id='X', ctx=Load()), Name(id='y', ctx=Load())], keywords=[])), Assign(targets=[Tuple(elts=[Name(id='X_train', ctx=Store()), Name(id='X_test', ctx=Store()), Name(id='y_train', ctx=Store()), Name(id='y_test', ctx=Store())], ctx=Store())], value=Call(func=Name(id='train_test_split', ctx=Load()), args=[Name(id='X', ctx=Load()), Name(id='y', ctx=Load())], keywords=[keyword(arg='test_size', value=Constant(value=0.2, kind=None)), keyword(arg='random_state', value=Constant(value=42, kind=None))]), type_comment=None), ImportFrom(module='sklearn.ensemble', names=[alias(name='RandomForestRegressor', asname=None)], level=0), Assign(targets=[Name(id='rfr', ctx=Store())], value=Call(func=Name(id='RandomForestRegressor', ctx=Load()), args=[], keywords=[]), type_comment=None), Expr(value=Call(func=Attribute(value=Name(id='rfr', ctx=Load()), attr='fit', ctx=Load()), args=[Name(id='X_train', ctx=Load()), Name(id='y_train', ctx=Load())], keywords=[])), ImportFrom(module='sklearn.model_selection', names=[alias(name='RandomizedSearchCV', asname=None)], level=0), Assign(targets=[Name(id='rfrandom', ctx=Store())], value=Call(func=Name(id='RandomizedSearchCV', ctx=Load()), args=[], keywords=[keyword(arg='estimator', value=Name(id='rfr', ctx=Load())), keyword(arg='param_distributions', value=Name(id='random_grid', ctx=Load())), keyword(arg='scoring', value=Constant(value='neg_mean_squared_error', kind=None)), keyword(arg='n_iter', value=Constant(value=10, kind=None)), keyword(arg='cv', value=Constant(value=5, kind=None)), keyword(arg='verbose', value=Constant(value=2, kind=None)), keyword(arg='n_jobs', value=Constant(value=1, kind=None))]), type_comment=None), Expr(value=Call(func=Attribute(value=Name(id='rfrandom', ctx=Load()), attr='fit', ctx=Load()), args=[Name(id='X_train', ctx=Load()), Name(id='y_train', ctx=Load())], keywords=[]))], type_ignores=[])"

In [4]: 
© www.soinside.com 2019 - 2024. All rights reserved.