Created
July 10, 2023 13:06
-
-
Save SnowyPainter/b5aceaeb0385aa1f2698c44a56c08fe8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from sklearn.datasets import load_iris | |
def andrews_curve(data, weights=None): | |
num_variables = data.shape[1] | |
t = np.linspace(0, 2*np.pi, 100) | |
curve = np.zeros((len(t), 2)) | |
for i in range(num_variables): | |
if weights is None: | |
weights = np.ones(num_variables) | |
theta = t * i | |
x = np.cos(theta) * weights[i] | |
y = np.sin(theta) * weights[i] | |
curve[:, 0] += x | |
curve[:, 1] += y | |
curve[:, 0] /= np.sum(weights) | |
curve[:, 1] /= np.sum(weights) | |
return curve | |
# 붓꽃 데이터셋 로드 | |
iris = load_iris() | |
data = iris.data | |
target = iris.target | |
feature_names = iris.feature_names | |
# 데이터 프레임 생성 | |
df = pd.DataFrame(data, columns=feature_names) | |
# 앤드루스 곡선 그리기 | |
curve = andrews_curve(df) | |
colors = ['r', 'g', 'b'] | |
for i in range(len(curve)): | |
plt.plot(curve[i, 0], curve[i, 1], color=colors[target[i]], marker='o') | |
plt.title("Andrews Curve - Iris Dataset") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment