产生螺旋分类样本集合 SpiralData

本文介绍了一种使用Python生成螺旋分类数据集的方法。通过调整参数如样本数量、螺旋臂数、旋转角度等,可以创建不同形态的数据集,适用于测试各种分类算法。

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

 

01 产生螺旋数据


螺旋分类集合是用来测试分类器的典型试金石。在 N-arms Spiral Data 给出了产生螺旋数据集合的PYTHON程序。

  • arg: 100 --arms 3
    ▲ 三条洛讯数据集合

    ▲ 三条洛讯数据集合

  • arg: 200 --arms 2
    ▲ 两条螺旋线数据集合

    ▲ 两条螺旋线数据集合

  • arg: 100 --arms 2 --end 270
    ▲ 两条螺旋线数据集合

    ▲ 两条螺旋线数据集合

 

02 PYTHON程序


#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# SPIRALDATA.PY                -- by Dr. ZhuoQing 2020-11-23
#
# Note:
#============================================================

from headm import *

import argparse
import math
import numpy as np

def rotate_point(point, angle):
    """Rotate two point by an angle.
    Parameters
    ----------
    point: 2d numpy array
        The coordinate to rotate.
    angle: float
        The angle of rotation of the point, in degrees.
    Returns
    -------
    2d numpy array
        Rotated point.
    """
    rotation_matrix = np.array([[np.cos(angle), -np.sin(angle)], [np.sin(angle), np.cos(angle)]])
    rotated_point = rotation_matrix.dot(point)
    return rotated_point

def generate_spiral(samples, start, end, angle, noise):
    """Generate a spiral of points.
    Given a starting end, an end angle and a noise factor, generate a spiral of points along
    an arc.
    Parameters
    ----------
    samples: int
        Number of points to generate.
    start: float
        The starting angle of the spiral in degrees.
    end: float
        The end angle at which to rotate the points, in degrees.
    angle: float
        Angle of rotation in degrees.
    noise: float
        The noisyness of the points inside the spirals. Needs to be less than 1.
    """
    # Generate points from the square root of random data inside an uniform distribution on [0, 1).
    points = math.radians(start) + np.sqrt(np.random.rand(samples, 1)) * math.radians(end)

    # Apply a rotation to the points.
    rotated_x_axis = np.cos(points) * points + np.random.rand(samples, 1) * noise
    rotated_y_axis = np.sin(points) * points + np.random.rand(samples, 1) * noise

    # Stack the vectors inside a samples x 2 matrix.
    rotated_points = np.column_stack((rotated_x_axis, rotated_y_axis))
    return np.apply_along_axis(rotate_point, 1, rotated_points, math.radians(angle))

def main():
    parser = argparse.ArgumentParser(description='Generate n-arm spiral')
    parser.add_argument('count', type=int, help='Number of samples to generate per arm', default=2)
    parser.add_argument('--arms', type=int, help='Number of args to generate', default=2)
    parser.add_argument('--angle', type=float, help='Angle between each arm.', default=180)
    parser.add_argument('--auto-angle', type=bool, default=True,
                        help='Automatically choose the angle for the arms')
    parser.add_argument('--start', type=float, help='Start angle of the arms', default=0)
    parser.add_argument('--end', type=float, default=360,
                        help='End angle of the arms. A value of 360 corresponds \
                              to a full circle.')
    parser.add_argument('--noise', type=float, help='Noise for the arms', default=0.5)
    parser.add_argument('--filename', type=str, help='Name of the file to save the dataset',
                        default='n_arm_spiral')

    args = parser.parse_args()

    # Create a list of the angles at which to rotate the arms.
    # Either we find the angles automatically by dividing by the number of arms
    # Or we just use the angle given by the user.
    classes = np.empty((0, 3))
    angles = [((360 / args.arms) if args.auto_angle else args.angle) * i for i in range(args.arms)]

    for i, angle in enumerate(angles):
        points = generate_spiral(args.count, args.start, args.end, angle, args.noise)
        classified_points = np.hstack((points, np.full((args.count, 1), i)))
        classes = np.concatenate((classes, classified_points))

        if i == 0:      cstr = 'r'
        elif i == 1:    cstr = 'b'
        else:           cstr = 'y'

        plt.scatter(points[:,0], points[:, 1], color=cstr)

    printf(classes.shape)
    tspsave('spiral', data=classes)

    plt.xlabel("x")
    plt.ylabel("y")
    plt.grid(True)
    plt.tight_layout()
    plt.show()

#    np.savetxt(args.filename + '.csv', classes, fmt=['%10.15f', '%10.15f', '%i'], delimiter=';')

if __name__ == '__main__':
    main()

#------------------------------------------------------------
#        END OF FILE : SPIRALDATA.PY
#============================================================
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

卓晴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值