ํ์ต๋ชฉํ
๋ชฉ์ฐจ
๋ค์ํ ๋จธ์ ๋ฌ๋ ์๊ณ ๋ฆฌ์ฆ
์ฌ์ดํท๋ฐ์์ ๊ฐ์ด๋ํ๋ ๋จธ์ ๋ฌ๋ ์๊ณ ๋ฆฌ์ฆ
Hello Scikit-learn
์ฌ์ดํท๋ฐ์ ์ฃผ์ ๋ชจ๋
ํ๋ จ ๋ฐ์ดํฐ์ ํ ์คํธ ๋ฐ์ดํฐ ๋ถ๋ฆฌํ๊ธฐ
์๋์ ์๊ณ ๋ฆฌ์ฆ์ ํฉ์ณ์ ์ฌ์ฉํ๊ธฐ๋ ํจ
์ง๋ํ์ต์ผ๋ก ์งํํ๋ค๊ฐ ์ฐจ์๊ณผ ํน์ง(Feature)์ ์๊ฐ ๋ง์ผ๋ฉด ๋น์ง๋ ํ์ต์ผ๋ก ์ ํ
์ฐธ๊ณ
์ต์ ์ ์๊ณ ๋ฆฌ์ฆ์ ๊ณ ๋ฅด๊ธฐ ์ํ ์นํธํค
์ต๊ณ ์ ์๊ณ ๋ฆฌ์ฆ์ ์ฐพ๋ ๋จํ๋์ ํ์คํ ๋ฐฉ๋ฒ์ ๋ชจ๋ ์๊ณ ๋ฆฌ์ฆ์ ์๋ํด๋ณด๋ ๊ฒ
1. If [path label] then use [algorithm]
(๋ง์ฝ <๊ฒฝ๋ก ๋ ์ด๋ธ>์ด๋ฉด <์๊ณ ๋ฆฌ์ฆ>์ ์ฌ์ฉํ๋ค)
2. If you want to perform dimension reduction then use principal component analysis.
(์ฐจ์ ์ถ์๋ฅผ ์ํํ๊ณ ์ถ์ผ๋ฉด ์ฃผ์ฑ๋ถ ๋ถ์์ ์ฌ์ฉํ๋ค.)
3. If you need a numeric prediction quickly, use decision trees or logistic regression.
(์ ์ํ ์์น ์์ธก์ด ํ์ํ๋ฉด ์์ฌ๊ฒฐ์ ํธ๋ฆฌ ๋๋ ๋ก์ง์คํฑ ํ๊ท๋ฅผ ์ฌ์ฉํ๋ค.)
4. If you need a hierarchical result, use hierarchical clustering.
(๊ณ์ธต์ ๊ฒฐ๊ณผ๊ฐ ํ์ํ๋ฉด ๊ณ์ธต์ ํด๋ฌ์คํฐ๋ง์ ์ฌ์ฉํ๋ค.)
โ์ปจ๋ณผ๋ฃจ์
์ ๊ฒฝ๋ง(convolution neural network) ์ํคํ
์ฒ(์ด๋ฏธ์ง ์ถ์ฒ: wikipedia creative commons)
SVD
์ ์ฌ ๋๋ฆฌํด๋ ํ ๋น(LDA)
Scikit-Learn์์๋ ์ด๋ป๊ฒ ์๊ณ ๋ฆฌ์ฆ์ ๋ถ๋ฅ?
pip install scikit-learn
import sklearn
print(sklearn.__version__)
1.0.2
์ฌ์ดํค๋ฐ์์ ํ๋ จ ๋ฐ์ดํฐ์ ํ
์คํธ ๋ฐ์ดํฐ๋ฅผ ๋๋๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๊ฒ์
train_test_split
์ฌ์ดํท๋ฐ์์
transformer()์ Estimator๊ฐ์ฒด์ fit()๊ณผ predict()๋ฉ์๋๊ฐ ์ค์ํ๊ฒ ๊ฐ์ต๋๋ค. ๋ชจ๋ธ ์
๋ ์
์์ train_test_split()
์ด๋ ํจ์๋ฅผ ์ด์ฉํด ํ๋ จ๋ฐ์ดํฐ์ ํ
์คํธ๋ฐ์ดํฐ๋ฅผ ๋๋คํ๊ฒ ์์ด์ค๋๋ค
์ฌ์ดํท๋ฐ์ ํ์ด์ฌ ๊ธฐ๋ฐ ๋จธ์ ๋ฌ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก Scipy ๋ฐ NumPy ์ ๋น์ทํ ๋ฐ์ดํฐ ํํ๊ณผ ์ํ ๊ด๋ จ ํจ์๋ฅผ ๊ฐ๊ณ ์์ต๋๋ค. ์ผ๋ฐ์ ์ผ๋ก ๋จธ์ ๋ฌ๋์์ ๋ฐ์ดํฐ ๊ฐ๊ณต(ETL)์ ๊ฑฐ์ณ ๋ชจ๋ธ์ ํ๋ จํ๊ณ ์์ธกํ๋ ๊ณผ์ ์ ๊ฑฐ์น๋๋ฐ ETL๋ถ๋ถ์ ScikitLearn์ transformer()๋ฅผ ์ ๊ณตํ๊ณ , ๋ชจ๋ธ์ ํ๋ จ๊ณผ ์์ธก์ Estimator ๊ฐ์ฒด๋ฅผ ํตํด ์ํ๋๋ฉฐ, Estimator์๋ ๊ฐ๊ฐ fit()(ํ๋ จ), predict()(์์ธก)์ ํํ๋ ๋ฉ์๋๊ฐ ์์ต๋๋ค. ๋ชจ๋ธ์ ํ๋ จ๊ณผ ์์ธก์ด ๋๋๋ฉด ์ด 2๊ฐ์ง๋ ์์ ์ Pipeline()์ผ๋ก ๋ฌถ์ด ๊ฒ์ฆ์ ์ํํฉ๋๋ค.
๋ฐ์ดํฐ์
โ๏ธํน์ฑ ํ๋ ฌ X์ n_samples์ ํ๊ฒ ๋ฒกํฐ y์ n_samples๋ ๋์ผํด์ผ ํจ
# ํ๊ท๋ชจ๋ธ์ ์ด์ฉํ ๋ฐ์ดํฐ๋ฅผ ์์ธกํ๋ ๋ชจ๋ธ
import numpy as np
import matplotlib.pyplot as plt
r = np.random.RandomState(10)
x = 10 * r.rand(100)
y = 2 * x - 3 * r.rand(100)
plt.scatter(x,y)
<matplotlib.collections.PathCollection at 0x11bde3110>
# ์
๋ ฅ๋ฐ์ดํฐ x์ ๋ชจ์
x.shape
(100,)
# ์ ๋ต ๋ฐ์ดํฐ y์ ๋ชจ์
y.shape
(100,)
x์ y์ ๋ชจ์์ (100,)์ผ๋ก 1์ฐจ์ ๋ฒกํฐ
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model
LinearRegression()
ํ๋ จ์ํค๋ ๋ฉ์๋๋ fit()
- fit ๋ฉ์๋์ ์ธ์๋ก ํน์ฑํ๋ ฌ๊ณผ ํ๊ฒ ๋ฒกํฐ๋ฅผ ๋ฃ์ด์ค
- ํ๋ ฌ ํํ์ ์ ๋ ฅ ๋ฐ์ดํฐ์ 1์ฐจ์ ๋ฒกํฐ ํํ์ ์ ๋ต(๋ผ๋ฒจ)์ ๋ฃ์ด์ค
- ์ ๋ ฅ ๋ฐ์ดํฐ์ธ x๋ฅผ ๊ทธ๋๋ก ๋ฃ์ผ๋ฉด, ์๋ฌ๊ฐ ๋ฐ์
- x๋ numpy์ ndarrayํ์ ์ด๋ reshape()๋ฅผ ์ฌ์ฉ
# ! ์๋ฌ ๋ฐ์
model.fit(x, y)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/var/folders/59/gjb3x8rx30s2cxwfl3zh2m040000gn/T/ipykernel_725/3325953541.py in <module>
1 # ! ์๋ฌ ๋ฐ์
----> 2 model.fit(x, y)
~/opt/anaconda3/envs/dev/lib/python3.7/site-packages/sklearn/linear_model/_base.py in fit(self, X, y, sample_weight)
661
662 X, y = self._validate_data(
--> 663 X, y, accept_sparse=accept_sparse, y_numeric=True, multi_output=True
664 )
665
~/opt/anaconda3/envs/dev/lib/python3.7/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
579 y = check_array(y, **check_y_params)
580 else:
--> 581 X, y = check_X_y(X, y, **check_params)
582 out = X, y
583
~/opt/anaconda3/envs/dev/lib/python3.7/site-packages/sklearn/utils/validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator)
974 ensure_min_samples=ensure_min_samples,
975 ensure_min_features=ensure_min_features,
--> 976 estimator=estimator,
977 )
978
~/opt/anaconda3/envs/dev/lib/python3.7/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
771 "Reshape your data either using array.reshape(-1, 1) if "
772 "your data has a single feature or array.reshape(1, -1) "
--> 773 "if it contains a single sample.".format(array)
774 )
775
ValueError: Expected 2D array, got 1D array instead:
array=[7.71320643 0.20751949 6.33648235 7.48803883 4.98507012 2.24796646
1.98062865 7.60530712 1.69110837 0.88339814 6.85359818 9.53393346
0.03948266 5.12192263 8.12620962 6.12526067 7.21755317 2.91876068
9.17774123 7.14575783 5.42544368 1.42170048 3.7334076 6.74133615
4.41833174 4.34013993 6.17766978 5.13138243 6.50397182 6.01038953
8.05223197 5.21647152 9.08648881 3.19236089 0.90459349 3.00700057
1.13984362 8.28681326 0.46896319 6.26287148 5.47586156 8.19286996
1.9894754 8.56850302 3.51652639 7.54647692 2.95961707 8.8393648
3.25511638 1.65015898 3.92529244 0.93460375 8.21105658 1.5115202
3.84114449 9.44260712 9.87625475 4.56304547 8.26122844 2.51374134
5.97371648 9.0283176 5.34557949 5.90201363 0.39281767 3.57181759
0.7961309 3.05459918 3.30719312 7.73830296 0.39959209 4.29492178
3.14926872 6.36491143 3.4634715 0.43097356 8.79915175 7.63240587
8.78096643 4.17509144 6.05577564 5.13466627 5.97836648 2.62215661
3.00871309 0.25399782 3.03062561 2.42075875 5.57578189 5.6550702
4.75132247 2.92797976 0.64251061 9.78819146 3.39707844 4.95048631
9.77080726 4.40773825 3.18272805 5.19796986].
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.
# ๋ณ์๋ช
X์ ํน์ฑ ํ๋ ฌ์ ๋ฃ๊ธฐ
X = x.reshape(100,1)
# X๋ฅผ fit()์ ์ธ์๋ก ๋ฃ๊ธฐ
model.fit(X,y)
LinearRegression()
โ ์ ๋ ฅ ๋ฐ์ดํฐ์ ๊ทธ ๋ผ๋ฒจ์ ์ด์ฉํด ํ๋ จ์ ์๋ฃ
x_new = np.linspace(-1, 11, 100)
X_new = x_new.reshape(100,1)
y_new = model.predict(X_new)
reshape() ํจ์์์ ๋๋จธ์ง ์ซ์๋ฅผ -1๋ก ๋ฃ์ผ๋ฉด ์๋์ผ๋ก ๋จ์ ์ซ์๋ฅผ ๊ณ์ฐํด ์ค๋๋ค.
์ฆ, x_new์ ์ธ์์ ๊ฐ์๊ฐ 100๊ฐ์ด๋ฏ๋ก, (100, 1)์ ํํ๋ (2, 50)์ ํํ ๋ฑ์ผ๋ก ๋ณํ
(2, -1)์ ์ธ์๋ก ๋ฃ์ผ๋ฉด (2, 50)์ ํํ๋ก ์๋์ผ๋ก ๋ณํ
X_ = x_new.reshape(-1,1)
X_.shape
(100, 1)
Scikit-learn: Mean Squared Error
# mean_squared_error ํจ์์ ๊ณต์ / np.sqrt๋ฅผ ํ์ฉ
from sklearn.metrics import mean_squared_error
error = np.sqrt(mean_squared_error(y,y_new))
print(error)
9.299028215052262
# 1. ๋ชจ๋ธ ๊ฐ์ฒด๋ฅผ ์์ฑ
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model
# 2. ๋ชจ๋ธ์ ํ๋ จ
# ๋ณ์๋ช
X์ ํน์ฑ ํ๋ ฌ์ ๋ฃ๊ธฐ
X = x.reshape(100,1)
# X๋ฅผ fit()์ ์ธ์๋ก ๋ฃ๊ธฐ
model.fit(X,y)
# 3. ์๋ก์ด ๋ฐ์ดํฐ๋ฅผ ๋ฃ๊ณ ์์ธก
x_new = np.linspace(-1, 11, 100) # ์๋ก์ด ๋ฐ์ดํฐ๋ np.linspace()๋ฅผ ์ด์ฉํด์ ์์ฑ
X_new = x_new.reshape(100,1)
y_new = model.predict(X_new)
# 4 .๋ชจ๋ธ ์ฑ๋ฅ ํ๊ฐ
# mean_squared_error ํจ์์ ๊ณต์ / np.sqrt๋ฅผ ํ์ฉ
from sklearn.metrics import mean_squared_error
error = np.sqrt(mean_squared_error(y,y_new))
print(error)
# 5. ์์๋ณด๊ธฐ ์ฝ๊ฒ ๊ทธ๋ํ๋ก
plt.scatter(x, y, label='input data')
plt.plot(X_new, y_new, color='red', label='regression line')
9.299028215052262
[<matplotlib.lines.Line2D at 0x11bdc3dd0>]
๊ทธ๋ํ์ ์ ๋ค๊ณผ ํ๊ท์ ์ด ๊ฑฐ์ ์ผ์น
sklearn.datasets ๋ชจ๋
๊ตฌ๋ถ์
Toy dataset์ ์์
from sklearn.datasets import load_wine
data = load_wine()
type(data)
sklearn.utils.Bunch
sklearn.utils.Bunch๋ผ๊ณ ํ๋ ๋ฐ์ดํฐ ํ์
โ Bunch๋ ํ์ด์ฌ์ ๋์ ๋๋ฆฌ์ ์ ์ฌํ ํํ์ ๋ฐ์ดํฐ ํ์
print(data)
{'data': array([[1.423e+01, 1.710e+00, 2.430e+00, ..., 1.040e+00, 3.920e+00,
1.065e+03],
[1.320e+01, 1.780e+00, 2.140e+00, ..., 1.050e+00, 3.400e+00,
1.050e+03],
[1.316e+01, 2.360e+00, 2.670e+00, ..., 1.030e+00, 3.170e+00,
1.185e+03],
...,
[1.327e+01, 4.280e+00, 2.260e+00, ..., 5.900e-01, 1.560e+00,
8.350e+02],
[1.317e+01, 2.590e+00, 2.370e+00, ..., 6.000e-01, 1.620e+00,
8.400e+02],
[1.413e+01, 4.100e+00, 2.740e+00, ..., 6.100e-01, 1.600e+00,
5.600e+02]]), 'target': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2]), 'frame': None, 'target_names': array(['class_0', 'class_1', 'class_2'], dtype='<U7'), 'DESCR': '.. _wine_dataset:\n\nWine recognition dataset\n------------------------\n\n**Data Set Characteristics:**\n\n :Number of Instances: 178 (50 in each of three classes)\n :Number of Attributes: 13 numeric, predictive attributes and the class\n :Attribute Information:\n \t\t- Alcohol\n \t\t- Malic acid\n \t\t- Ash\n\t\t- Alcalinity of ash \n \t\t- Magnesium\n\t\t- Total phenols\n \t\t- Flavanoids\n \t\t- Nonflavanoid phenols\n \t\t- Proanthocyanins\n\t\t- Color intensity\n \t\t- Hue\n \t\t- OD280/OD315 of diluted wines\n \t\t- Proline\n\n - class:\n - class_0\n - class_1\n - class_2\n\t\t\n :Summary Statistics:\n \n ============================= ==== ===== ======= =====\n Min Max Mean SD\n ============================= ==== ===== ======= =====\n Alcohol: 11.0 14.8 13.0 0.8\n Malic Acid: 0.74 5.80 2.34 1.12\n Ash: 1.36 3.23 2.36 0.27\n Alcalinity of Ash: 10.6 30.0 19.5 3.3\n Magnesium: 70.0 162.0 99.7 14.3\n Total Phenols: 0.98 3.88 2.29 0.63\n Flavanoids: 0.34 5.08 2.03 1.00\n Nonflavanoid Phenols: 0.13 0.66 0.36 0.12\n Proanthocyanins: 0.41 3.58 1.59 0.57\n Colour Intensity: 1.3 13.0 5.1 2.3\n Hue: 0.48 1.71 0.96 0.23\n OD280/OD315 of diluted wines: 1.27 4.00 2.61 0.71\n Proline: 278 1680 746 315\n ============================= ==== ===== ======= =====\n\n :Missing Attribute Values: None\n :Class Distribution: class_0 (59), class_1 (71), class_2 (48)\n :Creator: R.A. Fisher\n :Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n :Date: July, 1988\n\nThis is a copy of UCI ML Wine recognition datasets.\nhttps://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data\n\nThe data is the results of a chemical analysis of wines grown in the same\nregion in Italy by three different cultivators. There are thirteen different\nmeasurements taken for different constituents found in the three types of\nwine.\n\nOriginal Owners: \n\nForina, M. et al, PARVUS - \nAn Extendible Package for Data Exploration, Classification and Correlation. \nInstitute of Pharmaceutical and Food Analysis and Technologies,\nVia Brigata Salerno, 16147 Genoa, Italy.\n\nCitation:\n\nLichman, M. (2013). UCI Machine Learning Repository\n[https://archive.ics.uci.edu/ml]. Irvine, CA: University of California,\nSchool of Information and Computer Science. \n\n.. topic:: References\n\n (1) S. Aeberhard, D. Coomans and O. de Vel, \n Comparison of Classifiers in High Dimensional Settings, \n Tech. Rep. no. 92-02, (1992), Dept. of Computer Science and Dept. of \n Mathematics and Statistics, James Cook University of North Queensland. \n (Also submitted to Technometrics). \n\n The data was used with many others for comparing various \n classifiers. The classes are separable, though only RDA \n has achieved 100% correct classification. \n (RDA : 100%, QDA 99.4%, LDA 98.9%, 1NN 96.1% (z-transformed data)) \n (All results using the leave-one-out technique) \n\n (2) S. Aeberhard, D. Coomans and O. de Vel, \n "THE CLASSIFICATION PERFORMANCE OF RDA" \n Tech. Rep. no. 92-01, (1992), Dept. of Computer Science and Dept. of \n Mathematics and Statistics, James Cook University of North Queensland. \n (Also submitted to Journal of Chemometrics).\n', 'feature_names': ['alcohol', 'malic_acid', 'ash', 'alcalinity_of_ash', 'magnesium', 'total_phenols', 'flavanoids', 'nonflavanoid_phenols', 'proanthocyanins', 'color_intensity', 'hue', 'od280/od315_of_diluted_wines', 'proline']}
data๋ฅผ ์ถ๋ ฅ
- ๋ฒ์น ๋ฐ์ดํฐ ํ์ ์๋ ํ์ด์ฌ์ ๋์ ๋๋ฆฌ ๋ฉ์๋์ธ keys()๋ฅผ ์ฌ์ฉ ๊ฐ๋ฅ
data.keys()
dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names'])
data.data
array([[1.423e+01, 1.710e+00, 2.430e+00, ..., 1.040e+00, 3.920e+00,
1.065e+03],
[1.320e+01, 1.780e+00, 2.140e+00, ..., 1.050e+00, 3.400e+00,
1.050e+03],
[1.316e+01, 2.360e+00, 2.670e+00, ..., 1.030e+00, 3.170e+00,
1.185e+03],
...,
[1.327e+01, 4.280e+00, 2.260e+00, ..., 5.900e-01, 1.560e+00,
8.350e+02],
[1.317e+01, 2.590e+00, 2.370e+00, ..., 6.000e-01, 1.620e+00,
8.400e+02],
[1.413e+01, 4.100e+00, 2.740e+00, ..., 6.100e-01, 1.600e+00,
5.600e+02]])
data.data.shape
(178, 13)
โ ํน์ฑ์ด 13๊ฐ, ๋ฐ์ดํฐ๊ฐ 178๊ฐ์ธ ํน์ฑ ํ๋ ฌ
nidm ์ ์ด์ฉํ์ฌ ์ฐจ์ ํ์ธ
data.data.ndim
2
data.target
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2])
ํ๊ฒ ๋ฒกํฐ์ ๊ธธ์ด๋ ํน์ฑ ํ๋ ฌ์ ๋ฐ์ดํฐ ๊ฐ์์ ์ผ์นํด์ผ ํจ
data.target.shape
(178,)
ํน์ฑ ํ๋ ฌ์ ํ ์ดํฐ ์์ ์ผ์น
data.feature_names
['alcohol',
'malic_acid',
'ash',
'alcalinity_of_ash',
'magnesium',
'total_phenols',
'flavanoids',
'nonflavanoid_phenols',
'proanthocyanins',
'color_intensity',
'hue',
'od280/od315_of_diluted_wines',
'proline']
feature ๊ฐฏ์ ํ์ธ
โ ๋ด์ฅํจ์ len() ์ฌ์ฉ
len(data.feature_names)
13
feature_names์ ๊ฐ์์ ํน์ฑ ํ๋ ฌ์ n_features(์ด)์ ์ซ์๊ฐ ์ผ์น
data.target_names
array(['class_0', 'class_1', 'class_2'], dtype='<U7')
๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ฐ class_0๊ณผ class_1, class_2๋ก ๋ถ๋ฅํ๋ค๋ ๋ป
print(data.DESCR)
.. _wine_dataset:
Wine recognition dataset
------------------------
**Data Set Characteristics:**
:Number of Instances: 178 (50 in each of three classes)
:Number of Attributes: 13 numeric, predictive attributes and the class
:Attribute Information:
- Alcohol
- Malic acid
- Ash
- Alcalinity of ash
- Magnesium
- Total phenols
- Flavanoids
- Nonflavanoid phenols
- Proanthocyanins
- Color intensity
- Hue
- OD280/OD315 of diluted wines
- Proline
- class:
- class_0
- class_1
- class_2
:Summary Statistics:
============================= ==== ===== ======= =====
Min Max Mean SD
============================= ==== ===== ======= =====
Alcohol: 11.0 14.8 13.0 0.8
Malic Acid: 0.74 5.80 2.34 1.12
Ash: 1.36 3.23 2.36 0.27
Alcalinity of Ash: 10.6 30.0 19.5 3.3
Magnesium: 70.0 162.0 99.7 14.3
Total Phenols: 0.98 3.88 2.29 0.63
Flavanoids: 0.34 5.08 2.03 1.00
Nonflavanoid Phenols: 0.13 0.66 0.36 0.12
Proanthocyanins: 0.41 3.58 1.59 0.57
Colour Intensity: 1.3 13.0 5.1 2.3
Hue: 0.48 1.71 0.96 0.23
OD280/OD315 of diluted wines: 1.27 4.00 2.61 0.71
Proline: 278 1680 746 315
============================= ==== ===== ======= =====
:Missing Attribute Values: None
:Class Distribution: class_0 (59), class_1 (71), class_2 (48)
:Creator: R.A. Fisher
:Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)
:Date: July, 1988
This is a copy of UCI ML Wine recognition datasets.
https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data
The data is the results of a chemical analysis of wines grown in the same
region in Italy by three different cultivators. There are thirteen different
measurements taken for different constituents found in the three types of
wine.
Original Owners:
Forina, M. et al, PARVUS -
An Extendible Package for Data Exploration, Classification and Correlation.
Institute of Pharmaceutical and Food Analysis and Technologies,
Via Brigata Salerno, 16147 Genoa, Italy.
Citation:
Lichman, M. (2013). UCI Machine Learning Repository
[https://archive.ics.uci.edu/ml]. Irvine, CA: University of California,
School of Information and Computer Science.
.. topic:: References
(1) S. Aeberhard, D. Coomans and O. de Vel,
Comparison of Classifiers in High Dimensional Settings,
Tech. Rep. no. 92-02, (1992), Dept. of Computer Science and Dept. of
Mathematics and Statistics, James Cook University of North Queensland.
(Also submitted to Technometrics).
The data was used with many others for comparing various
classifiers. The classes are separable, though only RDA
has achieved 100% correct classification.
(RDA : 100%, QDA 99.4%, LDA 98.9%, 1NN 96.1% (z-transformed data))
(All results using the leave-one-out technique)
(2) S. Aeberhard, D. Coomans and O. de Vel,
"THE CLASSIFICATION PERFORMANCE OF RDA"
Tech. Rep. no. 92-01, (1992), Dept. of Computer Science and Dept. of
Mathematics and Statistics, James Cook University of North Queensland.
(Also submitted to Journal of Chemometrics).
ํน์ฑ ํ๋ ฌ์ Pandas์ DataFrame์ผ๋ก ๋ํ๋ผ ์ ์๋ค
import pandas as pd
pd.DataFrame(data.data, columns=data.feature_names)
alcohol | malic_acid | ash | alcalinity_of_ash | magnesium | total_phenols | flavanoids | nonflavanoid_phenols | proanthocyanins | color_intensity | hue | od280/od315_of_diluted_wines | proline | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 14.23 | 1.71 | 2.43 | 15.6 | 127.0 | 2.80 | 3.06 | 0.28 | 2.29 | 5.64 | 1.04 | 3.92 | 1065.0 |
1 | 13.20 | 1.78 | 2.14 | 11.2 | 100.0 | 2.65 | 2.76 | 0.26 | 1.28 | 4.38 | 1.05 | 3.40 | 1050.0 |
2 | 13.16 | 2.36 | 2.67 | 18.6 | 101.0 | 2.80 | 3.24 | 0.30 | 2.81 | 5.68 | 1.03 | 3.17 | 1185.0 |
3 | 14.37 | 1.95 | 2.50 | 16.8 | 113.0 | 3.85 | 3.49 | 0.24 | 2.18 | 7.80 | 0.86 | 3.45 | 1480.0 |
4 | 13.24 | 2.59 | 2.87 | 21.0 | 118.0 | 2.80 | 2.69 | 0.39 | 1.82 | 4.32 | 1.04 | 2.93 | 735.0 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
173 | 13.71 | 5.65 | 2.45 | 20.5 | 95.0 | 1.68 | 0.61 | 0.52 | 1.06 | 7.70 | 0.64 | 1.74 | 740.0 |
174 | 13.40 | 3.91 | 2.48 | 23.0 | 102.0 | 1.80 | 0.75 | 0.43 | 1.41 | 7.30 | 0.70 | 1.56 | 750.0 |
175 | 13.27 | 4.28 | 2.26 | 20.0 | 120.0 | 1.59 | 0.69 | 0.43 | 1.35 | 10.20 | 0.59 | 1.56 | 835.0 |
176 | 13.17 | 2.59 | 2.37 | 20.0 | 120.0 | 1.65 | 0.68 | 0.53 | 1.46 | 9.30 | 0.60 | 1.62 | 840.0 |
177 | 14.13 | 4.10 | 2.74 | 24.5 | 96.0 | 2.05 | 0.76 | 0.56 | 1.35 | 9.20 | 0.61 | 1.60 | 560.0 |
178 rows ร 13 columns
DataFrame์ผ๋ก ๋ํ๋ด๋ ํ๊ฒฐ ๋ฐ์ดํฐ ๋ณด๊ธฐ๊ฐ ํธํด์ง
์ด๋ ๊ฒ ํ๋ฉด EDA(Exploration Data Analysis)ํ ๋ ๊ต์ฅํ ํธํจ
ํน์ฑ ํ๋ ฌ์ ํต์ ๋ณ์๋ช X์ ์ ์ฅํ๊ณ , ํ๊ฒ ๋ฒกํฐ๋ y์ ์ ์ฅ
X = data.data
y = data.target
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model.fit(X, y)
RandomForestClassifier()
y_pred = model.predict(X)
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report
#ํ๊ฒ ๋ฒกํฐ ์ฆ ๋ผ๋ฒจ์ธ ๋ณ์๋ช
y์ ์์ธก๊ฐ y_pred์ ๊ฐ๊ฐ ์ธ์๋ก ๋ฃ์ต๋๋ค.
print(classification_report(y, y_pred))
#์ ํ๋๋ฅผ ์ถ๋ ฅํฉ๋๋ค.
print("accuracy = ", accuracy_score(y, y_pred))
precision recall f1-score support
0 1.00 1.00 1.00 59
1 1.00 1.00 1.00 71
2 1.00 1.00 1.00 48
accuracy 1.00 178
macro avg 1.00 1.00 1.00 178
weighted avg 1.00 1.00 1.00 178
accuracy = 1.0
Estimator ๊ฐ์ฒด๋ LinearRegression()๊ณผ RandomForestClassifier()
Estimator ๊ฐ์ฒด์ fit()๊ณผ prediction() ๋ฉ์๋์ ์ธ์๋ก ๊ฐ๊ธฐ ๋ค๋ฅธ ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ์ผ ํจ
ํ์ง๋ง ์๋ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ํ๋ จ์ ์ฐ์ด๋ ๋ฐ์ดํฐ์ ์์ธก์ ์ฐ์ด๋ ๋ฐ์ดํฐ๋ ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํด์ผ ํจ
ํ๋ จ ๋ฐ์ดํฐ์ ํ ์คํธ ๋ฐ์ดํฐ์ ๋น์จ์ 8:2๋ก ์ค์
from sklearn.datasets import load_wine
data = load_wine()
print(data.data.shape)
print(data.target.shape)
(178, 13)
(178,)
์ ์ฒด ๋ฐ์ดํฐ์ ๊ฐ์๋ 178๊ฐ์ ๋๋ค.
ํน์ฑ ํ๋ ฌ๊ณผ ํ๊ฒ ๋ฒกํฐ๋ ndarray type์ด๋ numpy์ ์ฌ๋ผ์ด์ฑ์ ์ฌ์ฉ
# ํน์ฑ ํ๋ ฌ๊ณผ ํ๊ฒ ๋ฒกํฐ๋ ndarray type์ด๋ numpy์ ์ฌ๋ผ์ด์ฑ์ ์ฌ์ฉ
X_train = data.data[:142]
X_test = data.data[142:]
print(X_train.shape, X_test.shape)
(142, 13) (36, 13)
y_train = data.target[:142]
y_test = data.target[142:]
print(y_train.shape, y_test.shape)
(142,) (36,)
# ํ๋ จ ๋ฐ์ดํฐ์ ํ
์คํธ ๋ฐ์ดํฐ์ ๋ถ๋ฆฌ๊ฐ ๋๋ฌ์ต๋๋ค. ๊ทธ๋ผ ๋ค์ ํ๋ จ๊ณผ ์์ธก
from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier()
model.fit(X_train, y_train)
RandomForestClassifier()
y_pred = model.predict(X_test)
from sklearn.metrics import accuracy_score
print("์ ๋ต๋ฅ =", accuracy_score(y_test, y_pred))
์ ๋ต๋ฅ = 0.9444444444444444
ํ๋ จ ๋ฐ์ดํฐ์ ํ ์คํธ ๋ฐ์ดํฐ ๋ถ๋ฆฌ๋ ํ์ ๊ธฐ๋ฅ์ ๋๋ค. ํ๋ จ์ ์ด ๋ฐ์ดํฐ๋ฅผ ์์ธก์ ์ฌ์ฉํ๋ฉด ํญ์ ์ ํ๋๋ 100%๊ฐ ๋์ฌ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ด์ฃ . ์ฌ์ดํท๋ฐ์์๋ ์ด ํ์ ๊ธฐ๋ฅ์ ๋น์ฐํ API๋ก ์ ๊ณตํ๊ณ ์์ต๋๋ค. ๋ฐ๋ก model_selection์ train_test_split() ํจ์
from sklearn.model_selection import train_test_split
result = train_test_split(X, y, test_size=0.2, random_state=42)
์ธ์๋ก ํน์ฑ ํ๋ ฌ X์ ํ๊ฒ ๋ฒกํฐ y๋ฅผ ๋ฃ๊ณ ํ ์คํธ ๋ฐ์ดํฐ์ ๋น์จ์ ๋ฃ์ด ํค์๋ ์ธ์๋ก ์ง์ ํด ์ค๋๋ค. 20%๋ก ํด ๋ณผ๊ฒ์. ๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ๋ 0๋ฒ๋ถํฐ ์์ฐจ์ ์ผ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ถํ ํ์ฃ ? ์ฌ์ดํท๋ฐ์ ๋๋คํ๊ฒ ๋ฐ์ดํฐ๋ฅผ ์์ด์ฃผ๋ ๊ธฐ๋ฅ๋ ์์ต๋๋ค. random_state ์ธ์์ seed ๋ฒํธ๋ฅผ ์ ๋ ฅํ๋ฉด ๋ฉ๋๋ค. seed ๋ฒํธ๋ ์์๋ก ๊ฒฐ์ ํ ์ ์๊ณ , ๊ฐ์ seed ๋ฒํธ๋ฅผ ์ฌ์ฉํ๋ฉด ์ธ์ ๋ ๊ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.
train_test_split()์ ๋ฐํ๊ฐ์ผ๋ก 4๊ฐ์ ์์๋ก ์ด๋ฃจ์ด์ง list๋ฅผ ๋ฐํํฉ๋๋ค. (*๋ฆฌ์คํธ ์์์ ๋ฐ์ดํฐ ํ์ ์ array์ ๋๋ค.)
print(type(result))
print(len(result))
<class 'list'>
4
result[0].shape
(142, 13)
result[1].shape
(36, 13)
result[2].shape
(142,)
result[3].shape
(36,)
๋ชจ์์ ๋ณด๋ ๊ฐ์ด ์กํ์๋์? ๋ค 0๋ฒ ์์๋ถํฐ ์์๋๋ก ํ๋ จ ๋ฐ์ดํฐ์ฉ ํน์ฑ ํ๋ ฌ, ํ ์คํธ ๋ฐ์ดํฐ์ฉ ํน์ฑ ํ๋ ฌ, ํ๋ จ ๋ฐ์ดํฐ์ฉ ํ๊ฒ ๋ฒกํฐ, ํ ์คํธ ๋ฐ์ดํฐ์ฉ ํ๊ฒ ๋ฒกํฐ์ ๋๋ค.
์ฐ๋ฆฌ๋ ์ด ํจ์๋ฅผ ์ด๋ฐ ์์ผ๋ก unpacking ํด์ ์ฌ์ฉ
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# ๋ฐ์ดํฐ์
๋ก๋ํ๊ธฐ
# [[your code]
# ํ๋ จ์ฉ ๋ฐ์ดํฐ์
๋๋๊ธฐ
# [[your code]
# ํ๋ จํ๊ธฐ
# [[your code]
# ์์ธกํ๊ธฐ
# [[your code]
# ์ ๋ต๋ฅ ์ถ๋ ฅํ๊ธฐ
# [[your code]
# ๋ฐ์ดํฐ์
๋ก๋ํ๊ธฐ
data = load_wine()
# ํ๋ จ์ฉ ๋ฐ์ดํฐ์
๋๋๊ธฐ
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2)
# ํ๋ จํ๊ธฐ
model = RandomForestClassifier()
model.fit(X_train, y_train)
# ์์ธกํ๊ธฐ
y_pred = model.predict(X_test)
# ์ ๋ต๋ฅ ์ถ๋ ฅํ๊ธฐ
print("์ ๋ต๋ฅ =", accuracy_score(y_test, y_pred))
์ ๋ต๋ฅ = 0.9166666666666666
data = load_wine()
# ํ๋ จ์ฉ ๋ฐ์ดํฐ์
๋๋๊ธฐ
X_train, X_test, y_train, y_test = train_test_split(data.data, data.target, test_size=0.2)
# ํ๋ จํ๊ธฐ
model = RandomForestClassifier()
model.fit(X_train, y_train)
# ์์ธกํ๊ธฐ
y_pred = model.predict(X_test)
# ์ ๋ต๋ฅ ์ถ๋ ฅํ๊ธฐ
print("์ ๋ต๋ฅ =", accuracy_score(y_test, y_pred))