9 분 소요

[공지사항] “출처: https://medium.com/@fareedkhandev/apply-40-machine-learning-models-in-two-lines-of-code-c01dad24ad99”

사용할 라이브러리에는 ML taks에 일반적으로 사용하는 특정 버전의 라이브러리가 필요하기 때문에 이 tasks에 google colab 또는 Kaggle을 사용하는 것이 좋습니다.

여기서는 한 줄의 코드로 데이터 세트에 많은 ML 모델을 구현할 수 있는 lazypredict 라이브러리를 사용할 것입니다. 이를 통해 데이터 세트에 적합한 모델에 대한 간략한 이해를 얻을 수 있습니다.

Step 1

lazypredict library를 사용하기 위해 설치해줍니다.

# !pip install lazypredict --user

Step 2

pandas를 import합니다.

import pandas as pd

Step 3

데이터세트를 로드합니다. tirthajyoti의 GitHub 계정에서 사용할 수 있는 데이터 세트를 사용하고 있습니다.

import urllib.request

urllib.request.urlretrieve("https://raw.githubusercontent.com/tirthajyoti/Machine-Learning-with-Python/master/Datasets/Mall_Customers.csv",
                           filename="Mall_Customers.csv")
('Mall_Customers.csv', <http.client.HTTPMessage at 0x2256e271790>)
df = pd.read_csv("Mall_Customers.csv")

Step 4

dataset를 간단히 print해봅니다.

print(df.shape)
df.head()
(200, 5)
CustomerID Gender Age Annual Income (k$) Spending Score (1-100)
0 1 Male 19 15 39
1 2 Male 21 15 81
2 3 Female 20 16 6
3 4 Female 23 16 77
4 5 Female 31 17 40

여기서 Y는 Spending Score 칼럼이고, 나머지 칼럼은 X입니다.

이제 X와 Y가 무엇인지 식별한 후, training 데이터 세트와 테스트 데이터 세트로 나눠 보겠습니다.

from sklearn.model_selection import train_test_split

X = df.loc[:, df.columns!= "Spending Score (1-100)"]
y = df["Spending Score (1-100)"]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

Step 5

lazypredict 라이브러리 내부에 두 개의 클래스가 있습니다. 하나는 Classification용이고, 두 번째 클래스는 Regression용입니다.

import lazypredict
from lazypredict.Supervised import LazyRegressor, LazyClassifier

여기서는 Regression 문제이기 때문에 LazyRegressor를 사용합니다.

multiple_ML_model = LazyRegressor(verbose=0, ignore_warnings=True, predictions=True)

models, predictions = multiple_ML_model.fit(X_train, X_test, y_train, y_test)
100%|██████████████████████████████████████████████████████████████████████████████████| 42/42 [00:00<00:00, 45.38it/s]

여기서 predictions=True는 각 모델의 정확도를 얻고, 모든 모델의 예측을 원한다는 의미입니다.

models 변수에는 다른 중요한 정보와 함께 모든 모델 정확도가 포함됩니다.

print(models.shape)
models
(42, 4)
Adjusted R-Squared R-Squared RMSE Time Taken
Model
LGBMRegressor 0.17 0.23 20.53 0.02
KNeighborsRegressor 0.16 0.22 20.63 0.01
AdaBoostRegressor 0.09 0.15 21.59 0.03
HistGradientBoostingRegressor 0.04 0.10 22.13 0.06
OrthogonalMatchingPursuitCV -0.02 0.05 22.82 0.01
OrthogonalMatchingPursuit -0.02 0.05 22.82 0.01
LassoLarsIC -0.03 0.04 22.95 0.01
Lasso -0.04 0.03 23.00 0.01
LassoCV -0.04 0.03 23.01 0.04
SGDRegressor -0.05 0.02 23.10 0.01
RidgeCV -0.05 0.02 23.13 0.01
Ridge -0.05 0.02 23.13 0.01
Lars -0.05 0.02 23.14 0.01
TransformedTargetRegressor -0.05 0.02 23.14 0.01
LinearRegression -0.05 0.02 23.14 0.01
BayesianRidge -0.05 0.02 23.18 0.02
PoissonRegressor -0.06 0.02 23.19 0.01
KernelRidge -0.06 0.01 23.25 0.01
ElasticNet -0.07 0.01 23.30 0.01
LarsCV -0.07 0.00 23.36 0.01
LassoLarsCV -0.07 0.00 23.36 0.01
RandomForestRegressor -0.07 -0.00 23.38 0.10
NuSVR -0.08 -0.01 23.44 0.01
ElasticNetCV -0.08 -0.01 23.50 0.04
GeneralizedLinearRegressor -0.08 -0.01 23.50 0.01
TweedieRegressor -0.08 -0.01 23.50 0.01
HuberRegressor -0.09 -0.02 23.61 0.01
GammaRegressor -0.10 -0.02 23.62 0.01
SVR -0.11 -0.04 23.78 0.01
ExtraTreesRegressor -0.11 -0.04 23.82 0.08
GradientBoostingRegressor -0.15 -0.07 24.15 0.03
BaggingRegressor -0.16 -0.08 24.32 0.02
LassoLars -0.18 -0.10 24.50 0.01
DummyRegressor -0.18 -0.10 24.50 0.01
LinearSVR -0.48 -0.38 27.49 0.01
PassiveAggressiveRegressor -0.49 -0.39 27.53 0.01
XGBRegressor -0.54 -0.43 27.98 0.07
DecisionTreeRegressor -0.91 -0.78 31.22 0.01
RANSACRegressor -1.06 -0.92 32.37 0.03
ExtraTreeRegressor -1.10 -0.96 32.70 0.01
MLPRegressor -3.88 -3.55 49.86 0.11
GaussianProcessRegressor -164.57 -153.35 290.38 0.01

보시다시피, 이 가이드는 정확도 향상보다 많은 모델을 테스트하는 방법에 더 중점을 두고 있기 때문에 regression 문제에 42개의 ML 모델을 구현했습니다.

모든 모델의 예측을 봅시다.

predictions
AdaBoostRegressor BaggingRegressor BayesianRidge DecisionTreeRegressor DummyRegressor ElasticNet ElasticNetCV ExtraTreeRegressor ExtraTreesRegressor GammaRegressor ... RANSACRegressor RandomForestRegressor Ridge RidgeCV SGDRegressor SVR TransformedTargetRegressor TweedieRegressor XGBRegressor LGBMRegressor
0 45.78 44.00 44.31 49.00 47.99 45.40 46.07 49.00 45.94 45.23 ... 46.31 45.04 43.54 43.78 43.42 47.33 43.52 45.74 47.04 43.93
1 63.56 65.60 59.41 92.00 47.99 56.24 53.59 6.00 61.21 54.89 ... 66.98 69.02 61.55 60.97 62.08 52.95 61.54 55.09 74.85 69.11
2 45.78 42.50 43.99 35.00 47.99 44.70 45.71 48.00 47.27 45.04 ... 38.99 44.96 43.34 43.45 43.16 45.30 43.40 45.52 43.24 36.01
3 46.63 32.80 48.15 35.00 47.99 47.94 48.05 13.00 40.50 47.50 ... 38.76 41.59 48.29 48.20 48.27 46.54 48.34 48.06 40.60 58.63
4 45.78 51.50 44.69 55.00 47.99 44.98 45.64 46.00 50.74 45.48 ... 58.68 49.47 43.71 44.13 43.98 46.27 43.61 46.05 52.39 45.13
5 50.76 29.40 56.80 10.00 47.99 55.34 53.48 69.00 16.10 52.99 ... 25.65 33.21 59.30 58.24 58.84 50.32 59.57 53.28 30.21 38.68
6 35.83 18.90 39.74 4.00 47.99 41.61 43.12 35.00 16.65 42.68 ... 61.63 12.92 37.85 38.49 37.76 45.36 37.77 43.02 8.90 23.93
7 51.18 38.40 57.43 41.00 47.99 55.57 53.39 41.00 45.30 53.47 ... 45.56 40.05 59.47 58.81 59.56 51.93 59.53 53.77 37.86 49.22
8 51.18 23.50 48.75 18.00 47.99 49.53 49.69 18.00 19.52 47.90 ... 16.60 33.18 47.80 48.70 48.91 47.70 47.33 48.34 19.07 58.83
9 57.05 69.80 49.40 85.00 47.99 49.26 49.34 85.00 65.07 48.25 ... 19.22 59.79 49.71 49.64 49.85 47.34 49.70 48.74 37.28 63.01
10 48.50 34.10 60.16 6.00 47.99 57.17 54.04 66.00 31.64 55.40 ... 75.58 33.11 62.73 61.88 62.87 54.54 62.83 55.57 9.33 69.62
11 18.60 15.40 41.75 16.00 47.99 43.79 45.28 13.00 17.08 43.74 ... 21.59 15.78 40.82 40.97 40.32 43.81 40.90 44.06 23.88 18.46
12 18.60 11.30 43.11 16.00 47.99 44.74 45.88 48.00 16.39 44.51 ... 27.31 15.13 42.44 42.51 41.98 44.84 42.52 44.91 23.86 17.35
13 61.43 45.40 57.44 57.00 47.99 54.95 52.89 57.00 50.81 53.51 ... 51.99 48.83 59.32 58.77 59.68 50.74 59.34 53.82 60.06 47.27
14 51.81 46.00 58.42 48.00 47.99 55.81 53.61 48.00 44.60 54.16 ... 43.36 44.94 60.83 59.97 60.91 50.35 60.97 54.37 46.22 44.17
15 45.78 51.50 44.16 59.00 47.99 44.58 45.37 46.00 50.34 45.17 ... 57.96 49.75 43.07 43.52 43.33 46.24 42.96 45.72 52.22 46.86
16 51.18 68.20 51.00 63.00 47.99 50.77 50.12 63.00 57.45 49.23 ... 32.00 65.72 52.01 51.53 51.68 49.08 52.15 49.76 45.98 54.30
17 45.78 42.60 46.57 42.00 47.99 46.54 46.87 32.00 41.79 46.58 ... 52.73 43.38 45.96 46.28 46.29 46.73 45.85 47.17 46.20 42.78
18 46.63 48.30 35.97 43.00 47.99 38.49 41.26 55.00 48.73 40.66 ... 40.57 48.34 33.39 34.21 33.28 46.90 33.31 40.62 46.17 50.64
19 51.18 41.50 47.10 8.00 47.99 47.58 48.21 85.00 40.39 46.89 ... 18.75 38.96 46.41 46.91 47.02 45.97 46.18 47.35 16.15 44.86
20 61.43 61.70 52.21 68.00 47.99 51.86 51.06 69.00 73.53 49.98 ... 22.03 73.62 53.61 52.97 53.19 49.32 53.80 50.46 78.89 64.27
21 51.81 45.20 58.52 48.00 47.99 55.86 53.63 46.00 43.83 54.22 ... 44.55 44.10 60.96 60.08 61.03 50.41 61.10 54.44 46.13 44.17
22 51.18 53.80 58.57 59.00 47.99 56.30 53.76 57.00 57.21 54.27 ... 55.19 54.40 60.75 60.08 60.94 53.91 60.79 54.51 51.16 54.78
23 56.71 54.10 53.83 61.00 47.99 52.01 50.64 61.00 56.73 51.12 ... 61.71 54.67 54.78 54.59 55.22 50.38 54.72 51.66 59.94 77.56
24 48.16 38.40 49.78 1.00 47.99 49.89 49.55 1.00 12.60 48.46 ... 28.07 24.76 50.58 50.16 50.19 48.28 50.72 49.00 19.53 47.79
25 61.43 63.80 58.45 76.00 47.99 55.91 53.21 66.00 70.72 54.19 ... 70.47 71.73 60.78 59.95 60.77 54.90 60.90 54.50 81.32 69.62
26 19.44 14.20 43.49 5.00 47.99 43.89 44.71 29.00 19.11 44.79 ... 67.14 14.48 42.30 42.74 42.48 45.31 42.22 45.35 13.41 21.40
27 30.67 16.30 39.86 16.00 47.99 41.70 43.75 15.00 12.94 42.70 ... 24.50 16.69 38.71 38.82 38.11 43.03 38.86 42.92 11.67 21.13
28 57.05 55.20 53.96 61.00 47.99 52.08 50.67 61.00 58.09 51.19 ... 62.44 56.12 55.02 54.75 55.38 50.52 55.00 51.73 57.16 77.56
29 61.43 71.20 51.79 83.00 47.99 50.90 50.32 71.00 77.08 49.72 ... 26.72 75.94 53.17 52.47 52.78 48.57 53.40 50.23 98.34 74.70
30 65.16 74.70 59.10 73.00 47.99 55.96 53.36 73.00 76.76 54.67 ... 67.83 81.01 61.30 60.64 61.69 53.05 61.33 54.90 76.35 69.11
31 60.52 54.60 52.49 47.00 47.99 51.17 50.24 47.00 54.66 50.22 ... 48.63 52.24 53.31 53.10 53.59 48.93 53.30 50.77 52.12 56.67
32 60.52 36.50 48.28 8.00 47.99 48.54 48.94 85.00 57.53 47.62 ... 19.17 41.12 47.48 48.20 48.47 46.77 47.12 48.08 52.51 63.01
33 49.81 81.80 51.73 42.00 47.99 50.33 49.38 75.00 57.37 49.77 ... 65.38 68.95 52.18 52.17 52.63 49.22 52.10 50.39 72.11 55.62
34 60.52 76.30 51.55 63.00 47.99 50.66 50.09 63.00 64.12 49.59 ... 31.80 58.20 52.67 52.15 52.48 48.31 52.81 50.11 71.54 62.34
35 48.16 78.00 47.74 91.00 47.99 48.24 48.30 88.00 87.27 47.23 ... 33.05 74.72 48.00 47.79 47.67 47.53 48.10 47.78 89.32 51.57
36 51.18 50.90 55.18 12.00 47.99 53.39 51.97 5.00 35.34 51.95 ... 37.39 48.19 56.96 56.28 56.94 49.75 57.09 52.36 23.44 40.79
37 45.78 45.50 43.43 48.00 47.99 44.81 45.74 48.00 45.12 44.71 ... 40.36 46.27 42.52 42.79 42.34 46.59 42.51 45.17 47.23 42.18
38 45.78 43.00 46.04 42.00 47.99 46.14 46.60 5.00 40.29 46.26 ... 50.14 42.73 45.52 45.72 45.66 46.54 45.48 46.83 42.23 42.34
39 60.52 58.80 54.93 88.00 47.99 52.82 51.20 88.00 64.45 51.83 ... 63.04 56.32 56.17 55.85 56.56 51.06 56.15 52.33 61.21 82.22
40 54.84 74.90 52.03 98.00 47.99 51.01 49.75 61.00 66.09 49.93 ... 67.55 73.62 52.87 52.58 52.87 51.51 52.91 50.56 73.61 55.84
41 61.43 64.10 53.54 88.00 47.99 52.17 51.15 97.00 85.67 50.87 ... 33.94 71.43 55.00 54.41 54.92 49.24 55.14 51.34 64.76 72.25
42 61.43 54.60 57.38 47.00 47.99 54.70 52.52 87.00 70.27 53.48 ... 64.59 58.32 59.13 58.66 59.58 52.23 59.12 53.84 66.42 61.19
43 61.43 49.50 57.12 57.00 47.99 54.67 52.66 57.00 54.96 53.30 ... 54.71 50.69 58.86 58.39 59.29 50.95 58.85 53.64 59.05 47.27
44 70.86 82.20 54.73 92.00 47.99 52.61 50.98 72.00 71.33 51.70 ... 66.14 82.85 55.98 55.63 56.31 51.11 55.97 52.22 79.85 79.52
45 51.18 54.40 50.90 63.00 47.99 50.72 50.11 63.00 57.28 49.17 ... 30.81 63.21 51.89 51.43 51.56 48.95 52.03 49.70 40.50 51.94
46 55.89 58.60 51.63 22.00 47.99 50.49 49.73 22.00 44.39 49.68 ... 50.28 43.25 52.23 52.11 52.54 48.63 52.19 50.26 19.74 52.55
47 45.78 48.80 39.62 46.00 47.99 41.65 43.27 47.00 42.77 42.61 ... 55.05 47.32 37.58 38.34 37.62 46.42 37.44 42.91 46.89 42.45
48 49.77 35.20 59.20 76.00 47.99 56.43 53.51 66.00 40.32 54.72 ... 74.98 40.22 61.58 60.78 61.68 54.45 61.68 54.98 4.58 69.62
49 60.52 60.20 51.61 63.00 47.99 50.69 50.10 63.00 65.38 49.63 ... 33.10 60.76 52.69 52.21 52.55 48.32 52.82 50.16 42.72 60.81
50 30.26 26.60 47.29 9.00 47.99 47.88 48.03 13.00 53.80 46.96 ... 34.33 25.24 47.33 47.25 47.11 47.36 47.39 47.51 22.76 59.56
51 18.60 15.10 45.45 20.00 47.99 46.56 47.18 35.00 29.11 45.86 ... 26.62 21.94 45.34 45.20 44.87 45.82 45.46 46.34 25.41 15.94
52 73.64 69.30 52.92 13.00 47.99 51.08 49.78 98.00 94.69 50.51 ... 71.52 76.01 53.96 53.58 54.08 49.72 54.02 51.13 90.30 51.50
53 51.81 52.20 54.95 52.00 47.99 53.57 51.87 57.00 56.56 51.82 ... 52.52 52.59 56.18 55.90 56.49 52.67 56.14 52.28 48.26 56.36
54 73.64 71.70 58.26 72.00 47.99 55.18 52.68 87.00 85.67 54.08 ... 73.81 81.12 60.42 59.69 60.65 52.78 60.52 54.41 85.40 64.99
55 61.43 71.10 52.40 78.00 47.99 51.34 50.60 78.00 78.15 50.11 ... 27.92 75.02 53.98 53.19 53.53 48.84 54.24 50.61 70.84 77.29
56 46.63 43.40 48.30 75.00 47.99 48.76 48.76 75.00 42.34 47.54 ... 25.24 44.08 48.97 48.50 48.37 47.39 49.18 48.08 39.35 39.71
57 45.78 43.80 45.23 41.00 47.99 45.38 45.91 46.00 45.13 45.80 ... 60.11 44.28 44.32 44.74 44.64 46.31 44.20 46.39 40.83 38.27
58 75.76 81.30 51.94 88.00 47.99 51.09 50.52 78.00 81.61 49.82 ... 23.37 81.11 53.28 52.65 52.98 48.80 53.48 50.31 95.47 85.04
59 49.81 48.20 48.59 75.00 47.99 48.40 48.50 32.00 47.29 47.74 ... 29.06 51.87 49.18 48.79 48.85 46.55 49.35 48.29 52.82 44.41

60 rows × 42 columns

이러한 예측을 사용하여 confusion matrix를 만들 수 있습니다.

Conclusions

lazypredict는 어떤 모델이 우리의 데이터 세트에 높은 성능을 보이는지 간단히 확인할 수 있고, 정확도 상위 몇개의 모델을 이용해 튜닝 과정을 거친다면 적합한 모델을 도출해내는데 도움이 될 것입니다.

태그:

카테고리:

업데이트:

댓글남기기