Chapter 1 萬物皆數(shù) | 《數(shù)學(xué)要素》 | 從加減乘除到機(jī)器學(xué)習(xí)

#初學(xué)python,如有錯誤或感覺繁瑣,請各位指正
#圓周率小數(shù)點(diǎn)后1024位熱圖及包含0-9數(shù)字個數(shù)
from mpmath import mp
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
#取pi的1024位小數(shù)
mp.dps = 1025??
pi = mp.pi
#print(pi)
# 轉(zhuǎn)換為字符串
pi = str(pi)
# 去除小數(shù)點(diǎn)
pi = pi.replace('.', '')
# 取后1024位??
pi_1024 = pi[1:]
#print(pi_1024)
#把整數(shù)分割為數(shù)組
pi_1024L = list(pi_1024)
#print(pi_1024L)
#字符串?dāng)?shù)組轉(zhuǎn)換為數(shù)字?jǐn)?shù)組
pi_1024N = [ int(x) for x in pi_1024L ]
#print(pi_1024N)
#一維數(shù)組轉(zhuǎn)多維數(shù)組
array = np.array(pi_1024N)
pi_array = array.reshape(32,32)
#print(pi_array)
#繪制熱圖
sns.set(rc = {'figure.figsize':(6,5)})
sns.heatmap(pi_array, cmap='RdYlBu_r', xticklabels = False, yticklabels = False)
plt.show()
#統(tǒng)計0-9數(shù)字個數(shù)
result = pd.value_counts(pi_1024N)
print (result)
熱圖結(jié)果:

統(tǒng)計結(jié)果:
