【Scikit-Learn】PCA人脸识别

本文首先给出使用PCA降维及恢复数据的示例代码。

通过实验证明了直接使用SVM对人脸数据集进行分类是行不通的。

然后分别对比不同 k(number of PCA components) 值的情况下,降维前后图片的差异。

最后使用降维后的图片进行SVM分类,分类准确率令人非常满意。

1. PCA示例代码

# 待降维的矩阵 A
A = np.array([[3, 2000], 
              [2, 3000], 
              [4, 5000], 
              [5, 8000], 
              [1, 2000]], dtype='float')


from sklearn.decomposition import PCA
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler

def std_PCA(**argv):
    scaler = MinMaxScaler()
    pca = PCA(**argv)
    pipeline = Pipeline([('scaler', scaler),
                         ('pca', pca)])
    return pipeline

# 将 A 降维为矩阵 R2
pca = std_PCA(n_components=1)
R2 = pca.fit_transform(A)
R2
'''
array([[-0.2452941 ],
       [-0.29192442],
       [ 0.29192442],
       [ 0.82914294],
       [-0.58384884]])
'''

# 将 R2还原为 A,可以看到还原出来的矩阵与原矩阵 A 存在误差
pca.inverse_transform(R2)
'''
array([[  2.33563616e+00,   2.91695452e+03],
       [  2.20934082e+00,   2.71106794e+03],
       [  3.79065918e+00,   5.28893206e+03],
       [  5.24568220e+00,   7.66090960e+03],
       [  1.41868164e+00,   1.42213588e+03]])
'''

2. 读入人脸数据集

该人脸数据集是sklearn自带的,其中共有400张图片,共有40位人员的的照片,每个人10张照片。

import time
import logging
from sklearn.datasets import fetch_olivetti_faces

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')

data_home='datasets/'
logging.info('Start to load dataset')
faces = fetch_olivetti_faces(data_home=data_home)
logging.info('Done with load dataset')
'''
2018-09-02 09:51:18,282 Start to load dataset
downloading Olivetti faces from https://ndownloader.figshare.com/files/5976027 to datasets/
2018-09-02 09:51:27,074 Done with load dataset
'''

X = faces.data
y = faces.target
targets = np.unique(faces.target)
target_names = np.array(["c%d" % t for t in targets])
n_targets = target_names.shape[0]
n_samples, h, w = faces.images.shape
print('Sample count: {}\nTarget count: {}'.format(n_samples, n_targets))
print('Image size: {}x{}\nDataset shape: {}\n'.format(w, h, X.shape))
'''
Sample count: 400
Target count: 40
Image size: 64x64
Dataset shape: (400, 4096)
'''

target_names
'''
array(['c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10',
       'c11', 'c12', 'c13', 'c14', 'c15', 'c16', 'c17', 'c18', 'c19',
       'c20', 'c21', 'c22', 'c23', 'c24', 'c25', 'c26', 'c27', 'c28',
       'c29', 'c30', 'c31', 'c32', 'c33', 'c34', 'c35', 'c36', 'c37',
       'c38', 'c39'],
      dtype='<U3')
'''

def plot_gallery(images, titles, h, w, n_row=2, n_col=5):
    """显示图片阵列"""
    plt.figure(figsize=(2 * n_col, 2.2 * n_row), dpi=144)
    plt.subplots_adjust(bottom=0, left=.01, right=.99, top=.90, hspace=.01)
    for i in range(n_row * n_col):
        plt.subplot(n_row, n_col, i + 1)
        plt.imshow(images[i].reshape((h, w)), cmap=plt.cm.gray)
        plt.title(titles[i])
        plt.axis('off')

# 从40种类别中各随机选取一张图片,组成一个长度为40的数组 sample_images
sample_images = None
sample_titles = []
for i in range(n_targets):
    people_images = X[y==i]
    people_sample_index = np.random.randint(0, people_images.shape[0], 1) # 从 0~people_images.shape[0] 中选出一个数
    people_sample_image = people_images[people_sample_index, :]
    if sample_images is not None:
        sample_images = np.concatenate((sample_images, people_sample_image), axis=0)
    else:
        sample_images = people_sample_image
    sample_titles.append(target_names[i])

sample_images.shape
'''
(40, 4096)
'''

# 将 sample_images 按照 n_row * n_col 的阵列来展示
n_row = 2
n_col = 6
plot_gallery(sample_images, sample_titles, h, w, n_row, n_col)

这里写图片描述

3.直接使用SVM对图片进行分类

