Thiết kế quy trình Machine Learning bằng Python
Dr. Chris Anagnostopoulos
Honorary Associate Professor
X)y)credit_scoring.head(4)
checking_status duration ... foreign_worker class
0 '<0' 6 ... yes good
1 '0<=X<200' 48 ... yes bad
2 'no checking' 12 ... yes good
3 '<0' 42 ... yes good
Tiền xử lý với LabelEncoder từ sklearn.preprocessing:
le = LabelEncoder()
le.fit_transform(credit_scoring['checking_status'])[:4]
array([1, 0, 3, 1])
.fit(features, labels).predict(features)features, labels = credit_scoring.drop('class', 1), credit_scoring['class']model_nb = GaussianNB() model_nb.fit(features, labels) model_nb.predict(features.head(5))
['good' 'bad' 'good' 'bad' 'good']
Độ chính xác 60% trên 5 mẫu đầu.
.fit() tối ưu tham số của mô hìnhAdaBoostClassifier vượt GaussianNB trên 5 điểm dữ liệu đầu:
model_ab = AdaBoostClassifier()
model_ab.fit(features, labels)
model_ab.predict(features.head(5))
numpy.array(labels[0:5])
['good' 'bad' 'good' 'good' 'bad']
['good' 'bad' 'good' 'good' 'bad']
Mẫu lớn hơn => ước lượng độ chính xác tốt hơn:
from sklearn.metrics import accuracy_score
accuracy_score(labels, model_nb.predict(features)) # naive bayes
0.706
accuracy_score(labels, model_ab.predict(features)) # adaboost
0.802
Tính toán này sai ở đâu?
Overfitting: mô hình luôn hoạt động tốt hơn trên dữ liệu huấn luyện so với dữ liệu chưa thấy.
Huấn luyện trên X_train, y_train, đánh giá độ chính xác trên X_test, y_test:
from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)GaussianNB().fit(X_train, y_train).predict(X_test)

Thiết kế quy trình Machine Learning bằng Python