title: Working with dataset mnist using R and SVD
body: <p>I'm trying to work with images from mnist data. I have downloaded 4 files from site to work with it: train-images-idx3-ubyte.gz, t10k-labels-idx1-ubyte.gz, t10k-images-idx3-ubyte.gz, train-labels-idx1-ubyte.gz. Then I use such code to display 9 numbers from dataset:</p>
  <pre><code>f = file(&quot;D:/.../train-images-idx3-ubyte/train-images.idx3-ubyte&quot;, &quot;rb&quot;)
  # Read Magic Number(A constant numerical or text value used to identify a file format)
  readBin(f, integer(), n=1, endian=&quot;big&quot;)
  # Read Number of Images
  readBin(f, integer(), n=1, endian=&quot;big&quot;)
  # Read Number of Rows
  readBin(f, integer(), n=1, endian=&quot;big&quot;)
  # Read Number of Columns
  readBin(f, integer(), n=1, endian=&quot;big&quot;)
  m &lt;- (matrix(readBin(f,integer(), size=1, n=28*28, endian=&quot;big&quot;),28,28))
  image(m)
  df &lt;- as.data.frame(m)
  df1 &lt;- df[,c(28:1)]
  m&lt;-as.matrix(df1)
  image(m)
  #set the plotting area into a 5*5 array
  par(mfrow=c(3,3))
  #margins for pictures
  par(mar=c(0.1,0.1,0.1,0.1))
  #par(mar=c(2,2,2,2))
  for(i in 1:9){
    m &lt;- matrix(readBin(f,integer(), size=1, n=28*28, endian=&quot;big&quot;),28,28);
    image(m[,28:1])
    print(cov(m))
  }
  </code></pre>
  <p>I need to do such things: using the MNIST database, construct a covariance matrix Σ for data and
  plot the eigenvalues ​​of the matrix Σ using a thermal diagram. For 55,000 objects eigenvalues ​​of the covariance matrix Σ have the following form: <a href="https://i.stack.imgur.com/A2Drr.png" rel="nofollow noreferrer">first image</a>
  Therefore, I should delete all pixels from the database, which are always 0, ie delete all
  pixels whose variance is 0. The number of such values ​​should be approximately 650, ie on
  in practice, only 28^2 - 650 = 134 pixels on the edges are not used. I need to find
  all eigenvalues ​​for a matrix containing only significant pixels (not all 0) and find the number of pixels that have zero variance. How can I do this?</p>