天天看點

jupyter、matplotlib、Linux常見問題和設定記錄

常見問題和設定記錄

1.jupyterlab問題

1.1 jupyter-lab修改工作目錄

1.2 jupyter-lab 多行輸出(單個cell)

2.matplotlib常用設定問題

​ 2.1 matplotlib 作圖中文顯示和負号顯示亂碼問題

​ 2.2 matplotlib 工作中常用繪圖,及其常用設定(坐标軸(名稱、刻度、顯示)、水準線、子圖(間距)、标題(子圖示題、總标題)、畫布大小,3D圖、動畫等)

3.linux工作中常用指令記錄

4.待定

  1. jupyter問題
    1. jupyter-lab修改工作目錄
      指令行下輸入>>:jupyter-lab  --generate-config
      
      預設在” C:\Users\使用者名\.jupyter“自動生成"jupyter_lab_config.py"配置檔案
      
      修改配置檔案root_dir即可切換工作目錄
                 
      jupyter、matplotlib、Linux常見問題和設定記錄
    2. jupyter-lab 多行輸出(單個cell)
      在電腦使用者路徑下”C:\Users\使用者名\.ipython\profile_default”
      建立ipython_config.py配置檔案
      寫入
      c = get_config()
      c.InteractiveShell.ast_node_interactivity = 'all'
      重新開機juputer-lab即可多行顯示
                 
    jupyter、matplotlib、Linux常見問題和設定記錄
    3. jupyter-lab指定預設浏覽器
    # 在配置檔案中添加配置
     import webbrowser
     webbrowser.register("chrome", None,webbrowser.GenericBrowser(r'C:\Users\26305\AppData\Local\Google\Chrome\Application\chrome.exe'))
     c.ServerApp.browser = 'chrome'
    
               
  2. matplotlib常用設定問題
    1. matplotlib 作圖中文顯示和負号顯示亂碼問題
      • 拷貝字型檔案到mat庫指定路徑下

        win系統下從路徑’C:\Windows\Fonts’下拷貝字型檔案(黑體就行)

        jupyter、matplotlib、Linux常見問題和設定記錄
        将拷貝字型檔案放到“xxx\python3.9\Lib\site-packages\matplotlib\mpl-data\fonts\ttf”下
        jupyter、matplotlib、Linux常見問題和設定記錄
      • 修改mat配置檔案(重新啟動python核心)
      ​ 在路徑“ xxx\python3.9\Lib\site-packages\matplotlib\mpl-data”下打開matplotlibrc檔案(可以使用NodePad++)
      1. 解開’font.family’注釋,使用 sans-serif 系列字型
      2. 解開"font.sans-serif"注釋,在 sans-serif 系列字型前加上 “simhei”(或其它放入’xx/ttf/'路徑内的字型)
      3. 解開"axes.unicode_minus"注釋,并設定“axes.unicode_minus:False ”
      jupyter、matplotlib、Linux常見問題和設定記錄
    2. matplotlib 工作中常用繪圖,及其常用設定(坐标軸(名稱、刻度、顯示)、水準線、子圖(間距)、标題(子圖示題、總标題)、畫布大小,3D圖、動畫等)
      1. 雙系列柱狀圖(用于系列對比/趨勢變化)
        import matplotlib.pyplot as plt
        from mpl_toolkits.mplot3d import Axes3D
        from matplotlib import animation
        
        from sklearn.datasets import make_biclusters
        from sklearn.cluster import KMeans
        from sklearn.metrics import silhouette_samples
        from sklearn.metrics import silhouette_score
        
        import numpy as np
        import pandas as pd
        import random
        
        # 1.雙系列柱狀圖(用于系列對比/趨勢變化)
        x = ['測試1','測試2','xxxx3']
        y1 = [1,2,3]
        y2 = [4,5,6]
        
        plt.figure(figsize=(8,4))
        plt.bar(x=x,height=y1,width=-0.4,align='edge',label='y1') # 調整柱子位置
        plt.bar(x=x,height=y2,width=0.4,align='edge',label='y2')
        # 設定刻度标簽名
        plt.yticks(ticks=[0,1,2,3,4,5,6],labels=['刻度0','刻度1','刻度2','刻度3','刻度4','刻度5','刻度6'])
        plt.xticks(rotation=45) # 調整x軸刻度标簽轉向
        plt.legend()
                   
        jupyter、matplotlib、Linux常見問題和設定記錄
      2. 三維圖繪制(展示資料空間分布,可用于PCA降維後檢視資料分布)
        # 2.三維圖繪制(展示資料空間分布,可用于PCA降維後檢視資料分布)
        plt.figure(figsize=(12,5))
        
        a = np.linspace(-5,5,num=110)
        b = np.linspace(-5,5,num=110)
        x,y = np.meshgrid(a,b)
        L1 = np.abs(x)+np.abs(y)
        L2 = np.sqrt(np.square(x)+np.square(y))
        
        # 設定子圖1
        ax = plt.subplot(1,2,1,projection='3d')
        ax.plot_surface(x,y,L1,cmap='Reds') # 繪制表面
        ax.plot_surface(x,y,L2,cmap='Greys') 
        # ax.contourf(x,y,l1,cmap='Reds') # 繪制3d等高線
        l1_pos = plt.Rectangle((0,0),1,1,fc='r') # 設定3維圖例
        l2_pos = plt.Rectangle((0,0),1,1,fc='dimgray')
        ax.legend([l1_pos,l2_pos],['L1正則','L2正則'])
        
        
        # 設定子圖2
        ax1 = plt.subplot(1,2,2,projection='3d')
        a1 = np.linspace(-5,5,num=10)
        b1 = np.linspace(-5,5,num=10)
        x1,y1 = np.meshgrid(a1,b1)
        c1 = np.abs(x1)+np.abs(y1)
        c2 = np.sqrt(np.square(x1)+np.square(y1))
        ax1.scatter3D(xs=x1,ys=y1,zs=c1,label='資料1')
        ax1.scatter3D(xs=x1,ys=y1,zs=c2,label='資料2')
        ax1.legend()
        ax1.view_init(elev=60,azim=30) # 調整3維圖像顯示角度
                   
        jupyter、matplotlib、Linux常見問題和設定記錄
      3. 小提琴圖(顯示一組資料分布,機器學習二分類任務,各個特征分布對比)
        # 3.小提琴圖(顯示一組資料分布,機器學習二分類任務,各個特征分布對比)
        fig,axs = plt.subplots(nrows=1,ncols=3)
        fig.set_size_inches(15,5) # 修改畫布尺寸(機關:英寸)
        fig.subplots_adjust(wspace=0.5) # 調整子圖間距
        
        test_data = pd.DataFrame({'feature1':random.choices(range(10,30),k=100)
                                  ,'feature2':random.choices(range(10,30),k=100)
                                  ,'feature3':random.choices(range(10,30),k=100)
                                  ,'label':[0 for i in range(100)]}).append(
                                  pd.DataFrame({'feature1':random.choices(range(25,45),k=100)
                                  ,'feature2':random.choices(range(25,45),k=100)
                                  ,'feature3':random.choices(range(25,45),k=100)
                                  ,'label':[1 for i in range(100)]}))
        for i in range(3):
            axs[i].violinplot(test_data.loc[test_data['label']==0,'feature'+str(i+1)],positions=[-2],widths=2,showmeans=True,showextrema=False)
            axs[i].violinplot(test_data.loc[test_data['label']==1,'feature'+str(i+1)],positions=[2],widths=2,showmeans=True,showextrema=False)
            axs[i].set_title('feature'+str(i+1))
            axs[i].set_xlabel(xlabel='0類特征                   1類特征') # 設定坐标軸标簽名稱
                   
        jupyter、matplotlib、Linux常見問題和設定記錄
      4. 填充圖或面積圖(聚類分析時,可檢視聚類效果)
        # 4. 填充圖或面積圖(聚類分析時,可檢視聚類效果)
        
        dataset_test = make_biclusters(shape=(1000,3),n_clusters=3,noise=0.1,minval=0,maxval=1,random_state=42)[0]
        # 測試資料展示
        fig = plt.figure(figsize=(12,5))
        ax = fig.add_subplot(projection='3d') # 添加3d坐标系
        ax.scatter(xs=dataset_test[:,0],ys=dataset_test[:,1],zs=dataset_test[:,2])
        
        # 聚類效果函數
        def make_clusters_effect(n_clusters=3):
            kmeans = KMeans(n_clusters=n_clusters)
            kmeans = kmeans.fit(X=dataset_test)
            res = kmeans.predict(dataset_test)
            silhouette_mean = silhouette_score(X=dataset_test,labels=res) # 總體輪廓系數(或整體輪廓系數平均值)
            silhouette_per = silhouette_samples(X=dataset_test,labels=res) # 每個樣本輪廓系數
            
            fig = plt.figure(figsize=(12,5))
            # 繪制子圖1
            plt.subplot(1,2,1)
            init_y = 0
            plt.xlim(-0.2,1) # 設定x軸刻度範圍
            for i in range(n_clusters):
                plt.fill_betweenx(y=range(init_y,init_y+len(silhouette_per[res==i])),x1=sorted(silhouette_per[res==i]))
                plt.text(x=-0.1,y=(2*init_y+len(silhouette_per[res==i]))/2,s=f'{i}類')
                init_y = init_y+len(silhouette_per[res==i])+10
            plt.vlines(x=silhouette_mean,ymin=0,ymax=1000,label='總輪廓系數',colors='r') # 設定水準線
            plt.legend()
            # 繪制子圖2
            ax = plt.subplot(1,2,2,projection='3d')
            for i in range(n_clusters):
                ax.scatter(xs=dataset_test[res==i,0],ys=dataset_test[res==i,1],zs=dataset_test[res==i,2],label=f'{i}類')
            ax.legend()
            fig.suptitle(f'聚類為{n_clusters}類效果圖') # 設定所有子圖總标題
            
        # 分别檢視聚類為3~5 的聚類效果
        for i in range(3,6):
            make_clusters_effect(i)    
                   
        jupyter、matplotlib、Linux常見問題和設定記錄
      5. 動态圖(git儲存,可以動态實時檢視深度學習目标函數損失情況)
        # 5. 動态圖(git儲存,可以動态實時檢視深度學習目标函數損失情況
        
        fig,axs = plt.subplots(1,2)
        # fig.set_size_inches(w=12,h=8)
        
        x = np.arange(0,2*np.pi,0.01)
        
        line1,=axs[0].plot(x,np.sin(x),c='C2');line2,=axs[1].plot(x,np.cos(x),c='C2')
        # 圖像初始化函數
        def init():
            axs[0].set_title('sin_images');axs[1].set_title('cos_images') # 設定子圖示題
            axs[0].set_xlim((0,10));axs[1].set_xlim((0,10)) # 設定x軸刻度範圍
            # axs[0].axis('off');axs[1].axis('off') # 取消坐标軸
            # axs[0].set_xticks(range(1,11));axs[1].set_xticks(range(1,11)) # 設定刻度值
            
        # 動态更新函數 
        def update(i):
            global x
            x+=i
            line1.set_ydata(np.sin(x)) # line1.set_ydata(y1) # 修改x軸,y軸資料
            line2.set_ydata(np.cos(x))
            
        ani = animation.FuncAnimation(fig=fig,func=update,
                                      frames=np.linspace(0,1),
                                      init_func=init,interval=200
                                     )
        
        # 将動态圖儲存為.gif
        ani.save('./test.gif',writer='pillow')
                   
        jupyter、matplotlib、Linux常見問題和設定記錄
      6. 修改坐标軸顯隐性及坐标軸位置
        data=np.array([[1,1,0],[-1,-1,0],[1,-1,1],[-1,1,1]])
        plt.figure(figsize=(12,5))
        axes=plt.subplot(1,2,1)
        # 設定坐标軸顯隐性
        axes.spines[['top','right']].set_visible(False)
        # 改變坐标軸位置
        axes.spines['left'].set_position(("data", 0))
        axes.spines['bottom'].set_position(("data", 0))
        axes.scatter(data[data[:,2]==0,0],data[data[:,2]==0,1],c='red')
        axes.scatter(data[data[:,2]==1,0],data[data[:,2]==1,1],c='blue')
        axes.set_xticks([-1,0,1]);axes.set_yticks([-1,1])
                   
        jupyter、matplotlib、Linux常見問題和設定記錄
  3. linux工作中常用指令記錄
    1. ssh免密登入
      ssh-keygen
      
       # 密鑰分發
       ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop01
       ssh-copy-id -i ~/.ssh/id_rsa.pub hadoop02
      
       # 驗證免密登入
      ssh hadoop02
                 
    2. 使用者設定(一般用不到)
      ## 使用者檔案
      #1. /etc/passwd --管理使用者UID/GID重要參數
         root : x : 0 : 0 : root : /root : /bin/bash
         使用者名稱:密碼:UserID:GroupID:使用者說明:主檔案:shell
      #2. /etc/shadow --管理使用者密碼(通過UID記錄相應密碼)
      #3. /etc/group  --管理使用者組相關資訊(通過GID關聯相應組資訊)
         root : x : 0 : root
         使用者組:使用者組密碼:GID:使用者組包含賬号名稱
      #4. /etc/gshadow --管理使用者組管理者相關資訊
      
      ## 新增使用者
         > useradd [options] [username] #(根據預設檔案生成使用者,但不會在/home生成同名賬号目錄,利      用passwd設定密碼,沒有密碼不能登入)
      
         > passwd [options] [username] # 設定使用者密碼
      
         > adduser [options] [username] # (相當與useradd的封裝,能自動生成使用者目錄,還得需要          passwd設定密碼)
      
      ## 删除使用者
         > userdel 同理
      
         > deluser
         
      ## 使用者權限
      # root使用者下下
      chmod +w /etc/sudoers # 增加寫權限
      vim  /etc/sudoers # 增加sudo使用者
      '''
      hadoop ALL=(ALL) NOPASSWD: ALL # 無密碼使用sudo
      '''
                 
    3. 磁盤、端口、系統資源檢視問題
      #1. 檢視端口
      netstat -apnt # 列出所有使用端口
        -a(all): 顯示所有連線中的Socket
        -p(programs): 顯示正在使用Socket的程式識别碼和程式名稱。
        -n(numeric): 直接顯示ip,而不是域名伺服器
        -t(TCP): 顯示TCP傳輸協定連結狀态.
        
      lsof -i:xx # 檢視指定端口 (list open file)
      
                 
    4. 檔案問題
      #1. 修改檔案權限
      chmod +w/777 file 
      
      # 2. 檔案複制
      cp -r 源檔案路徑 目标檔案路徑 # -r 遞歸複制目錄
                 
    5. 配置檔案
      # 1. 修改主機名
      vim /etc/hostname
      
      # 2. 配置主機IP映射
      vim /etc/hosts
      格式:ip hadoop hadoop
      
      # 3. 安裝telnet服務
      yum install telnet-server
      yum install telnet.*
      > telnet 指定IP 端口 
                 
    6. 日期的定時任務
      # 1. 擷取日期
      > date +%Y-%m-%d
      > 變量名=`date +%Y-%m-%d`
      
      # 2. 定時任務(沒有專門排程元件時可以linux自帶定時任務)
      crontab 
      
      
                 
    7. if語句和$符使用
      # 1. $符使用(本質上屬于變量替換)
      $n:指代腳本傳入的指定位置的參數,$0:代指該bash檔案名本身;兩位數以上位置需要${10}
      $*:所用腳本參數的内容,傳回字元串,字元串中存在空格
      $@:所有腳本參數的内容,傳回多個字元串。
      $#:傳回所有腳本參數的個數。
      $?:上一條指令的傳回值,成功是0,失敗是1。一般linux系統程序以系統調用exit()結束的,将其執行狀态status值,回傳給父程序,用來檢查子程序的執行狀态。
      $$:shell本身的PID,即目前程序PID。
      $!:shell最後運作的背景process的PID。
      
      ${}:作用1:劃分變量名的邊界(eg:each "${a}a" 第一個a是變量,第二個a是字元串)
          作用2:單行文本處理(eg: each "${#a}" 列印a變量的字元串長度) 
               
      # 2. if 語句
      if [ 條件表達式 ]; then
          指令一
      else
          指令二
      fi
      ## 條件表達式:
      # 2.1字元串判斷
      str1 = str2     當兩個串有相同内容、長度時為真
      str1 != str2     當串str1和str2不等時為真
      -n str1       當串的長度大于0時為真(串非空)
      -z str1       當串的長度為0時為真(空串)
      str1        當串str1為非空時為真
      # 2.2 數字的判斷
      int1 -eq int2   兩數相等為真
      int1 -ne int2   兩數不等為真
      int1 -gt int2    int1大于int2為真
      int1 -ge int2   int1大于等于int2為真
      int1 -lt int2    int1小于int2為真
      int1 -le int2    int1小于等于int2為真
      # 2.3 檔案相關的if判斷條件語句
      -r file     使用者可讀為真
      -w file     使用者可寫為真
      -x file     使用者可執行為真
      -f file     檔案為正規檔案為真
      -d file     檔案為目錄為真
      -c file     檔案為字元特殊檔案為真
      -b file     檔案為塊特殊檔案為真
      -s file     檔案大小非0時為真
      -t file     當檔案描述符(預設為1)指定的裝置為終端時為真
      # 2.4 複雜邏輯判斷
      -a        與
      -o       或
      !       非
      # 2.5 雙條件表達式
      if [[條件表達式1]||(或,&&與)[條件表達式2]];then
      fi
                 
  4. 待定

繼續閱讀