728x90
728x90

help(), dir() 활용하기

들어가며

  • 빅데이터분석기사 실기 시험장에서 특정 함수명이나 함수의 사용 방법을 잊어버렸을 경우, help() 또는 dir() 함수를 이용하여 확인할 수 있다.
  • 이에 대한 내용을 정리해본다.

 

방법

0️⃣ 참고 사항

  • help() 또는 dir() 함수의 인자에 넣을 대상을 우선 import 해줘야 한다.
import sklearn # (1) 확인하고자 할 대상 불러오기
print(help(sklearn)) # (2) 사용 방법 확인
print(dir(sklearn)) # (3) 속성/메서드 목록 확인

 

1️⃣ help() 함수 사용하기

  • 객체, 모듈, 함수, 클래스 등에 대한 도움말 문서(docstring)를 확인하고 싶을 경우 help() 함수를 사용한다.
  • 어떤 함수나 클래스의 사용 방법을 알고 싶을 때 사용한다.

 

예 : 사이킷런 패키지의 도움말 문서 확인하기
import sklearn
print(help(sklearn))
Help on package sklearn:
NAME
sklearn - Configure global settings and get information about the working environment.
PACKAGE CONTENTS
__check_build (package)
_build_utils (package)
_built_with_meson
_config
_distributor_init
_isotonic
_loss (package)
_min_dependencies
base
calibration
cluster (package)
compose (package)
conftest
covariance (package)
cross_decomposition (package)
datasets (package)
decomposition (package)
discriminant_analysis
dummy
ensemble (package)
exceptions
experimental (package)
externals (package)
feature_extraction (package)
feature_selection (package)
gaussian_process (package)
impute (package)
inspection (package)
isotonic
kernel_approximation
kernel_ridge
linear_model (package)
manifold (package)
metrics (package)
mixture (package)
model_selection (package)
multiclass
multioutput
naive_bayes
neighbors (package)
neural_network (package)
pipeline
preprocessing (package)
random_projection
semi_supervised (package)
svm (package)
tests (package)
tree (package)
utils (package)
FUNCTIONS
clone(estimator, *, safe=True)
Construct a new unfitted estimator with the same parameters.
Clone does a deep copy of the model in an estimator
without actually copying attached data. It returns a new estimator
with the same parameters that has not been fitted on any data.
.. versionchanged:: 1.3
Delegates to `estimator.__sklearn_clone__` if the method exists.
Parameters
----------
estimator : {list, tuple, set} of estimator instance or a single estimator instance
The estimator or group of estimators to be cloned.
safe : bool, default=True
If safe is False, clone will fall back to a deep copy on objects
that are not estimators. Ignored if `estimator.__sklearn_clone__`
exists.
Returns
-------
estimator : object
The deep copy of the input, an estimator if input is an estimator.
Notes
-----
If the estimator's `random_state` parameter is an integer (or if the
estimator doesn't have a `random_state` parameter), an *exact clone* is
returned: the clone and the original estimator will give the exact same
results. Otherwise, *statistical clone* is returned: the clone might
return different results from the original estimator. More details can be
found in :ref:`randomness`.
Examples
--------
>>> from sklearn.base import clone
>>> from sklearn.linear_model import LogisticRegression
>>> X = [[-1, 0], [0, 1], [0, -1], [1, 0]]
>>> y = [0, 0, 1, 1]
>>> classifier = LogisticRegression().fit(X, y)
>>> cloned_classifier = clone(classifier)
>>> hasattr(classifier, "classes_")
True
>>> hasattr(cloned_classifier, "classes_")
False
>>> classifier is cloned_classifier
False
config_context(*, assume_finite=None, working_memory=None, print_changed_only=None, display=None, pairwise_dist_chunk_size=None, enable_cython_pairwise_dist=None, array_api_dispatch=None, transform_output=None, enable_metadata_routing=None, skip_parameter_validation=None)
Context manager for global scikit-learn configuration.
Parameters
----------
assume_finite : bool, default=None
If True, validation for finiteness will be skipped,
saving time, but leading to potential crashes. If
False, validation for finiteness will be performed,
avoiding error. If None, the existing value won't change.
The default value is False.
working_memory : int, default=None
If set, scikit-learn will attempt to limit the size of temporary arrays
to this number of MiB (per job when parallelised), often saving both
computation time and memory on expensive operations that can be
performed in chunks. If None, the existing value won't change.
The default value is 1024.
print_changed_only : bool, default=None
If True, only the parameters that were set to non-default
values will be printed when printing an estimator. For example,
``print(SVC())`` while True will only print 'SVC()', but would print
'SVC(C=1.0, cache_size=200, ...)' with all the non-changed parameters
when False. If None, the existing value won't change.
The default value is True.
.. versionchanged:: 0.23
Default changed from False to True.
display : {'text', 'diagram'}, default=None
If 'diagram', estimators will be displayed as a diagram in a Jupyter
lab or notebook context. If 'text', estimators will be displayed as
text. If None, the existing value won't change.
The default value is 'diagram'.
.. versionadded:: 0.23
pairwise_dist_chunk_size : int, default=None
The number of row vectors per chunk for the accelerated pairwise-
distances reduction backend. Default is 256 (suitable for most of
modern laptops' caches and architectures).
Intended for easier benchmarking and testing of scikit-learn internals.
End users are not expected to benefit from customizing this configuration
setting.
.. versionadded:: 1.1
enable_cython_pairwise_dist : bool, default=None
Use the accelerated pairwise-distances reduction backend when
possible. Global default: True.
Intended for easier benchmarking and testing of scikit-learn internals.
End users are not expected to benefit from customizing this configuration
setting.
.. versionadded:: 1.1
array_api_dispatch : bool, default=None
Use Array API dispatching when inputs follow the Array API standard.
Default is False.
See the :ref:`User Guide <array_api>` for more details.
.. versionadded:: 1.2
transform_output : str, default=None
Configure output of `transform` and `fit_transform`.
See :ref:`sphx_glr_auto_examples_miscellaneous_plot_set_output.py`
for an example on how to use the API.
- `"default"`: Default output format of a transformer
- `"pandas"`: DataFrame output
- `"polars"`: Polars output
- `None`: Transform configuration is unchanged
.. versionadded:: 1.2
.. versionadded:: 1.4
`"polars"` option was added.
enable_metadata_routing : bool, default=None
Enable metadata routing. By default this feature is disabled.
Refer to :ref:`metadata routing user guide <metadata_routing>` for more
details.
- `True`: Metadata routing is enabled
- `False`: Metadata routing is disabled, use the old syntax.
- `None`: Configuration is unchanged
.. versionadded:: 1.3
skip_parameter_validation : bool, default=None
If `True`, disable the validation of the hyper-parameters' types and values in
the fit method of estimators and for arguments passed to public helper
functions. It can save time in some situations but can lead to low level
crashes and exceptions with confusing error messages.
Note that for data parameters, such as `X` and `y`, only type validation is
skipped but validation with `check_array` will continue to run.
.. versionadded:: 1.3
Yields
------
None.
See Also
--------
set_config : Set global scikit-learn configuration.
get_config : Retrieve current values of the global configuration.
Notes
-----
All settings, not just those presently modified, will be returned to
their previous values when the context manager is exited.
Examples
--------
>>> import sklearn
>>> from sklearn.utils.validation import assert_all_finite
>>> with sklearn.config_context(assume_finite=True):
... assert_all_finite([float('nan')])
>>> with sklearn.config_context(assume_finite=True):
... with sklearn.config_context(assume_finite=False):
... assert_all_finite([float('nan')])
Traceback (most recent call last):
...
ValueError: Input contains NaN...
get_config()
Retrieve current values for configuration set by :func:`set_config`.
Returns
-------
config : dict
Keys are parameter names that can be passed to :func:`set_config`.
See Also
--------
config_context : Context manager for global scikit-learn configuration.
set_config : Set global scikit-learn configuration.
Examples
--------
>>> import sklearn
>>> config = sklearn.get_config()
>>> config.keys()
dict_keys([...])
set_config(assume_finite=None, working_memory=None, print_changed_only=None, display=None, pairwise_dist_chunk_size=None, enable_cython_pairwise_dist=None, array_api_dispatch=None, transform_output=None, enable_metadata_routing=None, skip_parameter_validation=None)
Set global scikit-learn configuration.
.. versionadded:: 0.19
Parameters
----------
assume_finite : bool, default=None
If True, validation for finiteness will be skipped,
saving time, but leading to potential crashes. If
False, validation for finiteness will be performed,
avoiding error. Global default: False.
.. versionadded:: 0.19
working_memory : int, default=None
If set, scikit-learn will attempt to limit the size of temporary arrays
to this number of MiB (per job when parallelised), often saving both
computation time and memory on expensive operations that can be
performed in chunks. Global default: 1024.
.. versionadded:: 0.20
print_changed_only : bool, default=None
If True, only the parameters that were set to non-default
values will be printed when printing an estimator. For example,
``print(SVC())`` while True will only print 'SVC()' while the default
behaviour would be to print 'SVC(C=1.0, cache_size=200, ...)' with
all the non-changed parameters.
.. versionadded:: 0.21
display : {'text', 'diagram'}, default=None
If 'diagram', estimators will be displayed as a diagram in a Jupyter
lab or notebook context. If 'text', estimators will be displayed as
text. Default is 'diagram'.
.. versionadded:: 0.23
pairwise_dist_chunk_size : int, default=None
The number of row vectors per chunk for the accelerated pairwise-
distances reduction backend. Default is 256 (suitable for most of
modern laptops' caches and architectures).
Intended for easier benchmarking and testing of scikit-learn internals.
End users are not expected to benefit from customizing this configuration
setting.
.. versionadded:: 1.1
enable_cython_pairwise_dist : bool, default=None
Use the accelerated pairwise-distances reduction backend when
possible. Global default: True.
Intended for easier benchmarking and testing of scikit-learn internals.
End users are not expected to benefit from customizing this configuration
setting.
.. versionadded:: 1.1
array_api_dispatch : bool, default=None
Use Array API dispatching when inputs follow the Array API standard.
Default is False.
See the :ref:`User Guide <array_api>` for more details.
.. versionadded:: 1.2
transform_output : str, default=None
Configure output of `transform` and `fit_transform`.
See :ref:`sphx_glr_auto_examples_miscellaneous_plot_set_output.py`
for an example on how to use the API.
- `"default"`: Default output format of a transformer
- `"pandas"`: DataFrame output
- `"polars"`: Polars output
- `None`: Transform configuration is unchanged
.. versionadded:: 1.2
.. versionadded:: 1.4
`"polars"` option was added.
enable_metadata_routing : bool, default=None
Enable metadata routing. By default this feature is disabled.
Refer to :ref:`metadata routing user guide <metadata_routing>` for more
details.
- `True`: Metadata routing is enabled
- `False`: Metadata routing is disabled, use the old syntax.
- `None`: Configuration is unchanged
.. versionadded:: 1.3
skip_parameter_validation : bool, default=None
If `True`, disable the validation of the hyper-parameters' types and values in
the fit method of estimators and for arguments passed to public helper
functions. It can save time in some situations but can lead to low level
crashes and exceptions with confusing error messages.
Note that for data parameters, such as `X` and `y`, only type validation is
skipped but validation with `check_array` will continue to run.
.. versionadded:: 1.3
See Also
--------
config_context : Context manager for global scikit-learn configuration.
get_config : Retrieve current values of the global configuration.
Examples
--------
>>> from sklearn import set_config
>>> set_config(display='diagram') # doctest: +SKIP
show_versions()
Print useful debugging information"
.. versionadded:: 0.20
Examples
--------
>>> from sklearn import show_versions
>>> show_versions() # doctest: +SKIP
DATA
__SKLEARN_SETUP__ = False
__all__ = ['calibration', 'cluster', 'covariance', 'cross_decompositio...
VERSION
1.5.2
FILE
/usr/local/lib/python3.12/site-packages/sklearn/__init__.py

 

예 2 : 사이킷런 패키지의 preprocessing 모듈 도움말 문서 확인하기
import sklearn.preprocessing
print(help(sklearn.preprocessing))
Help on package sklearn.preprocessing in sklearn:
NAME
sklearn.preprocessing - Methods for scaling, centering, normalization, binarization, and more.
PACKAGE CONTENTS
_csr_polynomial_expansion
_data
_discretization
_encoders
_function_transformer
_label
_polynomial
_target_encoder
_target_encoder_fast
tests (package)
CLASSES
sklearn.base.BaseEstimator(sklearn.utils._estimator_html_repr._HTMLDocumentationLinkMixin, sklearn.utils._metadata_requests._MetadataRequester)
sklearn.preprocessing._data.Binarizer(sklearn.base.OneToOneFeatureMixin, sklearn.base.TransformerMixin, sklearn.base.BaseEstimator)
sklearn.preprocessing._data.KernelCenterer(sklearn.base.ClassNamePrefixFeaturesOutMixin, sklearn.base.TransformerMixin, sklearn.base.BaseEstimator)
sklearn.preprocessing._data.MaxAbsScaler(sklearn.base.OneToOneFeatureMixin, sklearn.base.TransformerMixin, sklearn.base.BaseEstimator)
sklearn.preprocessing._data.MinMaxScaler(sklearn.base.OneToOneFeatureMixin, sklearn.base.TransformerMixin, sklearn.base.BaseEstimator)
sklearn.preprocessing._data.Normalizer(sklearn.base.OneToOneFeatureMixin, sklearn.base.TransformerMixin, sklearn.base.BaseEstimator)
sklearn.preprocessing._data.PowerTransformer(sklearn.base.OneToOneFeatureMixin, sklearn.base.TransformerMixin, sklearn.base.BaseEstimator)
sklearn.preprocessing._data.QuantileTransformer(sklearn.base.OneToOneFeatureMixin, sklearn.base.Tran
...

 

2️⃣ dir() 함수 사용하기

  • 객체, 모듈 등이 가진 속성(Attribute)메서드(Method)리스트 형태로 확인하고 싶을 때 dir() 함수를 사용한다.
  • 객체의 구조나 사용할 수 있는 메서드를 확인하고자 할 때 사용한다.

 

예 1 : 사이킷런 패키지의 속성 및 메서드 목록 확인하기
import sklearn
print(dir(sklearn))
['_BUILT_WITH_MESON', '__SKLEARN_SETUP__', '__all__', '__builtins__', '__cached__', '__check_build', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_built_with_meson', '_config', '_distributor_init', 'base', 'clone', 'config_context', 'exceptions', 'externals', 'get_config', 'logger', 'logging', 'os', 'random', 'set_config', 'setup_module', 'show_versions', 'sklearn', 'sys', 'utils']

 

예 2: 사이킷런 패키지의 preprocessing 모듈의 속성 및 메서드 목록 확인하기
import sklearn.preprocessing
print(dir(sklearn.preprocessing))
['Binarizer', 'FunctionTransformer', 'KBinsDiscretizer', 'KernelCenterer', 'LabelBinarizer', 'LabelEncoder', 'MaxAbsScaler', 'MinMaxScaler', 'MultiLabelBinarizer', 'Normalizer', 'OneHotEncoder', 'OrdinalEncoder', 'PolynomialFeatures', 'PowerTransformer', 'QuantileTransformer', 'RobustScaler', 'SplineTransformer', 'StandardScaler', 'TargetEncoder', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_csr_polynomial_expansion', '_data', '_discretization', '_encoders', '_function_transformer', '_label', '_polynomial', '_target_encoder', '_target_encoder_fast', 'add_dummy_feature', 'binarize', 'label_binarize', 'maxabs_scale', 'minmax_scale', 'normalize', 'power_transform', 'quantile_transform', 'robust_scale', 'scale']

 

정리

  • 사이킷런(sklearn) 패키지preprocessing 모듈에 포함되어 있는 MinMaxScaler 함수의 사용 방법을 잊어 버렸을 경우, help(sklearn.preprocessing.MinMaxScaler) 을 이용하여 확인한다.
    • 사용 예시 코드까지 확인할 수 있다.
import sklearn.preprocessing.MinMaxScaler
print(help(sklearn.preprocessing.MinMaxScaler))

 

  • 사이킷런(sklearn) 패키지preprocessing 모듈에 포함되어 있는 특정 함수명을 잊어 버렸을 경우, dir(sklearn.preprocessing) 을 이용하여 확인한다.
import sklearn.preprocessing
print(dir(sklearn.preprocessing))

 

참고 사이트

 

Built-in Functions

The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.,,,, Built-in Functions,,, A, abs(), aiter(), all(), a...

docs.python.org

 

728x90
728x90

help(), dir() 활용하기들어가며방법0️⃣ 참고 사항1️⃣ help() 함수 사용하기2️⃣ dir() 함수 사용하기정리참고 사이트