<jupyter_start><jupyter_text>第4章 情報量基準  4.1 情報量規準<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import japanize_matplotlib
import scipy
from scipy import stats
from numpy.random import randn
from sklearn.linear_model import LinearRegression
import itertools  # 組合わせを列挙する
res = LinearRegression()
def RSS_min(X,y,T):
    S_min=np.inf
    m=len(T)
    for j in range(m):
        q=T[j]
        res.fit(X[:,q],y)
        y_hat=res.predict(X[:,q])
        S=np.linalg.norm(y_hat-y)**2
        if S<S_min:
            S_min=S
            set_q=q
    return(S_min, set_q)
from sklearn.datasets import load_boston
boston = load_boston()
X=boston.data[:,[0,2,4,5,6,7,9,10,11,12]]
y=boston.target
n,p=X.shape
AIC_min=np.inf
for k in range(1,p+1,1):
    T=list(itertools.combinations(range(p), k))
    # p個からk個を選ぶ組合わせを各列にもつ
    S_min, set_q=RSS_min(X,y,T)
    AIC=n*np.log(S_min)+2*k  ##
    if AIC<AIC_min:
        AIC_min=AIC
        set_min=set_q
print(AIC_min,set_min)
y_bar=np.mean(y)
TSS=np.linalg.norm(y-y_bar)**2
D_max=-np.inf
for k in range(1,p+1,1):
    T=list(itertools.combinations(range(p), k))
    S_min, set_q=RSS_min(X,y,T)
    D=1-(S_min/(n-p-1))/(TSS*(n-1))
    if D>D_max:
        D_max=D
        set_max=set_q
print(D_max,set_q)
def IC(X,y,k):
    n,p=X.shape
    T=list(itertools.combinations(range(p), k))
    S, set_q=RSS_min(X,y,T)
    AIC=n*np.log(S)+2*k
    BIC=n*np.log(S)+k*np.log(n)
    return {'AIC':AIC, 'BIC':BIC}
AIC_seq=[]; BIC_seq=[]
for k in range(1,p+1,1):
    AIC_seq.append(IC(X,y,k)['AIC'])
    BIC_seq.append(IC(X,y,k)['BIC']) 
x_seq=np.arange(1,p+1,1)
plt.plot(x_seq,AIC_seq,c="red",label="AIC")
plt.plot(x_seq,BIC_seq,c="blue",label="BIC")
plt.xlabel("変数の個数")
plt.ylabel("AIC/BICの値")
plt.title("変数の個数とAICとBICの値の変化")
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17778993435448578, 'fraction_numerical': 0.019146608315098467, 'mean_word_length': 4.152112676056338, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '333908', 'license': 'MIT', 'n_tokens_mistral': 943, 'n_tokens_neox': 938, 'n_words': 121, 'path': '2-4.ipynb', 'repo_name': 'hase8388/statistical_learning_with_python', 'size': '1828'}