下文直接将64*64的人脸图片reshape成1*4096的特征,然后直接输入SVM进行分类。

混淆矩阵的理想输出是对角线上有数字,其他地方没有数字。但是下文输出的混淆矩阵显然不是。

而且根据下文输出的SVM测试报告,图片40个类别里的查准率、召回率、F1值全为0。

这是因为共有4096个特征,但是数据集样本数量才400个,比特征数还少,因此直接将图片放入SVM是行不通的。

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, random_state=4)

from sklearn.svm import SVC

start = time.clock()
print('Fitting train datasets ...')
clf = SVC(class_weight='balanced')
clf.fit(X_train, y_train)
print('Done in {0:.2f}s'.format(time.clock()-start))
'''
Fitting train datasets ...
Done in 1.07s
'''

start = time.clock()
print("Predicting test dataset ...")
y_pred = clf.predict(X_test)
print('Done in {0:.2f}s'.format(time.clock()-start))
'''
Predicting test dataset ...
Done in 0.14s
'''

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test, y_pred, labels=range(n_targets))
print("confusion matrix:\n")

# 混淆矩阵是40*40的矩阵,默认情况下不会全部输出。np.set_printoptions能够完整输出混淆矩阵
np.set_printoptions(threshold=np.nan) 
print(cm)

这里写图片描述

from sklearn.metrics import classification_report

print(classification_report(y_test, y_pred, target_names=target_names))

这里写图片描述

4. 使用PCA降维人脸图片

为了提高分类的准确率,通过PCA算法选取图片的前k个最重要的特征。

首先我们要选取出一个恰当的k值,使得PCA处理后能够保留95%的原数据信息。

# 根据不同的 k 值,构建PCA模型,再把模型处理后数据的还原率放入 explained_ratios数组。
from sklearn.decomposition import PCA

print("Exploring explained variance ratio for dataset ...")
candidate_components = range(10, 300, 30) # list(candidate_components) = [10, 40, 70, 100, 130, 160, 190, 220, 250, 280]
explained_ratios = []
start = time.clock()
for c in candidate_components:
    pca = PCA(n_components=c)
    X_pca = pca.fit_transform(X)
    explained_ratios.append(np.sum(pca.explained_variance_ratio_))
print('Done in {0:.2f}s'.format(time.clock()-start))
'''
Exploring explained variance ratio for dataset ...
Done in 2.02s
'''

plt.figure(figsize=(10, 6), dpi=144)
plt.grid()
plt.plot(candidate_components, explained_ratios)
plt.xlabel('Number of PCA Components(k)')
plt.ylabel('Explained Variance Ratio')
plt.title('Explained variance ratio for PCA')
plt.yticks(np.arange(0.5, 1.05, .05))
plt.xticks(np.arange(0, 300, 20));

这里写图片描述

# 对比不同k值PAC降维前后的图像

def title_prefix(prefix, title):
    return "{}: {}".format(prefix, title)

n_row = 1
n_col = 5

sample_images = sample_images[0:5]
sample_titles = sample_titles[0:5]

plotting_images = sample_images
plotting_titles = [title_prefix('orig', t) for t in sample_titles]
candidate_components = [140, 75, 37, 19, 8]
for c in candidate_components:
    print("Fitting and projecting on PCA(n_components={}) ...".format(c))
    start = time.clock()
    pca = PCA(n_components=c)
    pca.fit(X)
    X_sample_pca = pca.transform(sample_images)
    X_sample_inv = pca.inverse_transform(X_sample_pca)
    plotting_images = np.concatenate((plotting_images, X_sample_inv), axis=0)
    sample_title_pca = [title_prefix('{}'.format(c), t) for t in sample_titles]
    plotting_titles = np.concatenate((plotting_titles, sample_title_pca), axis=0)
    print("Done in {0:.2f}s".format(time.clock() - start))

print("Plotting sample image with different number of PCA conpoments ...")
plot_gallery(plotting_images, plotting_titles, h, w,
    n_row * (len(candidate_components) + 1), n_col)
'''
Fitting and projecting on PCA(n_components=140) ...
Done in 0.84s
Fitting and projecting on PCA(n_components=75) ...
Done in 0.56s
Fitting and projecting on PCA(n_components=37) ...
Done in 0.44s
Fitting and projecting on PCA(n_components=19) ...
Done in 0.36s
Fitting and projecting on PCA(n_components=8) ...
Done in 0.22s
Plotting sample image with different number of PCA conpoments ...
'''

