Files
Machine-Learning-Collection/ML/algorithms/svm/__pycache__/knn.cpython-37.pyc

32 lines
3.2 KiB
Plaintext
Raw Normal View History

2021-01-30 21:49:15 +01:00
B
<00>ˢ^<5E><00>@slddlZGdd<03>d<03>Zedkrhejjdd<06>ejddd <09>Ze<01>d
<EFBFBD>Zed d <0C>Z e <09>
ee<08>e j edd <0A>Z dS)<0E>Nc@sFeZdZdd<02>Zdd<04>Zddd<07>Zdd <09>Zd
d <0B>Zd d <0A>Zdd<0F>Z dS)<12>KNearestNeighborcCs||_d|_dS)Ng:<3A>0<EFBFBD><30>yE>)<02>k<>eps)<02>selfr<00>r<00>+C:\Users\aladd\Desktop\ML_videos\svm\knn.py<70>__init__szKNearestNeighbor.__init__cCs||_||_dS)N)<02>X_train<69>y_train)r<00>X<>yrrr<00>trainszKNearestNeighbor.trainrcCs<|dkr|<00>|<01>}n|dkr(|<00>|<01>}n
|<00>|<01>}|<00>|<03>S)Nr<00>)<03>compute_distance_vectorized<65>compute_distance_one_loop<6F>predict_labels)r<00>X_test<73> num_loops<70> distancesrrr<00>predict s   
zKNearestNeighbor.predictc Cs<>|jd}|jjd}t<02>||f<02>}xbt|<02>D]V}xPt|<03>D]D}t<02>|jt<02>||dd<02>f|j|dd<02>fd<00><00>|||f<q<Wq.W|S)zp
Inefficient naive implementation, use only
as a way of understanding what kNN is doing
rN<>)<08>shaper <00>np<6E>zeros<6F>range<67>sqrtr<00>sum)rr<00>num_test<73> num_trainr<00>i<>jrrr<00>compute_distance_two_loopss
 Hz+KNearestNeighbor.compute_distance_two_loopsc
Csv|jd}|jjd}t<02>||f<02>}xLt|<02>D]@}t<02>|jtj|j||dd<02>fddd<05><00>||dd<02>f<q.W|S)z<>
Much better than two-loops but not as fast as fully vectorized version.
Utilize Numpy broadcasting in X_train - X_test[i,:]
rNrr)<01>axis)rr rrrrrr)rrrrrrrrrr)s 
 @z*KNearestNeighbor.compute_distance_one_loopcCsXtj|dddd<04>}tj|jdddd<04>}t<00>||jj<04>}t<00>|j|d||j<00>S)a<>
Can be tricky to understand, we utilize heavy
vecotorization as well as numpy broadcasting.
Idea: if we have two vectors a, b (two examples)
and for vectors we can compute (a-b)^2 = a^2 - 2a (dot) b + b^2
expanding on this and doing so for every vector lends to the
heavy vectorized formula for all examples at the same time.
rrT)r"<00>keepdims)rrr <00>dot<6F>Trr)rrZX_test_squaredZX_train_squaredZtwo_X_test_X_trainrrrr8s z,KNearestNeighbor.compute_distance_vectorizedcCsn|jd}t<01>|<02>}xTt|<02>D]H}t<01>||dd<00>f<00>}|j|d|j<06><00>t<08>}t<01> t<01>
|<06><01>||<qW|S)Nr) rrrr<00>argsortr
r<00>astype<70>int<6E>argmax<61>bincount)rrr<00>y_predr<00> y_indicesZk_closest_classesrrrrHs

zKNearestNeighbor.predict_labelsN)r)
<EFBFBD>__name__<5F>
__module__<EFBFBD> __qualname__rr rr!rrrrrrrrs
r<00>__main__r)<01>seedzexample_data/data.txt<78>,)<01> delimiterzexample_data/targets.txt<78>)r)r) <0A>numpyrrr-<00>randomr1<00>loadtxtr r <00>KNNr rr+rrrr<00><module>sP