도찐개찐

[데이터분석] 11. 상관분석 본문

PYTHON/데이터분석

[데이터분석] 11. 상관분석

도개진 2023. 1. 2. 13:13

상관분석

  • 두 변수 x, y가 있을때 두 변수가 서로 어떤 관계에 있는지 분석하는 과정
  • 보통 2개의 등간/비율 척도 변수를 분석할때
    • 우선적으로 산점도를 그려 변수간의 관계를 시각화 한 후 상관계수를 계산
  • 변수들 관계 예
    • 키x가 클수록 몸무게y도 증가하는가?
    • 교육을 많이 받으면 수입도 증가하는가?
    • 광고를 많이 하면 판매량이 증가하는가?
    • 운동을 많이 하면 몸무게는 감소하는가?
    • 담배를 줄이면 심혈관 질병 발생비율은 낮아지는가?
  • 두 변수 정의 : 독립변수, 종속변수
    • 독립변수 : 종속변수를 추정하거나 예측하는데 토대를 제공 (원인/설명 변수)
    • 종속변수 : 예측되거나 추정되는 변수, 독립변수의 특정값에 대한 결과를 의미 (결과/반응 변수)
    • 독립변수와 종속변수는 인과관계를 가질 가능성이 높음 (원인-결과 관계)
    • 즉, 변수 X가 다른 변수 Y에 영향을 주는 관계를 의미

광고비와 매출액간의 상관관계

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# A회사의 광고비 지출이 매출에 어느정도 영향을 주는지 파악하기 위해 
# 광고비와 매출액간의 관계를 조사한다고 가정
# 광고비와 매출액간의 연관성 파악을 위해 산점도로 시각화해 보세요

ad = [13,8,10,15,12,15,14,15,17,19,20,21,22,21,25]
sales = [94,70,90,100,95,100,85,95,105,105,110,105,104,105,121]
plt.scatter(ad, sales, color="red")
<matplotlib.collections.PathCollection at 0x7f1907f36b80>

상관관계correlation

  • 두 변수 사이에 서로 주고 받는 관계가 있음
  • 선형관계 여부와 연관성이 존재
    • 양/음의 상관관계, 무 상관관계등 3가지 존재
  • 각 변수의 관계를 기울기에 따라 양/음의 상관계수로 분류함
  • 상관관계를 나타내는 정도를 상관계수라 하는데
  • 한편, 강력하게 관계가 나타나기도 하지만 아리까리하게 있는듯 없는 듯한 관계도 있을수 있음
  • 이러한 차이를 명확하게 하기 위해 상관계수를 사용
    • 피어슨 상관계수

공분산covariance

  • 두 변수가 각 평균으로부터 떨어져 있는 정도를 구한 후 표본의 갯수로 나누어 계산한 것
  • 공분산에서는 $x$와 $y$의 편차들을 서로 곱해주는데, 그 이유는 두 변수간의 관계가 편차들의 곱에 반영이 되기 때문.
  • 하나의 $x$값이 평균보다 클 때 $y$도 평균보다 크면 두 편차의 곱은 양수.
  • 반면 한 $x$값이 평균보다 클 때 $y$는 평균보다 작으면 편차의 곱으은 음수가 되는 점을 이용한 것.
  • 곱한 값들을 모두 더해 양의 관계가 우세한지 음의 관계가 우세한지 측정.
  • 결과가 양수 또는 음수로 나올수 있음
    • 양수 -> 양의 상관관계 있음 ($x$증가->$y$증가)
    • 음수 -> 음의 상관관계 있음 ($x$증가->$y$감소)
# 각 변수의 평균을 구함
amean = np.mean(ad)
smean = np.mean(sales)
plt.scatter(ad, sales, color="red")
# 평균을 기준으로 수직선, 수평선 그림 : axvline, axhline
plt.axvline(amean, color='orange')
plt.axhline(smean, color='blue')
plt.show()

공분산 계산

  • cov(독립변수, 종속변수)
  • 결과가 양 : 양의 상관관계가 있음
  • 결과가 음 : 음의 상관관계가 있음
np.cov(ad, sales)[0, 1]
50.24761904761905

상관계수

  • 두 변수가 함께 변하는 정도를 -1~1 범위의 수치로 나타낸 것
  • 단위로 인한 공분산의 차이를 어느 정도 보완해 줌
  • 일반적으로 칼피어슨이 개발한 피어슨 상관계수를 사용함
  • numpy의 corrcoef 함수를 사용함

상관계수 범위에 따른 상관정도 해석

  • -1.0 ~ -0.7 : 매우 강한 음의 상관관계 있음
  • -0.7 ~ -0.3 : 강한 음의 상관관계 있음
  • -0.3 ~ -0.1 : 약한 음의 상관관계 있음
  • -0.1 ~ 0.1 : 상관관계 없음
  • 0.3 ~ 0.1 : 약한 양의 상관관계 있음
  • 0.7 ~ 0.3 : 강한 양의 상관관계 있음
  • 1.0 ~ 0.7 : 매우 강한 양의 상관관계 있음

상관계수의 종류

  • pearson : 기본적으로 등간/비율척도 변수에 사용 (선형적 관계) ** 주사용 됨
  • spearman : 서열척도 변수에 사용, 비선형적 관계
  • kendall : 서열척도 변수에 사용, 비선형적 관계 (표본수가 적을때 더 유용)
# 상관계수 계산 : corrcoef
np.corrcoef(ad, sales)[0, 1]
0.8879209022406115

놀이기구와 전체 만족도에 대한 상관관계 분석

parks = pd.read_csv('https://raw.githubusercontent.com/siestageek/datasets/master/csv/parks.csv')
parks.head(5)
  weekend num.child distance rides games wait clean overall
0 yes 0 114.648259 87 73 60 89 47
1 yes 2 27.014097 87 78 76 87 65
2 no 1 63.300975 85 80 70 88 61
3 yes 0 25.909931 88 72 66 89 37
4 no 4 54.718307 84 87 74 87 68
plt.figure(figsize=(25,8))
# parks = parks.loc[:, ['rides', 'overall']]
plt.scatter(parks.rides, parks.overall)

# 각 변수의 평균을 구함
rmean = np.mean(parks.rides)
omean = np.mean(parks.overall)

plt.axvline(rmean, color='red')
plt.axhline(omean, color='red')
plt.show()

np.cov(parks.rides, parks.overall)[0, 1]
50.82939078156313
np.corrcoef(parks.rides, parks.overall)[0, 1]
0.5859862820034283

구매자 연령과 판매이익간의 상관관계 분석

sales = pd.read_csv('https://raw.githubusercontent.com/siestageek/datasets/master/txt/applewood.txt', sep=' ')
# 분석에 불필요 문자 제거
sales.Profit = sales.Profit.replace('(\$|\,)', '', regex = True)
sales = sales.astype({'Profit':'int'})
plt.figure(figsize=(25,8))
plt.scatter(sales.Profit, sales.Age)
pmean = np.mean(sales.Profit)
amean = np.mean(sales.Age)
# print(pmean)
plt.axvline(pmean)
plt.axhline(amean)
<matplotlib.lines.Line2D at 0x7f19072d6c40>

np.cov(sales.Profit, sales.Age)[0, 1]
1774.4888268156421
np.corrcoef(sales.Profit, sales.Age)[0, 1]
0.26152899109728933

판촉 전화 횟수에 따라 판매량간의 상관관계 분석

p =  [96, 40, 104, 128, 164, 76, 72, 80, 36, 84, 180, 132, 120, 44, 84]
s = [41, 41, 51, 60, 61, 29, 39, 50, 28, 43, 70, 56, 45, 31, 30]

# cs = pd.DataFrame({'phone':p,''}
plt.scatter(p, s)

pmean = np.mean(p)
smean = np.mean(s)

plt.axvline(pmean)
plt.axhline(smean)
<matplotlib.lines.Line2D at 0x7f190724df70>

np.cov(p, s)[0, 1]
476.57142857142856
np.corrcoef(p, s)[0, 1]
0.8646317910740129

tip과 계산금액의 상관 관계

tips = sns.load_dataset('tips')
tips
plt.scatter(tips.total_bill, tips.tip, color='orange')

m_tot = np.mean(tips.total_bill)
m_tip = np.mean(tips.tip)

plt.axvline(m_tot, color='red')
plt.axhline(m_tip, color='red')
<matplotlib.lines.Line2D at 0x7f19071c8dc0>

np.cov(tips.total_bill, tips.tip)[0, 1]
8.323501629224854
np.corrcoef(tips.total_bill, tips.tip)[0, 1]
0.6757341092113641

캐럿당 가격

diamonds = sns.load_dataset('diamonds')
plt.scatter(diamonds.carat, diamonds.price)
c_mean = np.mean(diamonds.carat)
p_mean = np.mean(diamonds.price)

plt.axvline(c_mean, color='red')
plt.axhline(p_mean, color='red')


plt.show()

np.cov(diamonds.carat, diamonds.price)[0,1]
1742.76536426512
np.corrcoef(diamonds.carat, diamonds.price)[0, 1]
0.9215913011934779

상관계수 행렬과 히트맵

  • 보통 다수의 변수간의 상관계수를 구할때는 일일히 계산하지 않고 상관계수 행렬을 사용함
  • 하지만, 많은 변수들간의 상관계수를 표로 나타내기엔 가독성이 좋지 않음
  • 히트맵이라는 시각화도구를 사용하면 상관계수와 상관정도를 색상으로 표현하기 때문에 가독성과 이해도가 좋아짐
  • 상괄계수 행렬은 객체명.corr()를 이용

놀이동산 데이터에 대한 상관계수 행렬

parks = pd.read_csv('https://raw.githubusercontent.com/siestageek/datasets/master/csv/parks.csv')
# parks.head(5)
# parks.corr(numeric_only=True)#
parks.corr()
  num.child distance rides games wait clean overall
num.child 1.000000 -0.012136 -0.040260 0.004658 -0.020973 -0.013452 0.319480
distance -0.012136 1.000000 -0.009526 -0.006503 -0.007392 0.003969 0.087126
rides -0.040260 -0.009526 1.000000 0.455185 0.314200 0.789565 0.585986
games 0.004658 -0.006503 0.455185 1.000000 0.299105 0.516980 0.437468
wait -0.020973 -0.007392 0.314200 0.299105 1.000000 0.367885 0.572622
clean -0.013452 0.003969 0.789565 0.516980 0.367885 1.000000 0.639398
overall 0.319480 0.087126 0.585986 0.437468 0.572622 0.639398 1.000000

놀이동산 데이터에 대한 상관계수 히트맵

  • heatmap(상관계수행렬, 옵션)
sns.heatmap(parks.iloc[:, 2:].corr(), annot=True, fmt='.2f')
plt.show()

보스톤 집값 데이터를 이용한 상관 분석

  • 1978 보스턴 주택 가격, 506개 타운의 주택 가격 중앙값 (단위 1,000 달러)
  • 범죄율, 찰스강 인접도, 고속도로 접근성 등의 정보가 저장
    • CRIM 범죄율
    • INDUS 비소매상업지역 면적 비율
    • NOX 일산화질소 농도
    • RM 주택당 방 수
    • LSTAT 인구 중 하위 계층 비율
    • B 인구 중 흑인 비율
    • PTRATIO 학생/교사 비율
    • ZN 25,000 평방피트를 초과 거주지역 비율
    • CHAS 찰스강의 경계에 위치 유무 (1 :찰스강 경계 위치)
    • AGE 1940년 이전에 건축된 주택의 비율
    • RAD 방사형 고속도로까지의 거리
    • DIS 직업센터의 거리
    • TAX 재산세율
    • MEDV 주택 가격 중앙값
!conda install -y scikit-learn
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: /opt/miniconda3

  added / updated specs:
    - scikit-learn


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    certifi-2022.12.7          |   py39h06a4308_0         150 KB
    conda-22.11.1              |   py39h06a4308_4         927 KB
    pluggy-1.0.0               |   py39h06a4308_1          28 KB
    ruamel.yaml-0.17.21        |   py39h5eee18b_0         178 KB
    ruamel.yaml.clib-0.2.6     |   py39h5eee18b_1         140 KB
    ------------------------------------------------------------
                                           Total:         1.4 MB

The following NEW packages will be INSTALLED:

  pluggy             pkgs/main/linux-64::pluggy-1.0.0-py39h06a4308_1 None
  ruamel.yaml        pkgs/main/linux-64::ruamel.yaml-0.17.21-py39h5eee18b_0 None
  ruamel.yaml.clib   pkgs/main/linux-64::ruamel.yaml.clib-0.2.6-py39h5eee18b_1 None

The following packages will be UPDATED:

  certifi                          2022.9.24-py39h06a4308_0 --> 2022.12.7-py39h06a4308_0 None
  conda                               22.9.0-py39h06a4308_0 --> 22.11.1-py39h06a4308_4 None



Downloading and Extracting Packages
pluggy-1.0.0         | 28 KB     | ##################################### | 100% 
ruamel.yaml-0.17.21  | 178 KB    | ##################################### | 100% 
certifi-2022.12.7    | 150 KB    | ##################################### | 100% 
conda-22.11.1        | 927 KB    | ##################################### | 100% 
ruamel.yaml.clib-0.2 | 140 KB    | ##################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Retrieving notices: ...working... done
from sklearn.datasets import load_boston
import sklearn
sklearn.__version__
'1.1.3'
boston = load_boston()
print(boston.DESCR)
.. _boston_dataset:

Boston house prices dataset
---------------------------

**Data Set Characteristics:**  

    :Number of Instances: 506 

    :Number of Attributes: 13 numeric/categorical predictive. Median Value (attribute 14) is usually the target.

    :Attribute Information (in order):
        - CRIM     per capita crime rate by town
        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.
        - INDUS    proportion of non-retail business acres per town
        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
        - NOX      nitric oxides concentration (parts per 10 million)
        - RM       average number of rooms per dwelling
        - AGE      proportion of owner-occupied units built prior to 1940
        - DIS      weighted distances to five Boston employment centres
        - RAD      index of accessibility to radial highways
        - TAX      full-value property-tax rate per $10,000
        - PTRATIO  pupil-teacher ratio by town
        - B        1000(Bk - 0.63)^2 where Bk is the proportion of black people by town
        - LSTAT    % lower status of the population
        - MEDV     Median value of owner-occupied homes in $1000's

    :Missing Attribute Values: None

    :Creator: Harrison, D. and Rubinfeld, D.L.

This is a copy of UCI ML housing dataset.
https://archive.ics.uci.edu/ml/machine-learning-databases/housing/


This dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.

The Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic
prices and the demand for clean air', J. Environ. Economics & Management,
vol.5, 81-102, 1978.   Used in Belsley, Kuh & Welsch, 'Regression diagnostics
...', Wiley, 1980.   N.B. Various transformations are used in the table on
pages 244-261 of the latter.

The Boston house-price data has been used in many machine learning papers that address regression
problems.   

.. topic:: References

   - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.
   - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.



/opt/miniconda3/lib/python3.9/site-packages/sklearn/utils/deprecation.py:87: FutureWarning: Function load_boston is deprecated; `load_boston` is deprecated in 1.0 and will be removed in 1.2.

    The Boston housing prices dataset has an ethical problem. You can refer to
    the documentation of this function for further details.

    The scikit-learn maintainers therefore strongly discourage the use of this
    dataset unless the purpose of the code is to study and educate about
    ethical issues in data science and machine learning.

    In this special case, you can fetch the dataset from the original
    source::

        import pandas as pd
        import numpy as np

        data_url = "http://lib.stat.cmu.edu/datasets/boston"
        raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
        data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
        target = raw_df.values[1::2, 2]

    Alternative datasets include the California housing dataset (i.e.
    :func:`~sklearn.datasets.fetch_california_housing`) and the Ames housing
    dataset. You can load the datasets as follows::

        from sklearn.datasets import fetch_california_housing
        housing = fetch_california_housing()

    for the California housing dataset and::

        from sklearn.datasets import fetch_openml
        housing = fetch_openml(name="house_prices", as_frame=True)

    for the Ames housing dataset.
  warnings.warn(msg, category=FutureWarning)

 

### 보스턴 집값 데이터 : 독립변수
boston.data[:3]
array([[6.3200e-03, 1.8000e+01, 2.3100e+00, 0.0000e+00, 5.3800e-01,
        6.5750e+00, 6.5200e+01, 4.0900e+00, 1.0000e+00, 2.9600e+02,
        1.5300e+01, 3.9690e+02, 4.9800e+00],
       [2.7310e-02, 0.0000e+00, 7.0700e+00, 0.0000e+00, 4.6900e-01,
        6.4210e+00, 7.8900e+01, 4.9671e+00, 2.0000e+00, 2.4200e+02,
        1.7800e+01, 3.9690e+02, 9.1400e+00],
       [2.7290e-02, 0.0000e+00, 7.0700e+00, 0.0000e+00, 4.6900e-01,
        7.1850e+00, 6.1100e+01, 4.9671e+00, 2.0000e+00, 2.4200e+02,
        1.7800e+01, 3.9283e+02, 4.0300e+00]])
### 보스턴 집값 데이터 : 종속변수
boston.target[:3]
array([24. , 21.6, 34.7])
### 보스톤 집값 데이터 : 종속변수
boston.target[:3]
array([24. , 21.6, 34.7])
### 보스톤 집값 데이터를 데이터 프레임으로 생성
df = pd.DataFrame(boston.data, columns=boston.feature_names)
df['PRICE'] = boston.target
df.head()
  CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT PRICE
0 0.00632 18.0 2.31 0.0 0.538 6.575 65.2 4.0900 1.0 296.0 15.3 396.90 4.98 24.0
1 0.02731 0.0 7.07 0.0 0.469 6.421 78.9 4.9671 2.0 242.0 17.8 396.90 9.14 21.6
2 0.02729 0.0 7.07 0.0 0.469 7.185 61.1 4.9671 2.0 242.0 17.8 392.83 4.03 34.7
3 0.03237 0.0 2.18 0.0 0.458 6.998 45.8 6.0622 3.0 222.0 18.7 394.63 2.94 33.4
4 0.06905 0.0 2.18 0.0 0.458 7.147 54.2 6.0622 3.0 222.0 18.7 396.90 5.33 36.2

보스톤 집값 데이터 상관계수 행렬

df.corr()
  CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX PTRATIO B LSTAT PRICE
CRIM 1.000000 -0.200469 0.406583 -0.055892 0.420972 -0.219247 0.352734 -0.379670 0.625505 0.582764 0.289946 -0.385064 0.455621 -0.388305
ZN -0.200469 1.000000 -0.533828 -0.042697 -0.516604 0.311991 -0.569537 0.664408 -0.311948 -0.314563 -0.391679 0.175520 -0.412995 0.360445
INDUS 0.406583 -0.533828 1.000000 0.062938 0.763651 -0.391676 0.644779 -0.708027 0.595129 0.720760 0.383248 -0.356977 0.603800 -0.483725
CHAS -0.055892 -0.042697 0.062938 1.000000 0.091203 0.091251 0.086518 -0.099176 -0.007368 -0.035587 -0.121515 0.048788 -0.053929 0.175260
NOX 0.420972 -0.516604 0.763651 0.091203 1.000000 -0.302188 0.731470 -0.769230 0.611441 0.668023 0.188933 -0.380051 0.590879 -0.427321
RM -0.219247 0.311991 -0.391676 0.091251 -0.302188 1.000000 -0.240265 0.205246 -0.209847 -0.292048 -0.355501 0.128069 -0.613808 0.695360
AGE 0.352734 -0.569537 0.644779 0.086518 0.731470 -0.240265 1.000000 -0.747881 0.456022 0.506456 0.261515 -0.273534 0.602339 -0.376955
DIS -0.379670 0.664408 -0.708027 -0.099176 -0.769230 0.205246 -0.747881 1.000000 -0.494588 -0.534432 -0.232471 0.291512 -0.496996 0.249929
RAD 0.625505 -0.311948 0.595129 -0.007368 0.611441 -0.209847 0.456022 -0.494588 1.000000 0.910228 0.464741 -0.444413 0.488676 -0.381626
TAX 0.582764 -0.314563 0.720760 -0.035587 0.668023 -0.292048 0.506456 -0.534432 0.910228 1.000000 0.460853 -0.441808 0.543993 -0.468536
PTRATIO 0.289946 -0.391679 0.383248 -0.121515 0.188933 -0.355501 0.261515 -0.232471 0.464741 0.460853 1.000000 -0.177383 0.374044 -0.507787
B -0.385064 0.175520 -0.356977 0.048788 -0.380051 0.128069 -0.273534 0.291512 -0.444413 -0.441808 -0.177383 1.000000 -0.366087 0.333461
LSTAT 0.455621 -0.412995 0.603800 -0.053929 0.590879 -0.613808 0.602339 -0.496996 0.488676 0.543993 0.374044 -0.366087 1.000000 -0.737663
PRICE -0.388305 0.360445 -0.483725 0.175260 -0.427321 0.695360 -0.376955 0.249929 -0.381626 -0.468536 -0.507787 0.333461 -0.737663 1.000000

보스톤 집값 데이터 상관계수 히트맵

plt.figure(figsize=(25,8))
sns.heatmap(df.corr(), annot=True, fmt='.2f')
plt.tight_layout()

방갯수와 주택가격간의 선점도

plt.scatter(df.RM, df.PRICE)
plt.show()

하위계층비율과 주택가격 선점도

plt.scatter(df.LSTAT, df.PRICE)
plt.show()

산점도 행렬

  • 여러개의 연속형 변수에 대해서 각각 쌍을 이루어서
  • 산점도를 그려서 한꺼번에 변수 간 관계를 일목요연하게 볼 수 있음
  • pandas의 scatter_mattrix를 이용하거나
  • seaborn의 pairplot를 이용해서 시각화 할 수 있음
from pandas.plotting import scatter_matrix
scatter_matrix(df, figsize=(10,10), diagonal='kde')
plt.show()

sns.pairplot(df)
<seaborn.axisgrid.PairGrid at 0x7f190266a550>

