ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测daiding

简介: ML之kNNC:基于iris莺尾花数据集(PCA处理+三维散点图可视化)利用kNN算法实现分类预测daiding

设计思路

image.png




输出结果

image.pngimage.png















(149, 5)

   5.1  3.5  1.4  0.2  Iris-setosa

0  4.9  3.0  1.4  0.2  Iris-setosa

1  4.7  3.2  1.3  0.2  Iris-setosa

2  4.6  3.1  1.5  0.2  Iris-setosa

3  5.0  3.6  1.4  0.2  Iris-setosa

4  5.4  3.9  1.7  0.4  Iris-setosa

(149, 5)

   Sepal_Length  Sepal_Width  Petal_Length  Petal_Width            type

0           4.5          2.3           1.3          0.3     Iris-setosa

1           6.3          2.5           5.0          1.9  Iris-virginica

2           5.1          3.4           1.5          0.2     Iris-setosa

3           6.3          3.3           6.0          2.5  Iris-virginica

4           6.8          3.2           5.9          2.3  Iris-virginica

切分点: 29

label_classes: ['Iris-setosa', 'Iris-versicolor', 'Iris-virginica']

kNNDIY模型预测,基于原数据: 0.95

kNN模型预测,基于原数据预测: [0.96666667 1.         0.93333333 1.         0.93103448]

kNN模型预测,原数据PCA处理后: [1.         0.96       0.95918367]




核心代码


class KNeighborsClassifier Found at: sklearn.neighbors._classification

