본문 바로가기
Data Science/Multivariate Analysis

[Multivariate Analysis] 다차원 통계그래프

by AI_Wooah 2022. 3. 13.
  Python R
산점도 행렬
sns.pairplot(social)
#변수의 분포까지 확인 가능
#
sns.pairplot(iris, hue='species', height=2.5)
pairs(social)
상관계수행렬
round(cor(social, use="complete.obs"), 3)
round(social.corr(), 3)
별그림  
stars(social2)
얼굴그림  
faces(social2, face.type=0)

1. R

1) R 산점도 행렬

> social = read.table("/Users/DataAnalytics/MultivariateAnalysis/mva/social.txt", header=T)
> head(social, 3)
  YEAR  POP  GNI VEH CRIME ACCI HOSP
1 1981 3872 1800 572   626  123  773
2 1982 3933 1893 647   664  141  911
3 1983 3991 2076 785   787  170 1116
> pairs(social)
> round(cor(social, use="complete.obs"), 3)
       YEAR   POP   GNI   VEH CRIME  ACCI  HOSP
YEAR  1.000 0.998 0.935 0.981 0.993 0.788 0.991
POP   0.998 1.000 0.939 0.974 0.989 0.804 0.988
GNI   0.935 0.939 1.000 0.925 0.907 0.820 0.934
VEH   0.981 0.974 0.925 1.000 0.984 0.697 0.972
CRIME 0.993 0.989 0.907 0.984 1.000 0.762 0.983
ACCI  0.788 0.804 0.820 0.697 0.762 1.000 0.770
HOSP  0.991 0.988 0.934 0.972 0.983 0.770 1.000

2) 별그림(Star Plot)

> social2 = social[, -1]
> year = social[,1]
> rownames(social2) = year
> stars(social2)

3) 얼굴그림(Faces Plot)

> install.packages("aplpack")
> library(aplpack)
> # faces(social2, face.type=0, na.rm=TRUE) > faces(social2, face.type=0)

> faces(social2, face.type=1)

 

 

2. Python

- 산점도행렬

https://colab.research.google.com/drive/19Ex0LqUkU1OrG-qdpJU2lNsyIHS04_QZ?usp=sharing 

 

UnivariateBivariateMultivariate.ipynb

Colaboratory notebook

colab.research.google.com

import pandas as pd
# 데이터 읽기
social = pd.read_csv("/content/drive/MyDrive/DataAnalytics/MultivariateAnalysis/mva/social.csv")

# (행의 수, 열의 수)
social.shape

 

(25, 7)
# seaborn을 이용하여 산점도행렬 그리기
import seaborn as sns
sns.pairplot(social)

# 상관계수 행렬 구하기 – 소수점 이하 3자리 반올림
round(social.corr(), 3)

- 종으로 구분된 산점도 행렬 그리기

import seaborn as sns
# seaborn에 내장된 iris 데이터 가져오기
iris = sns.load_dataset("iris")
iris.head()

# species로 구분된 산점도행렬 그리기 - 대각선은 각 그룹별 분포
sns.pairplot(iris, hue='species', height=2.5)

 

반응형

댓글