2020第三屆中青杯問題總結


這篇文章是寫給我自己看的,裏面有很多技巧,當然大家也可以學習一下。

論文上

我是一個人參賽,只靠自己。論文用的 Latex。

如何雙欄排版?

在需要雙欄排版的地方,加入如下語句:

\begin{multicols}{2}

\end{multicols}

排出來效果是這樣的:

公式換行對齊

需要對齊的長公式可以用split 環境,它本身不能單獨使用,因此也稱作次環境,必須包含在equation 或其它數學環境內。split 環境用 \ 和& 來分行和設置對齊位置。

\begin{equation}

\begin{split}

x=&a+b+c+\\
&d+e+f+g

\end{split}
\end{equation}

怎麼設置行距

\renewcommand{\baselinestretch}{1.25}

編程

Pandas 讀取 Excel 文件的 Sheet

如圖:
在這裏插入圖片描述
如何將十隻股票的數據全部讀進 Python 中呢?

X0 = pd.read_excel(r'../附件/附件:十支股票參數.xlsx',0)
X1 = pd.read_excel(r'../附件/附件:十支股票參數.xlsx',1)
X2 = pd.read_excel(r'../附件/附件:十支股票參數.xlsx',2)
。。。
X9 = pd.read_excel(r'../附件/附件:十支股票參數.xlsx',9)

怎麼生成一個2019年1月1日,到2020年3月26日的時間序列呢?

(用於生成一個空表格,在填進去,然後找出缺失數據)定義一個類,再導入文件使用接口。

# -*- coding: utf-8 -*-
"""
Created on Fri May 29 21:07:59 2020

@author: Administrator
"""

import re
import calendar
import datetime


class FormatError(ValueError):
    pass