class KNeighborsClassifier(NeighborsBase, KNeighborsMixin,

   SupervisedIntegerMixin, ClassifierMixin):

   """Classifier implementing the k-nearest neighbors vote.

 

   Read more in the :ref:`User Guide <classification>`.

 

   Parameters

   ----------

   n_neighbors : int, default=5

   Number of neighbors to use by default for :meth:`kneighbors` queries.

 

   weights : {'uniform', 'distance'} or callable, default='uniform'

   weight function used in prediction.  Possible values:

 

   - 'uniform' : uniform weights.  All points in each neighborhood

   are weighted equally.

   - 'distance' : weight points by the inverse of their distance.

   in this case, closer neighbors of a query point will have a

   greater influence than neighbors which are further away.

   - [callable] : a user-defined function which accepts an

   array of distances, and returns an array of the same shape

   containing the weights.

 

   algorithm : {'auto', 'ball_tree', 'kd_tree', 'brute'}, default='auto'

   Algorithm used to compute the nearest neighbors:

 

   - 'ball_tree' will use :class:`BallTree`

   - 'kd_tree' will use :class:`KDTree`

   - 'brute' will use a brute-force search.

   - 'auto' will attempt to decide the most appropriate algorithm

   based on the values passed to :meth:`fit` method.

 

   Note: fitting on sparse input will override the setting of

   this parameter, using brute force.

 

   leaf_size : int, default=30

   Leaf size passed to BallTree or KDTree.  This can affect the

   speed of the construction and query, as well as the memory

   required to store the tree.  The optimal value depends on the

   nature of the problem.

 

   p : int, default=2

   Power parameter for the Minkowski metric. When p = 1, this is

   equivalent to using manhattan_distance (l1), and euclidean_distance

   (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

 

   metric : str or callable, default='minkowski'

   the distance metric to use for the tree.  The default metric is

   minkowski, and with p=2 is equivalent to the standard Euclidean

   metric. See the documentation of :class:`DistanceMetric` for a

   list of available metrics.

   If metric is "precomputed", X is assumed to be a distance matrix and

   must be square during fit. X may be a :term:`sparse graph`,

   in which case only "nonzero" elements may be considered neighbors.

 

   metric_params : dict, default=None

   Additional keyword arguments for the metric function.

 

   n_jobs : int, default=None

   The number of parallel jobs to run for neighbors search.

   ``None`` means 1 unless in a :obj:`joblib.parallel_backend` context.

   ``-1`` means using all processors. See :term:`Glossary <n_jobs>`

   for more details.

   Doesn't affect :meth:`fit` method.

 

   Attributes

   ----------

   classes_ : array of shape (n_classes,)

   Class labels known to the classifier

 

   effective_metric_ : str or callble

   The distance metric used. It will be same as the `metric` parameter

   or a synonym of it, e.g. 'euclidean' if the `metric` parameter set to

   'minkowski' and `p` parameter set to 2.

 

   effective_metric_params_ : dict

   Additional keyword arguments for the metric function. For most

    metrics

   will be same with `metric_params` parameter, but may also contain the

   `p` parameter value if the `effective_metric_` attribute is set to

   'minkowski'.

 

   outputs_2d_ : bool

   False when `y`'s shape is (n_samples, ) or (n_samples, 1) during fit

   otherwise True.

 

   Examples

   --------

   >>> X = [[0], [1], [2], [3]]

   >>> y = [0, 0, 1, 1]

   >>> from sklearn.neighbors import KNeighborsClassifier

   >>> neigh = KNeighborsClassifier(n_neighbors=3)

   >>> neigh.fit(X, y)

   KNeighborsClassifier(...)

   >>> print(neigh.predict([[1.1]]))

   [0]

   >>> print(neigh.predict_proba([[0.9]]))

   [[0.66666667 0.33333333]]

 

   See also

   --------

   RadiusNeighborsClassifier

   KNeighborsRegressor

   RadiusNeighborsRegressor

   NearestNeighbors

 

   Notes

   -----

   See :ref:`Nearest Neighbors <neighbors>` in the online

    documentation

   for a discussion of the choice of ``algorithm`` and ``leaf_size``.

 

   .. warning::

 

   Regarding the Nearest Neighbors algorithms, if it is found that two

   neighbors, neighbor `k+1` and `k`, have identical distances

   but different labels, the results will depend on the ordering of the

   training data.

 

  https://en.wikipedia.org/wiki/K-nearest_neighbor_algorithm

   """

   @_deprecate_positional_args

   def __init__(self, n_neighbors=5,

       *, weights='uniform', algorithm='auto', leaf_size=30,

       p=2, metric='minkowski', metric_params=None, n_jobs=None, **

       kwargs):

       super().__init__(n_neighbors=n_neighbors, algorithm=algorithm,

        leaf_size=leaf_size, metric=metric, p=p, metric_params=metric_params,

        n_jobs=n_jobs, **kwargs)

       self.weights = _check_weights(weights)

 

   def predict(self, X):

       """Predict the class labels for the provided data.

       Parameters

       ----------

       X : array-like of shape (n_queries, n_features), \

               or (n_queries, n_indexed) if metric == 'precomputed'

           Test samples.

       Returns

       -------

       y : ndarray of shape (n_queries,) or (n_queries, n_outputs)

           Class labels for each data sample.

       """

       X = check_array(X, accept_sparse='csr')

       neigh_dist, neigh_ind = self.kneighbors(X)

       classes_ = self.classes_

       _y = self._y

       if not self.outputs_2d_:

           _y = self._y.reshape((-1, 1))

           classes_ = [self.classes_]

       n_outputs = len(classes_)

       n_queries = _num_samples(X)

       weights = _get_weights(neigh_dist, self.weights)

       y_pred = np.empty((n_queries, n_outputs), dtype=classes_[0].

        dtype)

       for k, classes_k in enumerate(classes_):

           if weights is None:

               mode, _ = stats.mode(_y[neigh_indk], axis=1)

           else:

               mode, _ = weighted_mode(_y[neigh_indk], weights, axis=1)

           mode = np.asarray(mode.ravel(), dtype=np.intp)

           y_pred[:k] = classes_k.take(mode)

     

       if not self.outputs_2d_:

           y_pred = y_pred.ravel()

       return y_pred

 

   def predict_proba(self, X):

       """Return probability estimates for the test data X.

       Parameters

       ----------

       X : array-like of shape (n_queries, n_features), \

               or (n_queries, n_indexed) if metric == 'precomputed'

           Test samples.

       Returns

       -------

       p : ndarray of shape (n_queries, n_classes), or a list of n_outputs

           of such arrays if n_outputs > 1.

           The class probabilities of the input samples. Classes are ordered

           by lexicographic order.

       """

       X = check_array(X, accept_sparse='csr')

       neigh_dist, neigh_ind = self.kneighbors(X)

       classes_ = self.classes_

       _y = self._y

       if not self.outputs_2d_:

           _y = self._y.reshape((-1, 1))

           classes_ = [self.classes_]

       n_queries = _num_samples(X)

       weights = _get_weights(neigh_dist, self.weights)

       if weights is None:

           weights = np.ones_like(neigh_ind)

       all_rows = np.arange(X.shape[0])

       probabilities = []

       for k, classes_k in enumerate(classes_):

           pred_labels = _y[:k][neigh_ind]

           proba_k = np.zeros((n_queries, classes_k.size))

           # a simple ':' index doesn't work right

           for i, idx in enumerate(pred_labels.T): # loop is O(n_neighbors)

               proba_k[all_rowsidx] += weights[:i]

         

           # normalize 'votes' into real [0,1] probabilities

           normalizer = proba_k.sum(axis=1)[:np.newaxis]

           normalizer[normalizer == 0.0] = 1.0

           proba_k /= normalizer

           probabilities.append(proba_k)

     

       if not self.outputs_2d_:

           probabilities = probabilities[0]

       return probabilities







