Source: View original notebook on GitHub
Category: Machine Learning / Learn ML
Loading data from sklearn
1. MNIST dataset
original at http://yann.lecun.com/exdb/mnist/ (The MNIST database of handwritten digits, available from this page, has a training set of 60,000 examples, and a test set of 10,000 examples.)
- subpart of data is avaliable in sklearn datasets module in
load_digitsbunch object
from sklearn.datasets import load_digits
digits = load_digits()
type(digits)
Output:
sklearn.utils.Bunch
# bunch object is like dictionary
digits.keys()
Output:
dict_keys(['data', 'target', 'target_names', 'images', 'DESCR'])
digits.data.shape
Output:
(1797, 64)
digits.target.shape
Output:
(1797,)
digits.target_names
Output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
digits.images.shape
Output:
(1797, 8, 8)
digits.DESCR
Output:
".. _digits_dataset:\n\nOptical recognition of handwritten digits dataset\n--------------------------------------------------\n\n**Data Set Characteristics:**\n\n :Number of Instances: 5620\n :Number of Attributes: 64\n :Attribute Information: 8x8 image of integer pixels in the range 0..16.\n :Missing Attribute Values: None\n :Creator: E. Alpaydin (alpaydin '@' boun.edu.tr)\n :Date: July; 1998\n\nThis is a copy of the test set of the UCI ML hand-written digits datasets\nhttp://archive.ics.uci.edu/ml/datasets/Optical+Recognition+of+Handwritten+Digits\n\nThe data set contains images of hand-written digits: 10 classes where\neach class refers to a digit.\n\nPreprocessing programs made available by NIST were used to extract\nnormalized bitmaps of handwritten digits from a preprinted form. From a\ntotal of 43 people, 30 contributed to the training set and different 13\nto the test set. 32x32 bitmaps are divided into nonoverlapping blocks of\n4x4 and the number of on pixels are counted in each block. This generates\nan input matrix of 8x8 where each element is an integer in the range\n0..16. This reduces dimensionality and gives invariance to small\ndistortions.\n\nFor info on NIST preprocessing routines, see M. D. Garris, J. L. Blue, G.\nT. Candela, D. L. Dimmick, J. Geist, P. J. Grother, S. A. Janet, and C.\nL. Wilson, NIST Form-Based Handprint Recognition System, NISTIR 5469,\n1994.\n\n.. topic:: References\n\n - C. Kaynak (1995) Methods of Combining Multiple Classifiers and Their\n Applications to Handwritten Digit Recognition, MSc Thesis, Institute of\n Graduate Studies in Science and Engineering, Bogazici University.\n - E. Alpaydin, C. Kaynak (1998) Cascading Classifiers, Kybernetika.\n - Ken Tang and Ponnuthurai N. Suganthan and Xi Yao and A. Kai Qin.\n Linear dimensionalityreduction using relevance weighted LDA. School of\n Electrical and Electronic Engineering Nanyang Technological University.\n 2005.\n - Claudio Gentile. A New Approximate Maximal Margin Classification\n Algorithm. NIPS. 2000."
# this is a common structure in datasets that data is in `data` and labels in target
X = digits.data
Y = digits.target
import matplotlib.pyplot as plt
# lets print one of the image
example_x = X[49] #lets take 50th sample
example_y = Y[49]
print(example_x)
print(example_y) # it is 0's image(grayscale)
Output:
[ 0. 0. 1. 15. 13. 1. 0. 0. 0. 0. 7. 16. 14. 8. 0. 0. 0. 8.
12. 9. 2. 13. 2. 0. 0. 7. 9. 1. 0. 6. 6. 0. 0. 5. 9. 0.
0. 3. 9. 0. 0. 0. 15. 2. 0. 8. 12. 0. 0. 0. 9. 15. 13. 16.
6. 0. 0. 0. 0. 13. 14. 8. 0. 0.]
0
plt.imshow(example_x.reshape(8,8),cmap='gray')
Output:
<matplotlib.image.AxesImage at 0x1330c7b0>
# showing more
fig, axes = plt.subplots(2,5,dpi=100)
for i in range(2):
for j in range(5):
axes[i][j].imshow(X[5*i+j].reshape(8,8),cmap='gray')
2. Boston dataset(Housing price dataset)
from sklearn.datasets import load_boston
boston = load_boston()
boston.keys()
Output:
dict_keys(['data', 'target', 'feature_names', 'DESCR', 'filename'])
X = boston.data
print(X.shape)
Output:
(506, 13)
Y = boston.target
print(Y.shape)
Output:
(506,)
