怎么用算法分类曲线,python
老师给了我一个题目,用算法分类他给的曲线,用python实现。请教一下python怎么实现?我从来没用过。要求是调参特征组合。我不知道我要干什么。怎么去分类曲线。...
老师给了我一个题目,用算法分类他给的曲线,用python实现。请教一下python怎么实现?我从来没用过。
要求是 调参 特征组合。我不知道我要干什么。怎么去分类曲线。 展开
要求是 调参 特征组合。我不知道我要干什么。怎么去分类曲线。 展开
1个回答
2017-12-11 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
#!/usr/bin/env python#-*- coding: utf-8 -*-######################## Info : Curve Simplify# Version 1.0# Author : Alex Pan# Date : 2017-07-11#######################import numpy as npfrom termcolor import coloredimport ipdb## Data TypeuintType = np.uint8
floatType = np.float32##-----------------------------------------------------------------------------------## Get Distance Between point and [line_start-line_end] Linedef getPoint2LineDistance(point, line_start, line_end):
# Exception
if not isinstance(point, np.ndarray) or not isinstance(line_start, np.ndarray) or not isinstance(line_end, np.ndarray): raise TypeError('All points MUST be numpy.ndarray!') elif point.ndim != 1 or point.shape != line_start.shape or point.shape != line_end.shape: raise ValueError('points dimensions error or NOT matched!') elif (line_start == line_end).all(): raise Exception('line_start is the SAME as line_end!') return np.sqrt(np.sum(np.square(point - line_start)) - np.square(np.sum((line_end - line_start) * (point - line_start))) / np.sum(np.square(line_end - line_start), dtype = floatType))##-----------------------------------------------------------------------------------## Constrcuct np.linspace Array between raw_array[index_start] and raw_array[index_end]def getLinspaceArray(raw_array, index_start, index_end):
# Exception
if not isinstance(raw_array, np.ndarray): raise TypeError('raw_array MUST be numpy.ndarray!') elif index_start < 0 or index_end > raw_array.shape[0] or index_start > index_end: raise ValueError('index_start or index_end INVALID!') # Reconstruct Array by np.linspace Based on keyIndexes
linspaceArray = np.linspace(raw_array[index_start][0], raw_array[index_end][0], num = index_end - index_start + 1, endpoint = True, dtype = floatType) for i in xrange(1, raw_array.shape[1]):
linspaceArray = np.row_stack((linspaceArray, np.linspace(raw_array[index_start][i], raw_array[index_end][i], num = index_end - index_start + 1, endpoint = True, dtype = floatType))) return np.transpose(linspaceArray)##-----------------------------------------------------------------------------------## Compute Error Between 2 Arraysdef computeReconstructError(array_A, array_B):
# Exception
if not isinstance(array_A, np.ndarray) or not isinstance(array_B, np.ndarray): raise TypeError('array_A and array_B MUST be numpy.ndarray!') elif array_A.shape != array_B.shape: raise ValueError('array_A and array_B dimensions NOT matched!') # Vector
if array_A.ndim == array_B.ndim == 1: return np.sqrt(np.sum(np.square(array_A - array_B))) # Array
error_array = array_A - array_B
error_list = [np.sqrt(np.sum(np.square(error))) for error in error_array] return float(sum(error_list)) / len(error_list)##-----------------------------------------------------------------------------------## Function of Curve Simplify Algorithmdef curveSimplify(poses_array, max_key = 10, error_threshold = 0.05):
# Exception
if not isinstance(poses_array, np.ndarray): raise TypeError('poses_array MUST be numpy.ndarray!') # Initialize
N_poses, M_poses = poses_array.shape
keyIndexes = [0, N_poses - 1]
reconstructArray = getLinspaceArray(raw_array = poses_array, index_start = keyIndexes[0], index_end = keyIndexes[-1]) # Divide
flagContinue = True
while flagContinue:
keyIndexes.sort()
keyDeltas = [(keyIndexes[i], keyIndexes[i + 1]) for i in xrange(len(keyIndexes) - 1)] for keyStart, keyEnd in keyDeltas:
distanceList = [getPoint2LineDistance(point = poses_array[i], line_start = poses_array[keyStart], line_end = poses_array[keyEnd]) for i in xrange(keyStart + 1, keyEnd)]
keyNew = keyStart + distanceList.index(max(distanceList)) + 1
keyIndexes.append(keyNew) # Reconstruct [keyStart-keyNew] & [keyNew-keyEnd]
reconstructArray[keyStart : keyNew + 1] = getLinspaceArray(raw_array = poses_array, index_start = keyStart, index_end = keyNew)
reconstructArray[keyNew : keyEnd + 1] = getLinspaceArray(raw_array = poses_array, index_start = keyNew, index_end = keyEnd)
reconstructError = computeReconstructError(poses_array, reconstructArray) # Print Screen
print colored('keyNum:', 'magenta'), len(keyIndexes) print 'recError:', colored(str(reconstructError), 'white') # ipdb.set_trace()
# End Condition: KeyNum or ReconstructError
if len(keyIndexes) == max_key or reconstructError < error_threshold:
flagContinue = False
break
keyIndexes.sort() return keyIndexes, reconstructError
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询