Predict trong Python là gì

Khám phá thư viện sklearn machine learning

Viet Hung Nguyen
Follow
Oct 19, 2017 · 6 min read

sklearn là thư viện về machine learning phổ biến nhất của Python. Nó viết sẵn các thuật toán lằng nhằng phức tạp, bạn chỉ việc nhét dữ liệu vào, chờ nó tính toán rồi lấy kết quả, dễ như ăn kẹo.

Iris

Bài này thừa nhận rằng bạn có biết đọc code Python. Ta tập trung vào: kiến thức về xử lý dữ liệu, machine learning và sử dụng sklearn. Nếu bạn không biết cả 3 thứ này, học cả 3 cùng lúc sẽ rất mệt và không hiệu quả.

import sklearn
import sys
print["Python version: {}".format[sys.version]]
print["sklearn version: {}".format[sklearn.__version__]]

Kết quả

Python version: 3.5.2 [default, Nov 17 2016, 17:05:23]
[GCC 5.4.0 20160609]
sklearn version: 0.19.0

Dữ liệu và các thuộc tính

sklearn có sẵn các bộ dữ liệu thường được dùng trong nghiên cứu. Lấy danh sách các bộ dữ liệu có sẵn trong sklearn bằng code Python

import sklearn.datasets
list_of_data_funcs = [s for s in dir[sklearn.datasets] if s.startswith['load']]
list_of_data_funcs, len[list_of_data_funcs]

Kết quả

[['load_boston',
'load_breast_cancer',
'load_diabetes',
'load_digits',
'load_files',
'load_iris',
'load_linnerud',
'load_mlcomp',
'load_sample_image',
'load_sample_images',
'load_svmlight_file',
'load_svmlight_files',
'load_wine'],
13]

Hoa Iris

Bộ dữ liệu về các giống hoa Iris, được dùng làm ví dụ với các thuật toán phân loại [classification] dự đoán giống hoa dựa trên kích thước của hoa.

iris = sklearn.datasets.load_iris[]type[iris]
# sklearn.utils.Bunch

Chi tiết về kiểu Bunch: help[sklearn.utils.Bunch]

sklearn.utils.Bunch[**kwargs]
Docstring:
Container object for datasets

Dictionary-like object that exposes its keys as attributes.

Khám phá

dir[iris]# ['DESCR', 'data', 'feature_names', 'target', 'target_names']type[iris.data]# numpy.ndarrayiris.data.shape # 150 rows [samples], 4 columns [features]# [150, 4]iris.target.shape # 150 x 1# [150,]iris.target_names# array[['setosa', 'versicolor', 'virginica'],
dtype='

Chủ Đề