这里写图片描述

5. 对PCA降维后的图片进行SVM分类

n_components = 140

print("Fitting PCA by using training data ...")
start = time.clock()
pca = PCA(n_components=n_components, svd_solver='randomized', whiten=True).fit(X_train)
print("Done in {0:.2f}s".format(time.clock() - start))

print("Projecting input data for PCA ...")
start = time.clock()
X_train_pca = pca.transform(X_train)
X_test_pca = pca.transform(X_test)
print("Done in {0:.2f}s".format(time.clock() - start))
'''
Fitting PCA by using training data ...
Done in 0.68s
Projecting input data for PCA ...
Done in 0.04s
'''


# 使用GridSearchCV来选择SVC模型最佳参数,设置n_job=4来启动4个线程并发操作,
# 同时设置verbose=2来输出一些过程信息
from sklearn.model_selection import GridSearchCV

print("Searching the best parameters for SVC ...")
param_grid = {'C': [1, 5, 10, 50, 100],
              'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01]}
clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid, verbose=2, n_jobs=4)
clf = clf.fit(X_train_pca, y_train)
print("Best parameters found by grid search:")
print(clf.best_params_)
'''
Searching the best parameters for SVC ...
Fitting 3 folds for each of 25 candidates, totalling 75 fits
[CV] C=1, gamma=0.0001 ...............................................
[CV] C=1, gamma=0.0001 ...............................................
[CV] C=1, gamma=0.0001 ...............................................
[CV] C=1, gamma=0.0005 ...............................................
[CV] ................................ C=1, gamma=0.0001, total=   0.1s
[CV] ................................ C=1, gamma=0.0001, total=   0.1s
[CV] C=1, gamma=0.0005 ...............................................
[CV] C=1, gamma=0.0005 ...............................................
[CV] ................................ C=1, gamma=0.0001, total=   0.1s
[CV] C=1, gamma=0.001 ................................................
[CV] ................................ C=1, gamma=0.0005, total=   0.0s
[CV] C=1, gamma=0.001 ................................................
[CV] ................................ C=1, gamma=0.0005, total=   0.1s
[CV] C=1, gamma=0.001 ................................................
[CV] ................................ C=1, gamma=0.0005, total=   0.1s
[CV] C=1, gamma=0.01 .................................................
[CV] ................................. C=1, gamma=0.001, total=   0.1s
[CV] C=5, gamma=0.0001 ...............................................
[CV] ................................. C=1, gamma=0.001, total=   0.0s
[CV] C=5, gamma=0.0005 ...............................................
[CV] .................................. C=1, gamma=0.01, total=   0.1s
[CV] ................................. C=1, gamma=0.001, total=   0.1s
[CV] C=1, gamma=0.01 .................................................
[CV] C=1, gamma=0.005 ................................................
[CV] ................................ C=5, gamma=0.0001, total=   0.1s
[CV] C=5, gamma=0.0001 ...............................................
[CV] ................................ C=5, gamma=0.0005, total=   0.1s
[CV] C=5, gamma=0.001 ................................................
[CV] ................................. C=1, gamma=0.005, total=   0.1s
[CV] C=1, gamma=0.005 ................................................
[CV] .................................. C=1, gamma=0.01, total=   0.1s
[CV] C=1, gamma=0.01 .................................................
[CV] ................................ C=5, gamma=0.0001, total=   0.1s
[CV] C=5, gamma=0.0005 ...............................................
[CV] ................................. C=5, gamma=0.001, total=   0.1s
[CV] C=5, gamma=0.001 ................................................
[CV] ................................. C=1, gamma=0.005, total=   0.1s
[CV] C=1, gamma=0.005 ................................................
[CV] .................................. C=1, gamma=0.01, total=   0.1s
[CV] ................................ C=5, gamma=0.0005, total=   0.1s
[CV] C=5, gamma=0.0005 ...............................................
[CV] C=5, gamma=0.0001 ...............................................
[CV] ................................. C=5, gamma=0.001, total=   0.1s
[CV] C=5, gamma=0.001 ................................................
[CV] ................................ C=5, gamma=0.0001, total=   0.1s
[CV] ................................ C=5, gamma=0.0005, total=   0.1s
[CV] ................................. C=1, gamma=0.005, total=   0.1s
[CV] C=5, gamma=0.005 ................................................
[CV] C=5, gamma=0.01 .................................................
[CV] C=10, gamma=0.0001 ..............................................
[CV] ................................. C=5, gamma=0.001, total=   0.1s
[CV] C=10, gamma=0.001 ...............................................
[CV] .................................. C=5, gamma=0.01, total=   0.0s
[CV] ................................. C=5, gamma=0.005, total=   0.1s
[CV] C=5, gamma=0.01 .................................................
[CV] C=5, gamma=0.005 ................................................
[CV] ............................... C=10, gamma=0.0001, total=   0.1s
[CV] C=10, gamma=0.0005 ..............................................
[CV] ................................ C=10, gamma=0.001, total=   0.1s
[CV] C=10, gamma=0.001 ...............................................
[CV] ................................. C=5, gamma=0.005, total=   0.1s
[CV] C=5, gamma=0.005 ................................................
[CV] .................................. C=5, gamma=0.01, total=   0.1s
[CV] ............................... C=10, gamma=0.0005, total=   0.0s
[CV] C=10, gamma=0.0005 ..............................................
[CV] C=10, gamma=0.0001 ..............................................
[CV] ................................ C=10, gamma=0.001, total=   0.1s
[CV] C=10, gamma=0.001 ...............................................
[CV] ............................... C=10, gamma=0.0001, total=   0.0s
[CV] C=10, gamma=0.0001 ..............................................
[CV] ................................. C=5, gamma=0.005, total=   0.1s
[CV] ............................... C=10, gamma=0.0005, total=   0.1s
[CV] C=5, gamma=0.01 .................................................
[CV] C=10, gamma=0.0005 ..............................................
[CV] ................................ C=10, gamma=0.001, total=   0.1s
[CV] C=10, gamma=0.005 ...............................................
[CV] ............................... C=10, gamma=0.0001, total=   0.1s
[CV] C=10, gamma=0.005 ...............................................
[CV] .................................. C=5, gamma=0.01, total=   0.1s
[CV] C=10, gamma=0.01 ................................................
[CV] ............................... C=10, gamma=0.0005, total=   0.1s
[CV] C=50, gamma=0.0005 ..............................................
[CV] ................................ C=10, gamma=0.005, total=   0.1s
[CV] C=50, gamma=0.001 ...............................................
[CV] ................................ C=10, gamma=0.005, total=   0.1s
[CV] C=10, gamma=0.005 ...............................................
[CV] ................................. C=10, gamma=0.01, total=   0.1s
[CV] C=50, gamma=0.0001 ..............................................
[CV] ............................... C=50, gamma=0.0005, total=   0.1s
[CV] C=50, gamma=0.0005 ..............................................
[CV] ................................ C=50, gamma=0.001, total=   0.1s
[CV] C=50, gamma=0.001 ...............................................
[CV] ................................ C=10, gamma=0.005, total=   0.1s
[CV] C=10, gamma=0.01 ................................................
[CV] ............................... C=50, gamma=0.0001, total=   0.1s
[CV] C=50, gamma=0.0001 ..............................................
[CV] ............................... C=50, gamma=0.0005, total=   0.1s
[CV] C=50, gamma=0.0005 ..............................................
[CV] ................................. C=10, gamma=0.01, total=   0.1s
[CV] ................................ C=50, gamma=0.001, total=   0.1s
[CV] C=10, gamma=0.01 ................................................
[CV] C=50, gamma=0.005 ...............................................
[CV] ............................... C=50, gamma=0.0001, total=   0.1s
[CV] C=50, gamma=0.0001 ..............................................
[CV] ............................... C=50, gamma=0.0005, total=   0.1s
[CV] C=50, gamma=0.001 ...............................................
[CV] ................................ C=50, gamma=0.005, total=   0.0s
[CV] C=50, gamma=0.005 ...............................................
[CV] ................................. C=10, gamma=0.01, total=   0.1s
[CV] C=50, gamma=0.005 ...............................................
[CV] ............................... C=50, gamma=0.0001, total=   0.1s
[CV] C=100, gamma=0.0001 .............................................
[CV] ................................ C=50, gamma=0.005, total=   0.1s
[CV] C=100, gamma=0.0005 .............................................
[CV] ................................ C=50, gamma=0.001, total=   0.1s
[CV] C=100, gamma=0.001 ..............................................
[CV] ................................ C=50, gamma=0.005, total=   0.1s
[CV] C=50, gamma=0.01 ................................................
[CV] .............................. C=100, gamma=0.0001, total=   0.1s
[CV] C=100, gamma=0.0001 .............................................
[CV] .............................. C=100, gamma=0.0005, total=   0.1s
[CV] C=100, gamma=0.0005 .............................................
[CV] ............................... C=100, gamma=0.001, total=   0.1s
[CV] C=100, gamma=0.005 ..............................................
[CV] ................................. C=50, gamma=0.01, total=   0.1s
[CV] C=50, gamma=0.01 ................................................
[CV] .............................. C=100, gamma=0.0001, total=   0.1s
[CV] C=100, gamma=0.0001 .............................................
[CV] .............................. C=100, gamma=0.0005, total=   0.1s
[CV] C=100, gamma=0.001 ..............................................
[CV] ............................... C=100, gamma=0.005, total=   0.1s
[CV] C=100, gamma=0.005 ..............................................
[CV] ................................. C=50, gamma=0.01, total=   0.1s
[CV] C=50, gamma=0.01 ................................................
[CV] .............................. C=100, gamma=0.0001, total=   0.1s
[CV] C=100, gamma=0.0005 .............................................
[CV] ............................... C=100, gamma=0.001, total=   0.1s
[CV] C=100, gamma=0.001 ..............................................
[CV] ............................... C=100, gamma=0.005, total=   0.1s
[CV] C=100, gamma=0.005 ..............................................
[CV] ................................. C=50, gamma=0.01, total=   0.1s
[CV] C=100, gamma=0.01 ...............................................
[CV] .............................. C=100, gamma=0.0005, total=   0.1s
[CV] ............................... C=100, gamma=0.001, total=   0.1s
[CV] ............................... C=100, gamma=0.005, total=   0.1s
[CV] ................................ C=100, gamma=0.01, total=   0.0s
[CV] C=100, gamma=0.01 ...............................................
[CV] ................................ C=100, gamma=0.01, total=   0.0s
[CV] C=100, gamma=0.01 ...............................................
[CV] ................................ C=100, gamma=0.01, total=   0.0s

Best parameters found by grid search:
{'C': 10, 'gamma': 0.001}
'''
# 使用最佳参数的SVC来对测试集进行处理
start = time.clock()
print("Predict test dataset ...")
y_pred = clf.best_estimator_.predict(X_test_pca)
cm = confusion_matrix(y_test, y_pred, labels=range(n_targets))
print("Done in {0:.2f}.\n".format(time.clock()-start))
print("confusion matrix:")
np.set_printoptions(threshold=np.nan)
print(cm)
'''
Predict test dataset ...
Done in 0.01.

confusion matrix:
[[1 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 2 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
  0 0 0]
 [0 0 0 1 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 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 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 3 0 0 0 0 0 0 0 0 0 1 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 2 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 4 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 2 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 0 0 0 0 0 0 0 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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 2 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 3
  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 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 2 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 2]]
'''
print(classification_report(y_test, y_pred, target_names=target_names))
'''
             precision    recall  f1-score   support

         c0       0.50      1.00      0.67         1
         c1       1.00      0.67      0.80         3
         c2       1.00      0.50      0.67         2
         c3       1.00      1.00      1.00         1
         c4       1.00      1.00      1.00         1
         c5       1.00      1.00      1.00         1
         c6       1.00      0.75      0.86         4
         c7       1.00      1.00      1.00         2
         c8       1.00      1.00      1.00         4
         c9       1.00      1.00      1.00         2
        c10       1.00      1.00      1.00         1
        c11       1.00      1.00      1.00         4
        c12       1.00      1.00      1.00         4
        c13       1.00      1.00      1.00         1
        c14       1.00      1.00      1.00         1
        c15       0.75      1.00      0.86         3
        c16       1.00      1.00      1.00         2
        c17       1.00      1.00      1.00         2
        c18       1.00      1.00      1.00         2
        c19       1.00      1.00      1.00         1
        c20       1.00      1.00      1.00         2
        c21       0.75      1.00      0.86         3
        c22       1.00      1.00      1.00         2
        c23       1.00      1.00      1.00         3
        c24       0.67      0.67      0.67         3
        c25       1.00      1.00      1.00         2
        c26       1.00      1.00      1.00         2
        c27       1.00      1.00      1.00         2
        c28       1.00      1.00      1.00         2
        c29       1.00      1.00      1.00         3
        c30       1.00      1.00      1.00         2
        c31       1.00      1.00      1.00         2
        c32       1.00      1.00      1.00         2
        c33       1.00      1.00      1.00         3
        c34       1.00      1.00      1.00         1
        c35       1.00      1.00      1.00         2
        c36       1.00      1.00      1.00         2

avg / total       0.96      0.95      0.95        80
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值