Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I resolve 'NoneType' object has no attribute 'write' error with scikit-learn digits dataset?
    primarykey
    data
    text
    <p>I'm trying to do the <a href="http://scikit-learn.org/stable/tutorial/statistical_inference/supervised_learning.html" rel="nofollow">first exercise</a> on scikit-learn, but even when I run their <a href="http://scikit-learn.org/stable/_downloads/plot_digits_classification_exercise1.py" rel="nofollow">solution code</a> (shown below) I get the error in the code block immediately following. Does anyone know why this is happening? How can I resolve this?</p> <p>The predict method also fails when trying to use this dataset, for some reason it seems to work fine for the iris dataset using the code at the very bottom of the question. sorry if I am missing something very obvious, I am not an actual programmer.</p> <pre><code>Traceback (most recent call last): File "C:\Users\user2491873\Desktop\scikit_exercise.py", line 30, in &lt;module&gt; print(knn.fit(X_train, y_train).score(X_test, y_test)) File "C:\Python33\lib\site-packages\sklearn\base.py", line 279, in score return accuracy_score(y, self.predict(X)) File "C:\Python33\lib\site-packages\sklearn\neighbors\classification.py", line 131, in predict neigh_dist, neigh_ind = self.kneighbors(X) File "C:\Python33\lib\site-packages\sklearn\neighbors\base.py", line 254, in kneighbors warn_equidistant() File "C:\Python33\lib\site-packages\sklearn\neighbors\base.py", line 33, in warn_equidistant warnings.warn(msg, NeighborsWarning, stacklevel=3) File "C:\Python33\lib\idlelib\PyShell.py", line 59, in idle_showwarning file.write(warnings.formatwarning(message, category, filename, AttributeError: 'NoneType' object has no attribute 'write' </code></pre> <p>here is the code:</p> <pre><code>""" ================================ Digits Classification Exercise ================================ This exercise is used in the :ref:`clf_tut` part of the :ref:`supervised_learning_tut` section of the :ref:`stat_learn_tut_index`. """ from sklearn import datasets, neighbors, linear_model digits = datasets.load_digits() X_digits = digits.data y_digits = digits.target n_samples = len(X_digits) X_train = X_digits[:.9 * n_samples] y_train = y_digits[:.9 * n_samples] X_test = X_digits[.9 * n_samples:] y_test = y_digits[.9 * n_samples:] knn = neighbors.KNeighborsClassifier() logistic = linear_model.LogisticRegression() print('KNN score: %f' % knn.fit(X_train, y_train).score(X_test, y_test))\ print('LogisticRegression score: %f' % logistic.fit(X_train, y_train).score(X_test, y_test)) </code></pre> <p>This is the code for the Iris dataset which seems to work fine...</p> <pre><code>import numpy as np &gt;&gt;&gt; from sklearn import datasets &gt;&gt;&gt; iris = datasets.load_iris() &gt;&gt;&gt; iris_X = iris.data &gt;&gt;&gt; iris_y = iris.target &gt;&gt;&gt; np.unique(iris_y) array([0, 1, 2]) &gt;&gt;&gt; # Split iris data in train and test data &gt;&gt;&gt; # A random permutation, to split the data randomly &gt;&gt;&gt; np.random.seed(0) &gt;&gt;&gt; indices = np.random.permutation(len(iris_X)) &gt;&gt;&gt; iris_X_train = iris_X[indices[:-10]] &gt;&gt;&gt; iris_y_train = iris_y[indices[:-10]] &gt;&gt;&gt; iris_X_test = iris_X[indices[-10:]] &gt;&gt;&gt; iris_y_test = iris_y[indices[-10:]] &gt;&gt;&gt; # Create and fit a nearest-neighbor classifier &gt;&gt;&gt; from sklearn.neighbors import KNeighborsClassifier &gt;&gt;&gt; knn = KNeighborsClassifier() &gt;&gt;&gt; knn.fit(iris_X_train, iris_y_train) KNeighborsClassifier(algorithm='auto', leaf_size=30, n_neighbors=5, p=2, warn_on_equidistant=True, weights='uniform') &gt;&gt;&gt; knn.predict(iris_X_test) array([1, 2, 1, 0, 0, 0, 2, 1, 2, 0]) &gt;&gt;&gt; iris_y_test array([1, 1, 1, 0, 0, 0, 2, 1, 2, 0]) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload