728x90
앞에 설명하는 부분들은 파이썬 설명보다는 통계 설명이 더 많아서 통계 카테고리로 옮겼습니다
이 포스팅에서는 전체 코드를 올리고 마무리 지을까 합니다
당분간은 파이썬보다는 포트란과 NCL로 조금 눈을 돌려볼까 합니다
한동안 너무 파이썬만 한 거 같아요..
과제에서 했던 것들을 간간히 올려보는 걸로 파이썬은 이어가겠습니다!
정말 정말 깁니다...
318줄 정도 되요..
꼼꼼히 읽어보셔야 해요!
import numpy as np
from scipy import stats
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
#Coefficent of Detect
class COD:
def __init__(self, x, y):
self.r, self.p_val = stats.pearsonr(x, y)
self.R = self.r**2
print('R_squared : {}, p-value : {}'.format(round(self.R, 4), round(self.p_val, 4)))
class tvalue1:
def __init__(self, x, y):
self.x1 = np.array(x)
self.y1 = np.array(y)
self.x_mean = np.mean(x)
self.y_mean = np.mean(y)
self.x_bar = self.x1 - self.x_mean
self.y_bar = self.y1 - self.y_mean
self.t = (self.x_mean - self.y_mean)/np.sqrt((np.var(x))/len(x) + np.var(y)/len(y))
print('수학적으로 정의된 T-value는 {}입니다'.format(round(self.t, 4)))
print('수학적으로 정의된 T-value란 집단의 차이를 표준화 시키지 않은 값을 의미합니다')
class tvalue2:
def __init__(self, x, y):
self.x1 = np.array(x)
self.y1 = np.array(y)
self.x_mean1 = np.mean(x)
self.y_mean1 = np.mean(y)
self.x_dev = self.x1 - self.x_mean1
self.y_dev = self.y1 - self.y_mean1
self.r = np.sum(self.x_dev * self.y_dev)/(np.sqrt(np.sum(self.x_dev ** 2)) * np.sqrt(np.sum(self.y_dev ** 2)))
self.t2 = (self.r * np.sqrt(len(x) - 2))/np.sqrt(1 - self.r ** 2)
print('통계적으로 정의된 T-value는 {}입니다'.format(round(self.t2, 4)))
print('통계적으로 정의된 T-value란 집단의 차이를 표준화 시킨 값을 의미합니다')
#5% 유의성 검사
class valence5:
def __init__(self, x, y):
self.r10, self.p10_val = stats.pearsonr(x, y)
if self.p10_val < 0.05:
print('P-value가 충분히 작기 때문에 선형 회귀 모델이 자료에 적합합니다')
else:
print('P-value가 유의 수준 5%보다 크기 때문에 선형 회귀 모델이 자료에 적합하지 않습니다')
#1% 유의성 검사
class valence1:
def __init__(self, x, y):
self.r20, self.p20_val = stats.pearsonr(x, y)
if self.p20_val < 0.01:
print('P-value가 충분히 작기 때문에 선형 회귀 모델이 자료에 적합합니다')
else:
print('P-value가 유의 수준 1%보다 크기 때문에 선형 회귀 모델이 자료에 적합하지 않습니다')
#Correlation
class correlation:
def __init__(self, x, y):
self.p_r, self.pp_val = stats.pearsonr(x, y)
self.s_r, self.sp_val = stats.spearmanr(x, y)
def pearson(self):
print('변수 X,Y에 대한 피어슨 상관계수는 {}입니다'.format(round(self.p_r, 4)))
def spearman(self):
print('변수 X,Y에 대한 스피어만 순위 상관 계수는 {}입니다'.format(round(self.s_r, 4)))
#Fitting Model
class formula:
def __init__(self, x, y):
self.a = np.array(x)
self.reg = LinearRegression()
self.reg.fit(self.a.reshape(-1, 1), y)
self.a_grad = self.reg.coef_
self.a_y = self.reg.intercept_
self.a1_grad = self.a_grad.tolist()
print('Regression coefficient a : {}, b : {}'.format(round(self.a1_grad[0], 4), round(self.a_y, 4)))
print('Regression Formula : {} + {}x'.format(round(self.a_y, 4), round(self.a1_grad[0], 4)))
#Mean Squares Regression
class msr:
def __init__(self, x, y):
self.x1 = np.array(x)
self.x_me = np.mean(x)
self.reg1 = LinearRegression()
self.reg1.fit(self.x1.reshape(-1, 1), y)
self.x1_grad = self.reg1.coef_
self.x_dev = self.x1 - self.x_me
self.x_msr = self.x1_grad**2 * np.sum(self.x_dev ** 2)
self.x_msr1 = self.x_msr.tolist()
print('MSR(Regression Mean Squares) : {}'.format(round(self.x_msr1[0], 4)))
#Mean Squared Error(MSE)
class mse:
def __init__(self, x, y):
self.x2 = np.array(x)
self.y2 = np.array(y)
self.reg2 = LinearRegression()
self.reg2.fit(self.x2.reshape(-1, 1), self.y2)
self.res = self.y2 - self.reg2.predict(self.x2.reshape(-1, 1))
self.x2_mse = np.var(self.res)
print('MSE(Mean Squared Error) : {}'.format(round(self.x2_mse, 4)))
#F - ratio
class Fratio:
def __init__(self, x, y):
self.x3 = np.array(x)
self.y3 = np.array(y)
self.x3_me = np.mean(x)
self.reg3 = LinearRegression()
self.reg3.fit(self.x3.reshape(-1, 1), y)
self.x3_grad = self.reg3.coef_
self.x3_dev = self.x3 - self.x3_me
self.x3_msr = self.x3_grad**2 * np.sum(self.x3_dev ** 2)
self.res1 = self.y3 - self.reg3.predict(self.x3.reshape(-1, 1))
self.x3_mse = np.var(self.res1)
self.F = self.x3_msr/self.x3_mse
self.F1 = self.F.tolist()
print('F - ratio : {}'.format(round(self.F1[0], 4)))
#Rise Rate
class increase:
def __init__(self, x, y, x1, y1):
self.rise_rate1 = ((x - y)/y) * 100
self.rise_rate2 = ((x1 - y1)/y1) * 100
self.rise = self.rise_rate1/self.rise_rate2
print('First Region Rise Rate(%) : {}%'.format(round(self.rise_rate1, 2)))
print('Second Region Rise Rate(%) : {}%'.format(round(self.rise_rate2, 2)))
print('첫번째 입력 지역의 온난화율은 두번째 지역의 온난화율의 {}배 입니다'.format(round(self.rise, 2)))
#Predict Predictor
class predict:
def __init__(self, x, y, x10):
self.x4 = np.array(x)
self.model = LinearRegression()
self.model.fit(self.x4.reshape(-1, 1), y)
self.pred = np.array(x10)
self.forecast = self.model.predict(self.pred.reshape(1, -1))
self.sh = self.forecast.tolist()
print('{}년의 온도 아노말리 예측값은 {}\u00B0C 입니다'.format(x10, round(self.sh[0], 4)))
#Make Figure
class one_fig:
def __init__(self, x, y, v):
self.x20 = np.array(x)
self.model20 = LinearRegression()
self.model20.fit(self.x20.reshape(-1, 1), y)
bum = y >= 0
ab = np.logical_not(bum)
fig, ax = plt.subplots(figsize = (15, 9))
ax.bar(x, y, color = 'maroon', width = 0.8, zorder = 2)
ax.bar(x[ab], y[ab], color = 'steelblue', width = 0.8, zorder = 2)
ax.plot(self.x20, self.model20.predict(self.x20.reshape(-1, 1)), color = 'cyan')
ax.set_title('{} Anomalies'.format(v))
ax.set_xlabel('Date', fontsize = 15)
ax.set_ylabel('Anomalies', fontsize = 15)
ax.grid(zorder = 1)
ax.xaxis.grid(False)
class multi_fig:
def __init__(self, x, y, v):
self.x100 = np.array(x)
self.date50 = x.sort_values(ascending = False)
self.date50_1 = self.date50[:50].sort_values(ascending = True)
self.y50 = y.sort_values(ascending = False)
self.y50_1 = self.y50[:50].sort_values(ascending = True)
self.x50 = np.array(self.date50_1)
self.date30 = x.sort_values(ascending = False)
self.date30_1 = self.date30[:30].sort_values(ascending = True)
self.y30 = y.sort_values(ascending = False)
self.y30_1 = self.y30[:30].sort_values(ascending = True)
self.x30 = np.array(self.date30_1)
self.date10 = x.sort_values(ascending = False)
self.date10_1 = self.date10[:10].sort_values(ascending = True)
self.y10 = y.sort_values(ascending = False)
self.y10_1 = self.y10[:10].sort_values(ascending = True)
self.x10 = np.array(self.date10_1)
self.model30 = LinearRegression()
self.model40 = LinearRegression()
self.model50 = LinearRegression()
self.model60 = LinearRegression()
self.model30.fit(self.x100.reshape(-1, 1), y)
self.model40.fit(self.x50.reshape(-1, 1), self.y50_1)
self.model50.fit(self.x30.reshape(-1, 1), self.y30_1)
self.model60.fit(self.x10.reshape(-1, 1), self.y10_1)
bum = y >= 0
ab = np.logical_not(bum)
fig, ax = plt.subplots(figsize = (15, 9))
ax.bar(x, y, color = 'maroon', width = 0.8, zorder = 2)
ax.bar(x[ab], y[ab], color = 'steelblue', width = 0.8, zorder = 2)
ax.plot(self.x100, self.model30.predict(self.x100.reshape(-1, 1)), color = 'cyan', label = 'All times', lw = 3)
ax.plot(self.x50, self.model40.predict(self.x50.reshape(-1, 1)), color = 'mediumaquamarine', label = 'Last 50 Years', lw = 3)
ax.plot(self.x30, self.model50.predict(self.x30.reshape(-1, 1)), color = 'yellow', label = 'Last 30 Years', lw = 3)
ax.plot(self.x10, self.model60.predict(self.x10.reshape(-1, 1)), color = 'plum', label = 'Last 10 Years', lw = 3)
ax.set_title('{} Anomalies'.format(v))
ax.set_xlabel('Date', fontsize = 15)
ax.set_ylabel('Anomalies', fontsize = 15)
ax.grid(zorder = 1)
ax.xaxis.grid(False)
ax.legend(loc = 'upper left', prop = {'size' : 15})
class sa:
def __init__(self, x, y):
self.x1 = np.array(x)
self.y1 = np.array(y)
self.x_me = np.mean(x)
self.y_me = np.mean(y)
self.x_dev10 = self.x1 - self.x_me
self.reg10 = LinearRegression()
self.reg10.fit(self.x1.reshape(-1, 1), y)
self.yh = self.reg10.predict(self.x1.reshape(-1, 1))
self.a10 = self.reg10.coef_
self.Sa = np.std(self.yh) * ((np.sum(self.x1 ** 2)/(len(x) * np.sum(self.x_dev10 ** 2)))
print('$S_{a} : {}$'.format(self.Sa))
def ta(self):
print('$t_{a}$ : {}'.format(self.a10 / self.Sa))
class sb:
def __init__(self, x, y):
self.x1 = np.array(x)
self.y1 = np.array(y)
self.x_me = np.mean(x)
self.y_me = np.mean(y)
self.x_dev10 = self.x1 - self.x_me
self.reg10 = LinearRegression()
self.reg10.fit(self.x1.reshape(-1, 1), y)
self.yh = self.reg10.predict(self.x1.reshape(-1, 1))
self.b10 = self.reg10.intercept_
self.Sb = np.std(self.yh) / np.sqrt(np.sum(x_dev10) ** 2)
print('$S_{b}$ : {}'.format(self.Sb))
def (self):
print('$t_{a}$ : {}'.format(self.b10 / self.Sb))
코드 사용법이나 class 정의, def랑 __init__의 사용법은 첫 번째 포스팅에 적혀있습니다
보고 따라해보시면 좋을 것 같아요!
긴 글 읽어주셔서 감사합니다!
728x90
'파이썬' 카테고리의 다른 글
작업17: Python으로 EBM 기후모델 만들기2 (0) | 2023.01.06 |
---|---|
작업16: Python으로 EBM 기후모델 만들기1 (2) | 2023.01.04 |
작업 14: Python으로 선형 회귀 모델 적합성 검사 라이브러리 만들기1 (0) | 2022.12.06 |
작업13: Python으로 부산 평균 최고기온 분석하기 4 (0) | 2022.12.04 |
작업12: Python으로 부산 평균 최고기온 분석하기3 (0) | 2022.11.05 |