[sklearn] 알고리즘이 분류, 회귀, 이상치탐지 중 어떤건가?

Python/Scikit-Learn 2021. 1. 24. 22:11

아주 희박한 확률로, model 객체를 받았는데, 이게 분류 (classifier) 모델인지, 회귀 (regressor) 모델인지, 이상치 (outlier_detector) 인지를 프로그램적으로 판단해야할 때가 있다. 그럴 때를 위해 sklearn에는 is_classifier, is_regressor, is_outlier_detector 총 3가지의 validation 툴이 존재한다. from sklearn.base import is_classifier, is_regressor, is_outlier_detector from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor, IsolationForest models = [Rando..

Article Thumbnail
[sklearn] One Hot Encoding 되돌리기

Python/Scikit-Learn 2021. 1. 24. 21:56

scikit-learn에서 OneHotEncoder를 사용하고 되돌리는 방법에 대해 알아보자. 머신러닝을 할 때, sklearn.preprocessing.OneHotEncoder 또는 sklearn.preprocessing.LabelEncoder를 사용하여 학습을 하게된다면, 최종 예측 결과도 one-hot encoded 된 결과로 나오게 되는데, 간단하게 inverse_transform을 통해 원래 형식으로 바꿀 수 있다. 아래처럼 샘플 시리즈 ["a", "b", "c"]가 있다고 할 때, OneHotEncoder 또는 LabelEncoder를 사용하게 된다면: import pandas as pd from sklearn.preprocessing import LabelEncoder, OneHotEncod..

Article Thumbnail
[debugging] Expected 2D array, got 1D array instead

Python/Scikit-Learn 2019. 11. 15. 16:01

sklearn을 사용하다 보면 제목과 같은 에러 메시지를 종종 볼 수 있다. 다행히 개발자분들 께서 친절히 에러 메시지에 해결책을 써두고는 한다: from sklearn.impute import SimpleImputer imp = SimpleImputer() imp.fit_transform([1,2,3]) ValueError: Expected 2D array, got 1D array instead: array=[1. 2. 3.]. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample. fit_transform..

Article Thumbnail