Google Colab筆記


 

 

1.Google Colab掛載Google Drive並運行程序

from google.colab import drive
drive.mount('/content/drive/')

https://blog.csdn.net/weixin_43792757/article/details/84523103

 

2.利用kaggle的API講數據集直接下載到Google Colab

登錄kaggle在My Account中創建自己的Token:

{"username":"abc","key":"123"}

!pip install -U -q kaggle
!mkdir -p ~/.kaggle
!echo '{"username":"abc","key":"123"}' > ~/.kaggle/kaggle.json
!chmod 600 ~/.kaggle/kaggle.json
#需要注意的是,你必須在kaggle上參加相應的比賽才能下載對應的數據!!!
!kaggle competitions download -c digit-recognizer

https://blog.csdn.net/qq_35654046/article/details/87621396

https://medium.com/@move37timm/using-kaggle-api-for-google-colaboratory-d18645f93648

https://github.com/Kaggle/kaggle-api

 

3.解決google colab環境下使用matplotlib繪圖中文亂碼問題

從網上下載一個支持中文的字體到系統字體目錄:

!wget -O /usr/share/fonts/truetype/liberation/simhei.ttf "http://file3.data.weipan.cn/87016829/e6a4de46faf0c7ba622132eaaad96a291d62f045?ip=1546784382,2409:8a20:203b:550:dbc:11e3:2ef9:cee9&ssig=OSXPXG8otr&Expires=1546784982&KID=sae,l30zoo1wmz&fn=simhei.ttf&se_ip_debug=2409:8a20:203b:550:dbc:11e3:2ef9:cee9&from=1221134"

引入下載的中文字體

import matplotlib.pyplot as plt
import matplotlib as mpl
zhfont = mpl.font_manager.FontProperties(fname='/usr/share/fonts/truetype/liberation/simhei.ttf')
plt.rcParams['axes.unicode_minus'] = False  # 用來正常顯示負號

畫坐標及坐標軸文本的時候引用

df_Gender=df.groupby("Gender").Purchase.sum().reset_index()
df_Gender=df_Gender.apply(lambda x:x[1]/100000000,axis=1).reset_index(drop=True).reset_index().rename({0:"Purchase"},axis=1)
fig,ax=plt.subplots(figsize=(6,6))
rects=plt.bar(('F','M'),df_Gender.Purchase)
ax.set_xlabel("性別",fontproperties=zhfont)
ax.set_ylabel('億元',fontproperties=zhfont)
ax.set_ylim((0,45))
ax.set_title("不同性別的總消費額",fontproperties=zhfont)
for rect in rects:
    height = rect.get_height()
    plt.text(rect.get_x() + rect.get_width() / 2, height+0.2, '%.0f'%height, ha='center', va='bottom',fontsize=12,)
plt.show()

效果如下:

參考資料:解決google colab環境下使用matplotlib繪圖中文亂碼問題

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM