Sunday, June 4, 2017

Nearest Neighbor

Analyzed:
http://scikit-learn.org/stable/modules/neighbors.html

>>> X = np.array([[-1,-1], [-2,-1], [-3,-2], [1,1], [2,1], [6,3]])
>>> nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
>>> distances, indices = nbrs.kneighbors(X)
>>> indices
array([[0, 1],
       [1, 0],
       [2, 1],
       [3, 4],
       [4, 3],
       [5, 4]], dtype=int64)
>>> distances
array([[ 0.        ,  1.        ],
       [ 0.        ,  1.        ],
       [ 0.        ,  1.41421356],
       [ 0.        ,  1.        ],
       [ 0.        ,  1.        ],
       [ 0.        ,  4.47213595]])
>>> X = np.array([[-1,-1], [6,6], [-2,-1], [-3,-2], [1,1], [2,1], [6,3]])
>>> nbrs = NearestNeighbors(n_neighbors=2, algorithm='ball_tree').fit(X)
>>> distances, indices = nbrs.kneighbors(X)
>>> indices
array([[0, 2],
       [1, 6],
       [2, 0],
       [3, 2],
       [4, 5],
       [5, 4],
       [6, 1]], dtype=int64)
>>> distances
array([[ 0.        ,  1.        ],
       [ 0.        ,  3.        ],
       [ 0.        ,  1.        ],
       [ 0.        ,  1.41421356],
       [ 0.        ,  1.        ],
       [ 0.        ,  1.        ],
       [ 0.        ,  3.        ]])
>>> nbrs.kneighbors_graph(X).toarray()
array([[ 1.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  1.],
       [ 1.,  0.,  1.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  1.,  0.],
       [ 0.,  0.,  0.,  0.,  1.,  1.,  0.],
       [ 0.,  1.,  0.,  0.,  0.,  0.,  1.]])

Sunday, April 30, 2017

Reading prostate dataset into a variable

I have installed anaconda and created an environment where I have installed all statistics related libraries like NumPy, SciPy, Pandas.


On the ESL website: https://statweb.stanford.edu/~tibs/ElemStatLearn/, I goto Data tab and open the prostate dataset. I then copy this dataset onto an excel (eg. 'prostate.xls'). Note: Each column should be aligned and separated.


I then copy this excel into the C:\ProgramData\Miniconda2\envs\tutorial folder, wherein lies the python.exe.


I then start the application and run the below commands
>>> import pandas
>>> df = pandas.read_excel('prostate.xls')



Essential Links


Dec-2020
https://www.coursera.org/learn/python-data-analysis/home/welcome