【问题标题】:How to increase Logistic Regression score on Sklearn PCA?如何提高 Sklearn PCA 的逻辑回归分数?
【发布时间】:2019-06-03 00:10:44
【问题描述】:

我想在图像识别方面对 Lenet 和 PCA 进行比较,所以我使用了 German Traffic Signals Benchmark 和 Sklearn PCA 模块,但是当我使用 Logistic Regression 进行测试时,分数并没有高于6%,不管我怎么尝试。

我尝试修改交互次数和预处理次数(使用归一化和均衡化),但还是不行

文件由 Pickle 通过三个存档加载:

train.p, with shape of (34799, 32, 32, 3)
test.p, with shape of (12630, 32, 32, 3)
valid.p, with shape of (4410, 32, 32, 3)

它们中的每一个都有其标签,如 y_train、y_test 和 y_valid 中所写。 这是代码的相关部分:

def gray_scale(image):
    """
    Convert images to gray scale.
        Parameters:
            image: An np.array compatible with plt.imshow.
    """
    return cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

def preprocess2(data):

    n_training = data.shape
    gray_images = np.zeros((n_training[0], n_training[1], n_training[2]))
    for i, img in enumerate(data):
        gray_images[i] = gray_scale(img)
    gray_images = gray_images[..., None]
    return gray_images

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

pca = PCA(0.95)

X_train_preprocess = preprocess2(X_train)
#Removing one dimension (34799,32,32,1) to (34799,32,32)
X_train_preprocess = X_train_preprocess.reshape(34799,32,32)
nsamples, nx, ny = X_train_preprocess.shape
X_train_preprocess = X_train_preprocess.reshape((nsamples,nx*ny))

X_test_preprocess = preprocess2(X_test)
#Removing one dimension (34799,32,32,1) to (12630,32,32)
X_test_preprocess = X_test_preprocess.reshape(12630,32,32) 
n2samples, n2x, n2y = X_test_preprocess.shape
X_test_preprocess = X_test_preprocess.reshape((n2samples,n2x*n2y))

print(X_train_preprocess.shape)
pca.fit(X_train_preprocess)
print(pca.n_components_)
scaler = StandardScaler()
scaler.fit(X_train_preprocess)
X_t_train = scaler.transform(X_train_preprocess)
X_t_test = scaler.transform(X_test_preprocess)

X_t_train = pca.transform(X_t_train)
X_t_test = pca.transform(X_t_test)

from sklearn.linear_model import LogisticRegression
logisticRegr = LogisticRegression(solver = 'lbfgs', max_iter = 5000)
logisticRegr.fit(X_t_train, y_train)
print('score', logisticRegr.predict(X_t_test[0:10]))
print('score', logisticRegr.score(X_t_test, y_test))

结果如下:

(34799, 1024)
62
/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/logistic.py:469: FutureWarning: Default multi_class will be changed to 'auto' in 0.22. Specify the multi_class option to silence this warning.
  "this warning.", FutureWarning)
score [ 1  2 10 10 13 10 25  1  1  4]
score 0.028820269200316707

所以我想看看你们能不能告诉我我做错了什么以及我能做些什么来使它正常工作

【问题讨论】:

  • 如果您可以链接到数据集,这将非常有帮助。照原样,您的代码在结果中不可重现。
  • 我不确定我是否可以编辑帖子,但这里是完整的图片集:link,并且这些图片被放在了 .p 文件中:link
  • 您可以随时编辑您的帖子。它位于问题的底部。

标签: python machine-learning scikit-learn deep-learning pca


【解决方案1】:

你在图像识别中得到了二维数据,最好用cnn网络来呈现高维关系

相关链接:Training CNN with images in sklearn neural net

【讨论】:

  • 你没有抓住重点。他想将性能与 PCA 进行比较。他已经知道 CNN。
猜你喜欢
  • 2015-12-27
  • 2014-09-16
  • 2015-05-04
  • 2016-03-06
  • 2014-01-20
  • 2019-07-26
  • 1970-01-01
  • 2020-09-16
  • 2021-03-11
相关资源
最近更新 更多