《Python数据分析与挖掘实战》第7章——kmeans1 背景与目标分析3 数据预处理4 模型构建

Posted BabyGo000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《Python数据分析与挖掘实战》第7章——kmeans1 背景与目标分析3 数据预处理4 模型构建相关的知识,希望对你有一定的参考价值。

import numpy as np

import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.spines import Spine
from matplotlib.projections.polar import PolarAxes
from matplotlib.projections import register_projection


def radar_factory(num_vars, frame=\'circle\'):
"""Create a radar chart with `num_vars` axes.

This function creates a RadarAxes projection and registers it.

Parameters
----------
num_vars : int
Number of variables for radar chart.
frame : {\'circle\' | \'polygon\'}
Shape of frame surrounding axes.

"""
# calculate evenly-spaced axis angles
theta = np.linspace(0, 2*np.pi, num_vars, endpoint=False)
# rotate theta such that the first axis is at the top
theta += np.pi/2

def draw_poly_patch(self):
verts = unit_poly_verts(theta)
return plt.Polygon(verts, closed=True, edgecolor=\'k\')

def draw_circle_patch(self):
# unit circle centered on (0.5, 0.5)
return plt.Circle((0.5, 0.5), 0.5)

patch_dict = {\'polygon\': draw_poly_patch, \'circle\': draw_circle_patch}
if frame not in patch_dict:
raise ValueError(\'unknown value for `frame`: %s\' % frame)

class RadarAxes(PolarAxes):

name = \'radar\'
# use 1 line segment to connect specified points
RESOLUTION = 1
# define draw_frame method
draw_patch = patch_dict[frame]

def fill(self, *args, **kwargs):
"""Override fill so that line is closed by default"""
closed = kwargs.pop(\'closed\', True)
return super(RadarAxes, self).fill(closed=closed, *args, **kwargs)

def plot(self, *args, **kwargs):
"""Override plot so that line is closed by default"""
lines = super(RadarAxes, self).plot(*args, **kwargs)
for line in lines:
self._close_line(line)

def _close_line(self, line):
x, y = line.get_data()
# FIXME: markers at x[0], y[0] get doubled-up
if x[0] != x[-1]:
x = np.concatenate((x, [x[0]]))
y = np.concatenate((y, [y[0]]))
line.set_data(x, y)

def set_varlabels(self, labels):
self.set_thetagrids(np.degrees(theta), labels)

def _gen_axes_patch(self):
return self.draw_patch()

def _gen_axes_spines(self):
if frame == \'circle\':
return PolarAxes._gen_axes_spines(self)
# The following is a hack to get the spines (i.e. the axes frame)
# to draw correctly for a polygon frame.

# spine_type must be \'left\', \'right\', \'top\', \'bottom\', or `circle`.
spine_type = \'circle\'
verts = unit_poly_verts(theta)
# close off polygon by repeating first vertex
verts.append(verts[0])
path = Path(verts)

spine = Spine(self, spine_type, path)
spine.set_transform(self.transAxes)
return {\'polar\': spine}

register_projection(RadarAxes)
return theta


def unit_poly_verts(theta):
"""Return vertices of polygon for subplot axes.

This polygon is circumscribed by a unit circle centered at (0.5, 0.5)
"""
x0, y0, r = [0.5] * 3
verts = [(r*np.cos(t) + x0, r*np.sin(t) + y0) for t in theta]
return verts


def example_data():
# The following data is from the Denver Aero

data1 = [
[\'ZL\',\'ZR\',\'ZF\',\'ZM\',\'ZC\'],
(\'R\',
[[0.063,-0.0040000000000000001, -0.22600000000000001,-0.22900000000000001,2.1949999999999998],
[1.161, -0.377, -0.086999999999999994, -0.095000000000000001, -0.159],
[0.48299999999999998,-0.79900000000000004,2.4830000000000001,2.4249999999999998,0.308],
[-0.314,1.6859999999999999,-0.57399999999999995,-0.53700000000000003,-0.17299999999999999],
[-0.69999999999999996, -0.41499999999999998, -0.161, -0.161, -0.253]]
)
]
return data1


if __name__ == \'__main__\':
N = 5
theta = radar_factory(N, frame=\'polygon\')

data = example_data()
spoke_labels = data.pop(0)
fig, axes = plt.subplots(figsize=(9, 9), nrows=2, ncols=2,
subplot_kw=dict(projection=\'radar\'))
fig.subplots_adjust(wspace=0.25, hspace=0.20, top=0.85, bottom=0.05)

colors = [\'b\', \'r\', \'g\', \'m\', \'y\']
# Plot the four cases from the example data on separate axes
for ax, (title, case_data) in zip(axes.flatten(), data):
ax.set_rgrids([0.5, 1, 1.5,2,2.5])

ax.set_title(title, weight=\'bold\', size=\'medium\', position=(0.5, 1.1),
horizontalalignment=\'center\', verticalalignment=\'center\')
for d, color in zip(case_data, colors):
ax.plot(theta, d, color=color)
ax.fill(theta, d, facecolor=color, alpha=0.25)
ax.set_varlabels(spoke_labels)

# add legend relative to top-left plot
ax = axes[0, 0]
plt.rcParams[\'font.sans-serif\'] = [\'SimHei\']
plt.rcParams[\'axes.unicode_minus\'] = False
labels = (list(\'abcde\'))
legend = ax.legend(labels, loc=(0.9, .95),
labelspacing=0.1, fontsize=\'small\')

fig.text(0.5, 0.965, \'5-Factor Solution Profiles Across Four Scenarios\',
horizontalalignment=\'center\', color=\'black\', weight=\'bold\',
size=\'large\')

plt.show()
[/code]

 

此章中,由于matplotlib 官网上是画出一组,四个雷达图,为了整出一个雷达图,可花了博主不少时间(掩面笑哭。。。)

备注:本章节完整代码详见 [ 点击打开链接
](https://github.com/clover95/DataAnalysisbyPython/tree/master/chapter7)


![在这里插入图片描述](https://img-blog.csdnimg.cn/20210608151750993.gif)

以上是关于《Python数据分析与挖掘实战》第7章——kmeans1 背景与目标分析3 数据预处理4 模型构建的主要内容,如果未能解决你的问题,请参考以下文章

R语言游戏数据分析与挖掘:为啥要对游戏进行分析

python数据分析实战-第7章-用matplotlib实现数据可视化

[Python数据挖掘]第6章电力窃漏电用户自动识别

python数据分析与挖掘实战第十章

python数据分析实战-第5章-pandas数据读写

培训 | Python机器学习与数据挖掘案例实战