Как построить кривые Эндрюса с помощью панд в Python?

Опубликовано: 27 Марта, 2022

Кривые Эндрюса используются для визуализации многомерных данных путем сопоставления каждого наблюдения с функцией. Он сохраняет средства, дистанцию и отклонения. Дается формулой:

T(n) = x_1/sqrt(2) + x_2 sin(n) + x_3 cos(n) + x_4 sin(2n) + x_5 cos(2n) + …

Построение кривых Эндрюса на графике можно выполнить с помощью метода построения графиков andrews_curves (). модуль. Эта функция генерирует график matplotlib кривых Эндрюса для визуализации кластеров многомерных данных.

Syntax: andrews_curves(frame, class_column, ax=None, samples=200, color=None, colormap=None, **kwargs)

Parameters:

  • frame: It is the data to be plotted.
  • class_column: This is the name of the column containing class names.
  • ax: This parameter is a matplotlib axes object. Its default value is None.
  • samples: This parameter is the number of points to plot in each curve.
  • color: This parameter is an optional parameter and it is the list or tuple of colors to use for the different classes.
  • colormap: This parameter is the string/matplotlib colormap object. Its default value is None.

Returns: This function returns an object of class matplotlip.axis.Axes

Example 1: In the following example, A data frame is made from the CSV file and the data frame is used to plot andrews_curves. The used CSV file is here.

Python3

# importing various package
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
  
# making data frame from csv file
df = pd.read_csv(
    "C:\Users\digital india\Desktop\pand.csv"
)
  
# Creating Andrews curves
x = pd.plotting.andrews_curves(df, "animal")
  
# ploting the Curve
x.plot()
  
# Display
plt.show()

Выход:

Example 2: 

Python3

# importing various package
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
  
# making data frame from csv file
df = pd.read_csv(
    "pandas/master/pandas/tests/io/data/csv/iris.csv"
)
  
# Creating Andrews curves
x = pd.plotting.andrews_curves(df, "Name")
  
# ploting the Curve
x.plot()
  
# Display
plt.show()

Выход:

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.