# 机器学习-撰写我们自己的第一个分类器

### 背景介绍

#### 文章代码

```import random
class ScrappyKNN():
def fit(self,x_train,y_train):
self.x_train = x_train
self.y_train = y_train
def predict(self,x_test):
predictions = []
for row in x_test:
label = random.choice(self.y_train)
predictions.append(label)
return predictions

from sklearn import datasets
iris = datasets.load_iris()
x = iris.data
y = iris.target
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size =.5)
my_classifier = ScrappyKNN()
my_classifier.fit(x_train,y_train)
predictions = my_classifier.predict(x_test)
from sklearn.metrics import accuracy_score
print("ScrappyKNN:",accuracy_score(y_test,predictions))```

```from scipy.spatial import distance
#定义欧氏距离计算方法
def euc(a,b):
return distance.euclidean(a,b)

class ScrappyKNN():
def fit(self,x_train,y_train):
self.x_train = x_train
self.y_train = y_train
def predict(self,x_test):
predictions = []
for row in x_test:
label = self.closest(row)
predictions.append(label)
return predictions

def closest(self,row):
best_dist = euc(row,self.x_train[0])
best_index = 0
for i in range(1,len(self.x_train)):
dist = euc(row,self.x_train[i])
if dist < best_dist:
best_dist = dist
best_index = i
return self.y_train[best_index]

from sklearn import datasets
iris = datasets.load_iris()
x = iris.data
y = iris.target
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size =.5)
my_classifier = ScrappyKNN()
my_classifier.fit(x_train,y_train)
predictions = my_classifier.predict(x_test)
from sklearn.metrics import accuracy_score
print("ScrappyKNN-NEW:",accuracy_score(y_test,predictions))```

132 篇文章26 人订阅

0 条评论