<jupyter_start><jupyter_text>说明：第2至4行：利用for循环绘制不同自由度下的F分布的分布曲线<jupyter_code>plt.figure(figsize=(6,4))
for df,lt in [(2,'--'),(4,'-.'),(6,':'),(20,'-')]:
    x = np.linspace(st.chi.ppf(0.001, df),st.chi.ppf(0.999, df), 100)
    plt.plot(x, st.chi.pdf(x, df),label='df=%d'%df,linestyle=lt)
plt.legend()
plt.title("不同自由度下的卡方分布",fontdict={'fontsize':12})
plt.xlabel("X",fontdict={'fontsize':12})
plt.ylabel("概率密度",fontdict={'fontsize':12})<jupyter_output><empty_output><jupyter_text>说明：第2至4行：利用for循环绘制不同自由度下的卡方分布的分布曲线<jupyter_code>data=pd.read_table('邮政编码数据.txt',sep=' ',header=None)
tmp=data.loc[(data[0]==1) | (data[0]==3)]
X=tmp.iloc[:,1:-1]
Y=tmp.iloc[:,0]
np.random.seed(1)
ids=np.random.choice(len(Y),25)
plt.figure(figsize=(8,8))
for i,item in enumerate(ids):
    img=np.array(X.iloc[item,]).reshape((16,16))
    plt.subplot(5,5,i+1)
    plt.imshow(img,cmap=cm.gray)
plt.show()

selector=SelectKBest(score_func=f_classif,k=100)  
selector.fit(X,Y)
print("变量重要性评分：",selector.scores_[0:5])
print("变量的概率P-值:",selector.pvalues_[0:5])
X=selector.inverse_transform(selector.transform(X))
plt.figure(figsize=(8,8))
for i,item in enumerate(ids):
    img=np.array(X[item,]).reshape((16,16))
    plt.subplot(5,5,i+1)
    plt.imshow(img,cmap=cm.gray)
plt.show()<jupyter_output>变量重要性评分： [  5.10898495  21.93357405  82.3841514  282.84509937 719.07806468]
变量的概率P-值: [2.39312394e-002 3.05218712e-006 3.08578340e-019 9.82205018e-059
 6.44143983e-132]
{'fraction_non_alphanumeric': 0.22964509394572025, 'fraction_numerical': 0.12038970076548365, 'mean_word_length': 7.559523809523809, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '177145', 'license': 'MIT', 'n_tokens_mistral': 828, 'n_tokens_neox': 768, 'n_words': 58, 'path': 'chapter9-2.ipynb', 'repo_name': 'cutelittletiantian/python-ML-principles-and-practice', 'size': '1437'}