내 인생의 멤피스에서 뜨겁든 아니든 (python,sqlite)
https://creon.substack.com/p/hot-or-not
그리고 파이썬과 sqlite를 사용하여 직접 복제하기로 결정했습니다. 저는 60년대에 태어났기 때문에 제 개인적인 진술은 제가 어렸을 때보다 지금이 더 덥다고 확신합니다.
그래서 여기로 자료를 구하러 갔습니다https://mesonet.agron.iastate.edu/request/download.phtml?network=TN_ASOS
그리고 내 근처의 TN에 대한 데이터를 다운로드했습니다. 주로 기온을 봅니다.
그로부터 sqlite 데이터베이스를 만들고 CSV를 로드했습니다.
CREATE TABLE data (
station varchar,
dt datetime,
tmpf real,
dewf real,
relh real
)
.mode csv
.import asos.csv data
내 인생을 더 쉽게 만들기 위해 연도와 월에 대한 몇 가지 새로운 열을 만들었습니다(날짜를 나누기 위해).
그런 다음 매일 MAX 온도를 측정했습니다. 확인하려는 진술은 지금이 더 덥다고 생각하므로 MAX 온도를 사용하는 것이 좋은 것 같습니다. 매일 MAX 온도를 측정하는 또 다른 이유는 내가 사용하는 데이터가 하루에 여러 번 측정되기 때문에 하루에 MAX 온도 하나만 필요하기 때문입니다.
나는 주로 내 삶을 더 쉽게 만들기 위해 두 번째 테이블을 만들었습니다.
create table mem_daily_max as select y,m,d,max(tmpf),max(dewf),max(relh) from data where station='MEM' group by y,m,d
그런 다음 1990년부터 매월 온도를 평균화하여 플롯했습니다. 다른 달은 여름 달이 덜 변화하는 것처럼 다른 추세를 보여줍니다. 여름의 멤피스는 항상 그래왔고 앞으로도 그럴 것입니다.
겨울 달을 보면 추세가 분명합니다.
1960년부터 현재까지의 12월 원시 데이터

그것에서 많은 것을 보는 것은 상당히 어렵습니다. 파이썬 코드는 다음과 같습니다
import sqlite3
sql ="""
SELECT y,m,d,tmpf FROM
mem_daily_max WHERE m=12 AND y>1960
ORDER BY y,m,d ASC
"""
db = None
cur = None
try:
db = sqlite3.connect("data.db")
cur = db.cursor()
cur.execute(sql)
data = cur.fetchall()
finally:
if cur is not None:
cur.close()
if db is not None:
db.close()
pd = []
for (y,m,d,tmpf) in data:
print(y,m,d,tmpf)
pd.append(tmpf)
import matplotlib.pyplot as plt
plt.plot(pd)
plt.xlabel("max temp")
plt.savefig("foo.png")
그래서 평활화하기 위해 매월 평균을 취했고 아래 차트는 12월만을 보여줍니다.
선형 회귀선이 있는 12월 평균입니다. 공기 온도가 적어도 내 평생 동안 12월에 올라가고 있다는 것은 상당히 분명합니다.

import sqlite3
sql ="""
SELECT y,m,avg(tmpf) as tmp FROM mem_daily_max
WHERE m=12 GROUP BY y,m ORDER BY y,m asc;
"""
# just used some code I found online for this function
def estimate_coef(x, y):
# number of observations/points
n = np.size(x)
# mean of x and y vector
m_x = np.mean(x)
m_y = np.mean(y)
# calculating cross-deviation and deviation about x
SS_xy = np.sum(y*x) - n*m_y*m_x
SS_xx = np.sum(x*x) - n*m_x*m_x
# calculating regression coefficients
b_1 = SS_xy / SS_xx
b_0 = m_y - b_1*m_x
return (b_0, b_1)
# just used some code I found online for this function
def plot_regression_line(x, y, b):
# plotting the actual points as scatter plot
plt.scatter(x, y, color = "m",
marker = "o", s = 30)
# predicted response vector
y_pred = b[0] + b[1]*x
# plotting the regression line
plt.plot(x, y_pred, color = "g")
# putting labels
plt.xlabel('x')
plt.ylabel('y')
# function to show plot
plt.savefig("goo.png")
db = None
cur = None
try:
db = sqlite3.connect("data.db")
cur = db.cursor()
cur.execute(sql)
data = cur.fetchall()
finally:
if cur is not None:
cur.close()
if db is not None:
db.close()
pd = []
for (y,m,tmp,) in data:
pd.append(tmp)
# just plot the data
import matplotlib.pyplot as plt
plt.plot(pd)
plt.xlabel("max temp")
plt.savefig("foo.png")
# now run the regression
import numpy as np
x = np.array([i for i in range(len(pd))])
y = pd
b = estimate_coef(x,y)
print("Estimated coefficients:\nb_0 = {} \
\nb_1 = {}".format(b[0], b[1]))
plot_regression_line(x,y,b)
Reference
이 문제에 관하여(내 인생의 멤피스에서 뜨겁든 아니든 (python,sqlite)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/tonetheman/hot-or-not-in-memphis-in-my-life-pythonsqlite-6ae텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)