機器學習演算法實戰案例
阿新 • • 發佈:2018-12-29
1、svm演算法
from sklearn.svm import SVC
svm = SVC(kernel='rbf', random_state=0, gamma=0.2, C=1.0)
svm.fit(featureListArray)
pred = svm.predict(featureListArray)
print(pred)
2、KMeans演算法
>>> from sklearn.cluster import KMeans >>> import numpy as np >>> X = np.array([[1, 2], [1, 4], [1, 0], ... [4, 2], [4, 4], [4, 0]]) >>> kmeans = KMeans(n_clusters=2, random_state=0).fit(X) >>> kmeans.labels_ array([0, 0, 0, 1, 1, 1], dtype=int32) >>> kmeans.predict([[0, 0], [4, 4]]) array([0, 1], dtype=int32) >>> kmeans.cluster_centers_ array([[1., 2.], [4., 2.]])
fit (X[, y, sample_weight]) |
Compute k-means clustering. |
fit_predict (X[, y, sample_weight]) |
Compute cluster centers and predict cluster index for each sample. |
fit_transform (X[, y, sample_weight]) |
Compute clustering and transform X to cluster-distance space. |
get_params ([deep]) |
Get parameters for this estimator. |
predict (X[, sample_weight]) |
Predict the closest cluster each sample in X belongs to. |
score (X[, y, sample_weight]) |
Opposite of the value of X on the K-means objective. |
set_params (**params) |
Set the parameters of this estimator. |
transform (X) |
Transform X to a cluster-distance space. |
3、GMM聚類
from sklearn import mixture
clf = mixture.GaussianMixture(n_components=2, covariance_type='full')
clf.fit(featureListArray)
pred = clf.predict(featureListArray)
print(pred)
4、One-svm
# # TODO: 異常檢測 = one-svm / SVDD /...
# print()
# print("異常檢測...")
# print("model training...")
# # 定義 OneClassSvm
# clf = svm.OneClassSVM(nu=0.5, kernel="rbf", gamma=0.1)
# # OneClassSVM 模型訓練
# clf.fit(newFeaturenArray)
#
# # # 儲存 One-SVM 模型
# if not os.path.exists('model/svm'):
# os.makedirs('model/svm')
# print("model saving...")
# joblib.dump(clf, 'model/svm/svm_clf.model')