class Date(object):
    @classmethod
    def date_range(cls, start=None, end=None, periods=None, freq=None, input_format=None, out_format=None):
        """
        生成時間序列
        :param start: 序列開始時間
        :param end: 序列結束時間, 給定start時, 結束時間不包含end
        :param periods: int, 生成的時間序列長度
        :param freq: 要生成時間序列的時間間隔
        :param out_format: 是否輸出格式化後的字符串, 若要輸出可指定輸出格式. "%Y-%m-%d %H:%M:%S"
        :param input_format: 若start或end是字符串且無法自動推斷時間格式則需指定格式
        :return: [date or date_str]
        """
        start = cls.str_to_date(start, input_format)
        end = cls.str_to_date(end, input_format)
        out = []
        if start is None and end and periods:
            for i in range(periods-1):
                old, end = cls.date_replace(end, cls._freq(freq), mod="-")
                if i == 0:
                    out.append(old)
                out = [end] + out
        elif end is None and start and periods:
            for i in range(periods-1):
                old, start = cls.date_replace(start, cls._freq(freq), mod="+")
                if i == 0:
                    out.append(old)
                out.append(start)
        elif periods is None and start and end:
            i = 0
            while True:
                old, start = cls.date_replace(start, cls._freq(freq), mod="+")
                if i == 0:
                    out.append(old)
                    i += 1
                if start < end:
                    out.append(start)
                else:
                    break
        else:
            raise ValueError("start, end, periods 須且只能指定其中兩個")
        if out_format is True:
            out = [str(i) for i in out]
        elif out_format is not None:
            out = [i.strftime(out_format) for i in out]
        return out

    @staticmethod
    def date_replace(date, freq=(0, )*6, mod="+"):
        timedelta = datetime.timedelta(days=freq[2], hours=freq[3], minutes=freq[4], seconds=freq[5])
        if mod == "+":
            if sum(freq[:2]) == 0:
                old = date
                date = date + timedelta
            elif sum(freq[2:]) == 0:
                y = date.year + freq[0] + (date.month + freq[1] - 1) // 12
                m = (date.month + freq[1] - 1) % 12 + 1
                old = date.replace(day=calendar.monthrange(date.year, date.month)[1])
                date = date.replace(year=y, month=m, day=calendar.monthrange(y, m)[1])
            else:
                raise ValueError(" '年月' 不能同時和 '日時分秒' 作爲間隔")
        elif mod == "-":
            if sum(freq[:2]) == 0:
                old = date
                date = date - timedelta
            elif sum(freq[2:]) == 0:
                y = date.year - freq[0] + (date.month - freq[1] - 1) // 12
                m = (date.month - freq[1] - 1) % 12 + 1
                old = date.replace(day=calendar.monthrange(date.year, date.month)[1])
                date = date.replace(year=y, month=m, day=calendar.monthrange(y, m)[1])
            else:
                raise ValueError(" '年月' 不能同時和 '日時分秒' 作爲間隔")
        else:
            raise ValueError("mod值只能是 '+' 或 '-' ")
        return old, date

    @staticmethod
    def _freq(freq=None):
        """
        設置時間間隔
        :param freq: "Y2m3d4H5M6S" 表示間隔 1年2月3日4時5分6秒
        :return: [年, 月, 日, 時, 分, 秒]
        """
        freq_ = [0] * 6
        if freq is None:
            freq_[2] = 1
            return freq_
        for n, i in enumerate(["Y", "m", "d", "H", "M", "S"]):
            r = f'((\d*){i})'
            s = re.search(r, freq)
            if s:
                freq_[n] = int(s.group(2)) if s.group(2) else 1
        return freq_

    @staticmethod
    def str_to_date(string, format_=None):
        """
        字符串轉時間, 默認自動推斷格式
        :param string: 時間字符串
        :param format_: 格式
        :return: 對應的時間類型, 輸入非字符串則原值輸出
        """
        if not isinstance(string, str):
            return string
        if format_:
            return datetime.datetime.strptime(string, format_)
        s = re.match(r'(\d{4})\D+(\d{1,2})\D+(\d{1,2})(?:\D+(\d{1,2}))?(?:\D+(\d{1,2}))?(?:\D+(\d{1,2}))?\D*$', string)
        if s:
            result = [int(i) for i in s.groups() if i]
            return datetime.datetime(*result)
        s = re.match(r'(\d{4})\D*(\d{2})\D*(\d{2})\D*(\d{2})?\D*(\d{2})?\D*(\d{2})?\D*$', string)
        if s:
            result = [int(i) for i in s.groups() if i]
            return datetime.datetime(*result)
        else:
            raise FormatError("自動推斷失敗, 請指定format_")

導入文件使用:

from Daterange import Date   #Daterange是我取的 py 文件名,下面是使用方法
print(Date.date_range(datetime.datetime(2018, 9, 18), periods=10))
print()
print(Date.date_range('20180918', '2018-09-28'))
print()
print(Date.date_range(end='20180927', periods=10))
print()
print(Date.date_range('20180918', '2018-09-28', out_format=True))
print()
print(Date.date_range('2018/09/18', '2018-09-28', out_format="%Y-%m-%d"))
print()
print(Date.date_range('2018年9月18日', '2019-09-28', freq="m", out_format="%Y-%m-%d"))
print()
print(Date.date_range('2018/9/18', '2018-9-19', freq="3H", out_format=True))

DataFrame 左連接

df1:
   co12 col1
0     1    a
1     2    b

df2:
   co13 col1
0    11    a
1    33    c

print pd.merge(left=df1, right=df2, how='inner', left_on='col1', right_on='col1')
----------
   co12 col1  co13
0     1    a    11

print pd.merge(left=df1, right=df2, how='left', left_on='col1', right_on='col1')
----------
   co12 col1  co13
0     1    a    11
1     2    b   NaN

print pd.merge(left=df1, right=df2, how='left', left_on='col1', right_on='col1')
----------
   co12 col1  co13
0     1    a    11
1   NaN    c    33

如何將多個dataframe保存到同一個excel中的不同sheet裏

writer = pd.ExcelWriter('result.xlsx')
for i in X:
       i.to_excel(excel_writer=writer,sheet_name=i,index=False)
writer.save()
writer.close()

