09.10.2023 Views

Advanced Data Analytics Using Python_ With Machine Learning, Deep Learning and NLP Examples ( 2023)

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 3

Supervised Learning Using Python

model.fit(X, y)

model.score(X, y)

print model.predict(x_test)

Nearest Neighbor Classifier

The nearest neighbor classifier is a simple distance-based classifier. It

calculates the distance of test data from the training data and groups

the distances according to the target class. The target class, which has a

minimum average distance from the test instance, is selected as the class

of the test data. A Python example is shown here:

def Distance(point1, point2, length):

distance = 0

for x in range(length):

distance += pow((point1[x] -point2[x]), 2)

return math.sqrt(distance)

def getClosePoints(trainingData, testData, k):

distances = []

length = len(testInstance)-1

for x in range(len(trainingData)):

dist = Distance(testData, trainingDatat[x], length)

distances.append((trainingData[x], dist))

distances.sort(key=operator.itemgetter(1))

close= []

for x in range(k):

close.append(distances[x][0])

return close

64

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!