头哥数据特征分析

第一关

import pandas as pd

import matplotlib.pyplot as plt

import seaborn as sns

sns.set(style='darkgrid')

plt.tight_layout()

df = pd.read_csv("src/step1/data.csv") # 读入数据

def student():

    ########## Begin #########  

    fig, ax = plt.subplots(1, 5, figsize=(12, 3))  

      

    ax[0].set_title("happiness=1")   

    df[df['happiness'] == 1]['county'].hist(bins=10, ax=ax[0])  

      

    ax[1].set_title("happiness=2")  

    df[df['happiness'] == 2]['county'].hist(bins=10, ax=ax[1])  

      

    ax[2].set_title("happiness=3")  

    df[df['happiness'] == 3]['county'].hist(bins=10, ax=ax[2])  

      

    ax[3].set_title("happiness=4")  

    df[df['happiness'] == 4]['county'].hist(bins=10, ax=ax[3])  

      

    ax[4].set_title("happiness=5")  

    df[df['happiness'] == 5]['county'].hist(bins=10, ax=ax[4])  

      

    # 确保所有子图布局紧凑  

    #plt.tight_layout()  

      

    # 保存图形  

    fig.savefig('photo/step1/stu_ph/stu.jpg')  

    ########## End #########  

  

# 调用函数  

student()

 

第四关

import pandas as pd

import matplotlib

import matplotlib.pyplot as plt

from statsmodels.tsa.seasonal import seasonal_decompose

def student():

    data = pd.read_csv("src/step4/data.csv", index_col='Month')

    data.index = pd.date_range(start=data.index[0], periods=len(data.index), freq='M')

    ########## Begin #########

    decomposition = seasonal_decompose(data) # data:需要解析的数据

    trend = decomposition.trend # 趋势

    seasonal = decomposition.seasonal # 周期

    residual = decomposition.resid # 残差

    plt.subplot(4, 1, 1)

    plt.plot(data, linestyle='-', color='red', alpha=0.8)

    plt.title('Source data')

    plt.subplot(4, 1, 2)

    plt.plot(trend, linestyle='-', color='red', alpha=0.8)

    plt.title('Trend info')

    plt.subplot(4, 1, 3)

    plt.plot(seasonal, linestyle='-', color='red', alpha=0.8)

    plt.title('Seasonal info')

    plt.subplot(4, 1, 4)

    plt.plot(residual, linestyle='-', color='red', alpha=0.8)

    plt.title('Residual info')

########## End #########

    plt.tight_layout()

    plt.savefig("photo/step4/stu_ph/stu.jpg")

    

    

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值