机器学习算法实战案例

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')

猜你喜欢

转载自blog.csdn.net/mago2015/article/details/84036360