My project is a handwriting digit analyzer. It is working on Google Colab but is showing an error on Pycharm. The statement that is making it work in colab is “%matplotlib inline” this line is showing an error in pycharm.
from sklearn.datasets import fetch_openml from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression from sklearn import metrics import numpy as np import matplotlib.pyplot as plt %matplotlib inline mnist = fetch_openml("mnist_784") plt.figure(figsize=(20, 4)) for index, (image, label) in enumerate(zip(mnist.data[:5], mnist.target[:5])): plt.subplot(1, 5, index + 1) plt.imshow(np.reshape(image, (28, 28)), cmap="gray") plt.title("Number: %s" % label) X_train, X_test, y_train, y_test = train_test_split(mnist.data, mnist.target, test_size=0.2) mdl = LogisticRegression(solver="lbfgs") mdl.fit(X_train, y_train) predictions = mdl.predict(X_test) score = mdl.score(X_test, y_test) index = 0 plt.imshow(np.reshape(X_test[index], (28, 28))) print("Prediction: " + mdl.predict([X_test[index]])[0]) cm = metrics.confusion_matrix(y_test, predictions) plt.figure(figsize=(9, 9)) plt.imshow(cm, cmap='Pastell') plt.title('Confusion Matrix for MNIST Data') plt.xticks(np.arange(10)) plt.yticks(np.arange(10)) plt.ylabel('Actual Label') plt.xlabel('Predicted Label') plt.colorbar() width, height = cm.shape for x in range(width): for y in range(height): plt.annotate(str(cm[x][y]), xy=(y, x), horizontalalignment='center', verticalalignment='center')
Traceback (most recent call last): File “C:UsersAsusPycharmProjectspythonProjectHandwriting_Digit_Recognitiondigit.py”, line 15, in plt.imshow(np.reshape(image, (18, 18)), cmap=”gray”) File “<array_function internals>”, line 5, in reshape File “C:UsersAsusPycharmProjectspythonProjectvenvlibsite-packagesnumpycorefromnumeric.py”, line 299, in reshape return _wrapfunc(a, ‘reshape’, newshape, order=order) File “C:UsersAsusPycharmProjectspythonProjectvenvlibsite-packagesnumpycorefromnumeric.py”, line 55, in _wrapfunc return _wrapit(obj, method, *args, **kwds) File “C:UsersAsusPycharmProjectspythonProjectvenvlibsite-packagesnumpycorefromnumeric.py”, line 44, in _wrapit result = getattr(asarray(obj), method)(*args, **kwds) ValueError: cannot reshape array of size 1 into shape (18,18)
Advertisement
Answer
%matplotlib inline
is a message to Jupyter notebook to render images into the notebook itself. You need to remove that line if you use your code from Pycharm.