python导入knn库_knnFeat - K最近邻特征提取的Python实现

本文介绍了使用Python实现KNN进行特征提取的方法,包括数据生成、KNN特征提取算法的详细解释以及可视化过程。推荐使用由momijiame更新的gokinjo库进行KNN特征提取,并提供了安装和演示的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

knnFeat

Feature Extraction with KNN

Description

Python implementation of feature extraction with KNN.

And @momijiame updated my implementation. I recommend to use this:

https://github.com/momijiame/gokinjo

pip install gokinjo

Requirements

Python 3.x

numpy

scikit-learn

scipy

Install

git clone git@github.com:upura/knnFeat.git

cd knnFeat

pip install -r requirements.txt

Demo

Notebook version can be seen here.

Packages for visualization

import numpy as np

%matplotlib inline

import matplotlib.pyplot as plt

Data generation

x0 = np.random.rand(500) - 0.5

x1 = np.random.rand(500) - 0.5

X = np.array(list(zip(x0, x1)))

y = np.array([1 if i0 * i1 > 0 else 0 for (i0, i1) in list(zip(x0, x1))])

Visualization

before.png

Feature extraction with KNN

from knnFeat import knnExtract

newX = knnExtract(X, y, k = 1, folds = 5)

Visualization

19fd84e3dd65cb2ad7d2c3d478676d2f.png

Algorithm

Quote from here.

It generates k * c new features, where c is the number of class labels. The new features are computed from the distances between the observations and their k nearest neighbors inside each class, as follows:

The first test feature contains the distances between each test instance and its nearest neighbor inside the first class.

The second test feature contains the sums of distances between each test instance and its 2 nearest neighbors inside the first class.

The third test feature contains the sums of distances between each test instance and its 3 nearest neighbors inside the first class.

And so on.

This procedure repeats for each class label, generating k * c new features. Then, the new training features are generated using a n-fold CV approach, in order to avoid overfitting.

Development

flake8 .

pytest

pytest -v -m 'success' --cov=.

License

Author

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值