from sklearn.datasets import fetch_california_housing
housing = fetch_california_housing()
print(housing)
{'data': array([[   8.3252    ,   41.        ,    6.98412698, ...,    2.55555556,
          37.88      , -122.23      ],
       [   8.3014    ,   21.        ,    6.23813708, ...,    2.10984183,
          37.86      , -122.22      ],
       [   7.2574    ,   52.        ,    8.28813559, ...,    2.80225989,
          37.85      , -122.24      ],
       ...,
       [   1.7       ,   17.        ,    5.20554273, ...,    2.3256351 ,
          39.43      , -121.22      ],
       [   1.8672    ,   18.        ,    5.32951289, ...,    2.12320917,
          39.43      , -121.32      ],
       [   2.3886    ,   16.        ,    5.25471698, ...,    2.61698113,
          39.37      , -121.24      ]]), 'target': array([4.526, 3.585, 3.521, ..., 0.923, 0.847, 0.894]), 'frame': None, 'target_names': ['MedHouseVal'], 'feature_names': ['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude', 'Longitude'], 'DESCR': '.. _california_housing_dataset:\n\nCalifornia Housing dataset\n--------------------------\n\n**Data Set Characteristics:**\n\n    :Number of Instances: 20640\n\n    :Number of Attributes: 8 numeric, predictive attributes and the target\n\n    :Attribute Information:\n        - MedInc        median income in block group\n        - HouseAge      median house age in block group\n        - AveRooms      average number of rooms per household\n        - AveBedrms     average number of bedrooms per household\n        - Population    block group population\n        - AveOccup      average number of household members\n        - Latitude      block group latitude\n        - Longitude     block group longitude\n\n    :Missing Attribute Values: None\n\nThis dataset was obtained from the StatLib repository.\nhttps://www.dcc.fc.up.pt/~ltorgo/Regression/cal_housing.html\n\nThe target variable is the median house value for California districts,\nexpressed in hundreds of thousands of dollars ($100,000).\n\nThis dataset was derived from the 1990 U.S. census, using one row per census\nblock group. A block group is the smallest geographical unit for which the U.S.\nCensus Bureau publishes sample data (a block group typically has a population\nof 600 to 3,000 people).\n\nAn household is a group of people residing within a home. Since the average\nnumber of rooms and bedrooms in this dataset are provided per household, these\ncolumns may take surpinsingly large values for block groups with few households\nand many empty houses, such as vacation resorts.\n\nIt can be downloaded/loaded using the\n:func:`sklearn.datasets.fetch_california_housing` function.\n\n.. topic:: References\n\n    - Pace, R. Kelley and Ronald Barry, Sparse Spatial Autoregressions,\n      Statistics and Probability Letters, 33 (1997) 291-297\n'}

캘리포니아 주택가격 상관분석

  • MedInc 블록의 중간 소득
  • HouseAge 블록의 중간 주택 연도
  • AveRooms 평균 방 수
  • AveBedrms 평균 침실 수
  • Population 블록 내 거주중인 인구수
  • AveOccup 평균 주택점유율
  • Latitude 주택 블록 위도
  • Longitude 주택 블록 경도
df = pd.DataFrame(housing.data, columns=housing.feature_names)
df = df.iloc[:, :6]
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 20640 entries, 0 to 20639
Data columns (total 6 columns):
 #   Column      Non-Null Count  Dtype  
---  ------      --------------  -----  
 0   MedInc      20640 non-null  float64
 1   HouseAge    20640 non-null  float64
 2   AveRooms    20640 non-null  float64
 3   AveBedrms   20640 non-null  float64
 4   Population  20640 non-null  float64
 5   AveOccup    20640 non-null  float64
dtypes: float64(6)
memory usage: 967.6 KB
df.corr()
  MedInc HouseAge AveRooms AveBedrms Population AveOccup
MedInc 1.000000 -0.119034 0.326895 -0.062040 0.004834 0.018766
HouseAge -0.119034 1.000000 -0.153277 -0.077747 -0.296244 0.013191
AveRooms 0.326895 -0.153277 1.000000 0.847621 -0.072213 -0.004852
AveBedrms -0.062040 -0.077747 0.847621 1.000000 -0.066197 -0.006181
Population 0.004834 -0.296244 -0.072213 -0.066197 1.000000 0.069863
AveOccup 0.018766 0.013191 -0.004852 -0.006181 0.069863 1.000000
df['target'] = housing.target
scatter_matrix(df, figsize=(10,10), diagonal='kde')
plt.show()

sns.heatmap(df.corr(), annot=True, fmt='.2f')
plt.tight_layout()

tmean = np.mean(df.target)
mmean = np.mean(df.MedInc)

plt.scatter(df.target, df.MedInc)
plt.axvline(tmean, color='red')
plt.axhline(mmean, color='red')

plt.show()

np.cov(df.target, df.MedInc)[0, 1]
1.508474827926604
np.corrcoef(df.target, df.MedInc)[0, 1]
0.6880752079585474
728x90
Comments