본문 바로가기

IT/Python

[파이썬] Data Analysis

- Descriptive Statistic: 기술적 통계학, 서술적 통계학, 연속형 데이터를 몇개의 의미있는 수치로 요약하여 데이터의 전반적인 특성을 파악하는것. 대부분 통계학의 기초인 평균, 편차 및 기본적인 통계값을 의미한다.

 

- Correlation (상관 관계):  한쪽이 증가하면 다른 한쪽도 증가하거나 반대로 감소하는 경향을 인정하는 두 변량 사이의 통계적 관계.

예) df[[ ]].corr() 을 통하여 correlation value를 구할 수 있다.

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

path='https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/DA0101EN/automobileEDA.csv'
df = pd.read_csv(path)
# print(df.head())

print(df[['bore','stroke' ,'compression-ratio','horsepower']].corr())

>>

 

- 선형회귀곡선: 선형회귀 곡선을 그릴 수 있다. (sns.regplot)

sns.regplot(x="engine-size", y="price", data=df)
plt.ylim(0,)
plt.show()

>>

 

- value_count( ) : Data Column 당 각 Data가 몇개씩 있는지 확인 할 수 있는 method

 

- Box Plots (sns.boxplot)

 

 

예) Groupby를 사용하여 보기좋은 Table 만들기

df_group_one = df[['drive-wheels','body-style','price']]
df_group_one = df_group_one.groupby(['body-style','drive-wheels'],as_index=False).mean()
print(df_group_one)

>>

 

 

 

예) Pivot()을 사용하여 Pivot Table 만들기

grouped_pivot = df_group_one.pivot(index='drive-wheels',columns='body-style')
print(grouped_pivot)

>>

 

 

- Heatmap 그리기

plt.pcolor(grouped_pivot, cmap='RdBu')
plt.colorbar()
plt.show()

>>

 

 

 

 

 

- Pearson Correlation: X & Y 의 종속성(linear dependence)을 측정한다.

The Pearson Correlation measures the linear dependence between two variables X and Y.

The resulting coefficient is a value between -1 and 1 inclusive, where:

 1: Total positive linear correlation. (서로 영향력이 깊은 두 변수)

 0: No linear correlation, the two variables most likely do not affect each other. (서로 영향력 없는 두 변수)

 -1: Total negative linear correlation. (서로 반대로 영향력이 있는 두 변수)

 

P-value: 두 변수를 통계적으로 의미가 있는 값인지 측정하기 위하여 확인해야 한다.

What is this P-value? The P-value is the probability value that the correlation between these two variables is statistically significant. Normally, we choose a significance level of 0.05, which means that we are 95% confident that the correlation between the variables is significant.

By convention, when the

p-value is << 0.001: we say there is strong evidence that the correlation is significant.

the p-value is << 0.05: there is moderate evidence that the correlation is significant.

the p-value is << 0.1: there is weak evidence that the correlation is significant.

the p-value is >> 0.1: there is no evidence that the correlation is significant.

from scipy import stats

pearson_coef, p_value = stats.pearsonr(df['wheel-base'], df['price'])
print("The Pearson Correlation Coefficient is", pearson_coef, " with a P-value of P =", p_value)  

>>

The Pearson Correlation Coefficient is 0.5846418222655079  with a P-value of P = 8.076488270733275e-20

 

 

 

 

 

- ANOVA: Analysis of Variance

ANOVA(분산분석)은 두 개 이상 집단들의 평균을 비교하는 통계분석 기법이다. 다시 말해, 분산분석은 두 개 이상 집단들의 평균 간 차이에 대한 통계적 유의성을 검증하는 방법이다. 기본적으로 분산분석은 회귀분석의 특별한 형태이다.

ANOVA를 통하여 2개의 값을 가져온다. 

 

P-Value는 위에서 설명한 바와 같고, F-test Score는 평균값과 얼마나 차이나는 Data set으로 구성 되어있는지 알려준다.

The Analysis of Variance (ANOVA) is a statistical method used to test whether there are significant differences between the means of two or more groups. ANOVA returns two parameters:

F-test score: ANOVA assumes the means of all groups are the same, calculates how much the actual means deviate from the assumption, and reports it as the F-test score. A larger score means there is a larger difference between the means.

P-value: P-value tells how statistically significant is our calculated score value.

If our price variable is strongly correlated with the variable we are analyzing, expect ANOVA to return a sizeable F-test score and a small p-value.

f_val, p_val = stats.f_oneway(grouped_test2.get_group('fwd')['price'], grouped_test2.get_group('rwd')['price'], grouped_test2.get_group('4wd')['price'])
print( "ANOVA results: F=", f_val, ", P =", p_val) 

>>

ANOVA results: F= 67.95406500780399 , P = 3.3945443577151245e-23