X爲多個 dataframe 構成的 python 列表

用 Matlab 讀取 Excel 表格

我想用 Matlab 裏的 cftool 來填補缺失值。然後,只要將模型輸出,就可以直接拿來使用。(點擊 Save to workspace)
在這裏插入圖片描述
之後,只要在 workspace 裏面,輸出 y = fittedmodel2(x) 就可以了。

用Matlab找出 NaN 所在的行

[m n] = find(isnan(X))

劍蕩八荒式畫圖(Python)

plt.rcParams['font.sans-serif']=['SimHei']    #畫圖時顯示中文字體
plt.rcParams['axes.unicode_minus'] = False

for i, ax in enumerate(axes.flat):
    ax.hist(X[i].iloc[:,-1])
    ax.set_xlabel('柱狀圖',fontsize=14)
 

Dickey-Fuller 檢驗

它可以用於驗證 時序數據是否穩定,具體可點擊鏈接:https://www.docin.com/p-1628484711.html,用法如下:

from statsmodels.tsa.stattools import adfuller
result = adfuller(X[1].iloc[:,-1])
print('ADF Statistic: %f' % result[0])
print('p-value: %f' % result[1])
print('Critical Values:')
for key, value in result[4].items():
 	print('\t%s: %.3f' % (key, value))

df 怎麼把第一列設置爲索引

for i in range(10):
    X.append(pd.read_excel(r'../附件/填充後數據.xlsx',i,index_col = '時間'))

直接在讀取的時候用index_col聲明即可。

df 按時間分組

首先要把時間設置爲索引,具體方法見上。之後,用 pandas 的 Grouper 模塊即可,如下所示:

    tmp = X[i].iloc[:,-1]   # tmp 爲 Series(主要是我要畫箱子圖)
    months 
    groups = tmp.groupby(pd.Grouper(freq='30d'))    #groupby 接口
    for name,group in groups:    #groups 是一個亂七八糟的數據結構,我們要做的是遍歷他,使之可用。 這裏的 group 就是一個 Series 了。
        months[name.month] = group.values
    months.boxplot()

Python 畫箱型圖

箱型圖能夠反映數據部分總體,或者局部的變化。如下所示
在這裏插入圖片描述
雖然能夠用 Pandas 直接畫出箱型圖,但是卻不能和 plt 聯動,因此對於畫多個箱型圖來說,比較不方便。那麼,如何用plt 來畫箱型圖呢?

plt.boxplot(x=df.values,labels=df.columns,whis=1.5)
plt.show()

ValueError: Length of values does not match length of index

fig, axes = plt.subplots(10,1, figsize=(8, 6),subplot_kw={'xticks':[], 'yticks':[]},
                        gridspec_kw=dict(hspace=0.05, wspace=0.1))
for i, ax in enumerate(axes.flat):
    tmp = X[i].iloc[:,-1]
    groups = tmp.groupby(pd.Grouper(freq='30d'))
    month = pd.DataFrame()
    for name,group in groups:
        try:
            month[str(name.year)+str(name.month)] = group.values   #這裏由於有“剩餘”,所以導致長度不匹配,故需要另外處理,如下:
        except:
            l = len(group.values)
            month[str(name.year)+str(name.month)] = np.nan   #先給他賦值爲 nan,再用數據替代。
            month[str(name.year)+str(name.month)].iloc[0:l] = group.values

    ax.boxplot(x=month.values,labels=month.columns,whis=1.5)
    ax.set_xlabel('日期序列',fontsize=14)

怎麼創建特定長度的,值爲 nan 的 array

month[str(name.year)+str(name.month)] = np.array([np.nan]*31)

劍蕩八荒式畫圖——2

fig = plt.figure(figsize=(9,12))
for i in range(10):
    ax = fig.add_subplot(10,1,i+1)
    tmp = X[i].iloc[:,-1]
    plot_acf(tmp,lags=30,ax=ax,title='')

怎麼把 df index 設置爲第一列——2

df.index = df.iloc[:,1]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章