我想编写一个函数来返回一个包含多数类别的数字。
我编写了以下函数来计算距离。
[距离 度量已给定(Euclid,Manthan等)。
xTrainInstances-是一个包含所有火车实例的数据框
xSeriesTestVector-是来自测试集的Series对象
def calc_distances(xSeriesTestVector, xTrainInstances, distanceMetric):
distances = np.zeros(xTrainInstances.shape[0])
for i in range(xTrainInstances.shape[0]):
distances[i] = distanceMetric(xSeriesTestVector, xTrainInstances.iloc[i])
return distances
假设我具有以下数据框,幸存的列是我的类别。
Survived
PassengerId
1 0
2 1
3 1
4 1
5 0
[我的问题] >>
我想知道如何实现以下功能?我被卡住了,距离返回了我的距离数组,来自predict_one_instance,我想返回正确的类别
k
Pclass SibSp Parch Fare Age
pid
1 3 1 0 7.2500 22.0
2 1 1 0 71.2833 38.0
3 3 0 0 7.9250 26.0
4 1 1 0 53.1000 35.0
5 3 0 0 8.0500 35.0
Pclass SibSp Parch Fare Age
pid
1 3 1 0 7.2500 22.0
2 1 1 0 71.2833 38.0
3 3 0 0 7.9250 26.0
4 1 1 0 53.1000 35.0
5 3 0 0 8.0500 35.0
def predict_one_instance(xSeriesTestVector,
xTrainInstances,yTrainCategories,distanceMetric,k):
distances = calc_distances(xSeriesTestVector, xTrainInstances,distanceMetric)
我想编写一个函数来返回一个包含多数类别的数字。我编写了以下函数来计算距离。给定的距离度量(Euclid,Manthan等)。 ...
请使用“曼哈顿”查看此示例