티스토리 뷰
import numpy as np
import pandas as pd
from sklearn.datasets import load_iris
datasets = load_iris()
print(datasets.feature_names)
#['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)']
x = datasets['data']
y = datasets['target']
df = pd.DataFrame(x,
columns=[['sepal length', 'sepal width', 'petal length', 'petal width']])
# 컬럼명을 사용하기 위해 넘파이 => 판다스 형태로 바꿔줌
# 컬럼명은 히트맵에서 show
컬럼명을 사용하기 위해 넘파이 형태 X ▶ 판다스 형태로 바꿔준다. |
df['Target(Y)'] = y
print(df) #[150 rows x 5 columns]
넘파이 형태의 Y ▶ 판다스 형태로 바꿔주고, X와 Y 컬럼 하나로 합쳐준다. |
print("======================== 상관계수 히트 맵 ========================")
print(df.corr())
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=1.2)
sns.heatmap(data=df.corr(), square=True, annot=True, cbar=True)
plt.show()
df.corr() 데이터프레인 df의 상관계수를 계산 corr() 함수로 간단하게 상관계수를 구할 수 있다. sns.heatmap(시각화할 데이터) seaborn 라이브러리의 히트맵을 이용해서 시각화 할 것이다. |
'Computer Science > Python' 카테고리의 다른 글
[파이썬] os 모듈, 파일(file)과 디렉토리(directory)활용 (0) | 2022.10.03 |
---|---|
[파이썬] 파이썬에서 파일 압축 해제 (0) | 2022.10.01 |
[파이썬] String startswith(), 어떤 문자열로 시작하는지 확인 (0) | 2022.08.08 |
[파이썬] Pandas DataFrame을 numpy 배열로 변환하는 방법 (0) | 2022.08.08 |
[파이썬] 예외처리(try, except, finally, else) (0) | 2022.08.04 |