相关文章
|
2月前
|
机器学习/深度学习 算法 安全
【无人机三维路径规划】多目标螳螂搜索算法MOMSA与非支配排序的鲸鱼优化算法NSWOA求解无人机三维路径规划研究(Matlab代码实现)
【无人机三维路径规划】多目标螳螂搜索算法MOMSA与非支配排序的鲸鱼优化算法NSWOA求解无人机三维路径规划研究(Matlab代码实现)
147 0
|
3月前
|
机器学习/深度学习 Dragonfly 人工智能
基于蜻蜓算法优化支持向量机(DA-SVM)的数据多特征分类预测研究(Matlab代码实现)
基于蜻蜓算法优化支持向量机(DA-SVM)的数据多特征分类预测研究(Matlab代码实现)
112 0
|
2月前
|
机器学习/深度学习 算法 调度
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
14种智能算法优化BP神经网络(14种方法)实现数据预测分类研究(Matlab代码实现)
327 0
|
1月前
|
开发框架 算法 .NET
基于ADMM无穷范数检测算法的MIMO通信系统信号检测MATLAB仿真,对比ML,MMSE,ZF以及LAMA
简介:本文介绍基于ADMM的MIMO信号检测算法,结合无穷范数优化与交替方向乘子法,降低计算复杂度并提升检测性能。涵盖MATLAB 2024b实现效果图、核心代码及详细注释,并对比ML、MMSE、ZF、OCD_MMSE与LAMA等算法。重点分析LAMA基于消息传递的低复杂度优势,适用于大规模MIMO系统,为通信系统检测提供理论支持与实践方案。(238字)
|
2月前
|
机器学习/深度学习 边缘计算 并行计算
【无人机三维路径规划】基于遗传算法GA结合粒子群算法PSO无人机复杂环境避障三维路径规划(含GA和PSO对比)研究(Matlab代码代码实现)
【无人机三维路径规划】基于遗传算法GA结合粒子群算法PSO无人机复杂环境避障三维路径规划(含GA和PSO对比)研究(Matlab代码代码实现)
245 2
|
3月前
|
传感器 算法 Python
【无人机设计与控制】改进型粒子群优化算法(IPSO)的无人机三维路径规划研究(Matlab代码实现)
【无人机设计与控制】改进型粒子群优化算法(IPSO)的无人机三维路径规划研究(Matlab代码实现)
171 1
|
2月前
|
算法 调度 决策智能
基于高尔夫优化算法GOA求解无人机三维路径规划研究(Matlab代码实现)
基于高尔夫优化算法GOA求解无人机三维路径规划研究(Matlab代码实现)
|
2月前
|
机器学习/深度学习 存储 算法
基于密集型复杂城市场景下求解无人机三维路径规划的Q-learning 算法研究(Matlab代码实现)
基于密集型复杂城市场景下求解无人机三维路径规划的Q-learning 算法研究(Matlab代码实现)
|
3月前
|
传感器 编解码 分布式计算
【创新未发表】基于吕佩尔狐算法RFO复杂城市地形无人机避障三维航迹规划研究(Matlab代码实现)
【创新未发表】基于吕佩尔狐算法RFO复杂城市地形无人机避障三维航迹规划研究(Matlab代码实现)
105 0
|
3月前
|
机器学习/深度学习 传感器 数据采集
【23年新算法】基于鱼鹰算法OOA-Transformer-BiLSTM多特征分类预测附Matlab代码 (多输入单输出)(Matlab代码实现)
【23年新算法】基于鱼鹰算法OOA-Transformer-BiLSTM多特征分类预测附Matlab代码 (多输入单输出)(Matlab代码实现)
313 0

热门文章

最新文章