<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by 靈芝 LING-ZHI on Medium]]></title>
        <description><![CDATA[Stories by 靈芝 LING-ZHI on Medium]]></description>
        <link>https://medium.com/@shiuanshr?source=rss-30ea9c5a5779------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*7t8LfrbUQMUGhvL2f7_low.jpeg</url>
            <title>Stories by 靈芝 LING-ZHI on Medium</title>
            <link>https://medium.com/@shiuanshr?source=rss-30ea9c5a5779------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 00:47:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shiuanshr/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Kubernete Learning Note — Preface]]></title>
            <link>https://medium.com/@shiuanshr/kubernete-learning-note-preface-3c7ee332502f?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/3c7ee332502f</guid>
            <category><![CDATA[sre]]></category>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Mon, 26 Jan 2026 19:56:53 GMT</pubDate>
            <atom:updated>2026-01-26T19:56:53.940Z</atom:updated>
            <content:encoded><![CDATA[<h3>Kubernete Note — Preface</h3><p>當你走向企業應用, 你需要討論的AI 架構不再只是算法或是開發, 而是穩定性/HA/資安甚至到產品規劃是否能夠快速地模組化, 夠General, 支援各種應用, 達到scale out的目的，這時候, 你就慢慢地走向K8S的深淵(?)</p><p>這邊先shout out我在TSMC這三年從事DevOps/MLOps, 期間受到各個SRE大神的教導, 幫助我良多</p><p>從前沿應用走到團隊的產品系列的系統架構規劃, 每次接觸新領域, 就會意識到自己的淺薄時, 對比這個領域, 我所知的好比滄海一粟, 希望自己永遠飢渴的學習, 並能夠持續的擴大幫助到的人, 以此利用筆記的方式強迫自己融會貫通。</p><p>希望涵蓋的基本概念, 會持續的開展開來系列文章</p><ul><li>Docker</li><li>Helm / plain YAML</li><li>Deployment/Pod/ReplicaSet</li><li>ConfigMap</li><li>Ingress/egress</li><li>Service</li><li>Cluster Autoscaler</li><li>Istio Basic</li><li>Istio Service Mesh</li><li>RBAC</li><li>Node Hardening</li><li>CRI-O</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*kDvj0eLFXn2KlVzO.jpg" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3c7ee332502f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[一文搞懂 Kubernetes：Minikube、Cluster 與 TLS 憑證全攻略]]></title>
            <link>https://medium.com/@shiuanshr/%E4%B8%80%E6%96%87%E6%90%9E%E6%87%82-kubernetes-minikube-cluster-%E8%88%87-tls-%E6%86%91%E8%AD%89%E5%85%A8%E6%94%BB%E7%95%A5-fd97859e872f?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd97859e872f</guid>
            <category><![CDATA[kubectl]]></category>
            <category><![CDATA[tls-certificate]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[minikube]]></category>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Sun, 11 Jan 2026 08:32:47 GMT</pubDate>
            <atom:updated>2026-01-11T08:32:47.995Z</atom:updated>
            <content:encoded><![CDATA[<p>本文筆者簡單用minikube + Docker起了一個本機端的Kubernete (方法參考本連結 <a href="https://kubernetes.io/fr/docs/tasks/tools/install-minikube/">MiniKube</a>)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/642/1*AeVfZ1zz2pd9-62mSd041Q.png" /><figcaption><em>Cluster MiniKube architecture</em></figcaption></figure><p>這是一個最小可執行的架構, 也就代表minikube其實跟實務的最佳實作不同。 minikube目標是方便開發prototype/學習等用途, 因此, Master Node 內的元件會跟worker node的元件共住同一個node (default name: minikube)。實務上. 時常master跟worker node是切開的, 或是利用taint等技術, 避免control plant被當成是可消耗資源, 這邊特別註記, 小心不要誤會了。</p><p>實務上各公司的叢集內部建置會更複雜, 看需求而定, 依照各公司狀況準, 這邊列個可能worker 與master節點切割的樣子。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*rn6oJu99ZI-UmfBxlsQ7Sw.png" /></figure><p>第一步, 先了解自己是哪一個叢集(cluster), 所謂的cluster, 表示一組協同工作的資源與服務的集合, 一定會包含控制平面（Control Plane）和一到多個工作節點（Worker Node)</p><pre># 查閱目前本地kubectl kube config設定<br>kubectl config get-contexts</pre><p>輸出解釋如下(範例) :</p><pre>CURRENT   NAME       CLUSTER    AUTHINFO   NAMESPACE<br>*         minikube   minikube   minikube   default<br># CURRENT: current location (目前位置)<br># NAME: alias (暱稱, 可略)<br># Cluster: cluster entry point<br># AUTHINO: with user certificate auth, 指向一組使用者憑證<br># NAMESPACE: ns name</pre><p>重點是這一欄：</p><pre>AUTHINFO = minikube</pre><p>這代表你用key值為minikube的身分組進行活動, 特過查閱kubeconfig來了解更多, kubeconfig 是 kubectl client的本地設定檔，它不是 Kubernetes API 物件，也不能被 API Server 控制或查詢, 所以你無法透過下kubectl指令修改kubeconfig</p><pre>kubectl config view # Search for current context config setup<br><br>kubectl get config # wrong way, it&#39;s client side<br>kubectl get kubeconfig # wrong way, it&#39;s client side</pre><pre>apiVersion: v1<br>clusters:<br>- cluster:<br>    certificate-authority: C:\Users\MOCK\.minikube\ca.crt # Root CA<br>    extensions:<br>    - extension:<br>        last-update: Sun, 11 Jan 2026 12:41:23 CST<br>        provider: minikube.sigs.k8s.io<br>        version: v1.37.0<br>      name: cluster_info<br>    server: https://127.0.0.1:XXX # API Server 位址, 127.X.X.X是local用的網段; XXX為port<br>  name: minikube<br>contexts:<br>- context:<br>    cluster: minikube # cluster 的識別名稱<br>    extensions:<br>    - extension:<br>        last-update: Sun, 11 Jan 2026 12:41:23 CST<br>        provider: minikube.sigs.k8s.io<br>        version: v1.37.0<br>      name: context_info<br>    namespace: default<br>    user: minikube<br>  name: minikube<br>current-context: minikube<br>kind: Config # 這不是 K8S cluster 裡的物件, 而是 kubectl 的本地設定檔<br>users:<br>- name: minikube<br>  user:<br>    # client-certificate 透過 TLS 用來向 API server 證明使用者身分<br>    client-certificate: C:\Users\MOCK\.minikube\profiles\minikube\client.crt # # CA 憑證位置<br>    client-key: C:\Users\MOCK\.minikube\profiles\minikube\client.key</pre><p>我們可以發現你面有幾組CA, 分別是 client-certificate, client-key, certificate-authority, 我們去放CA的位置來看看</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/483/1*fqSqzf4pSkuYqVJ6iRMs3A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*S-763XT2WZgdMH7u9tGz0Q.png" /><figcaption>Minikube TLS Releationship Diagram</figcaption></figure><p>啟動 Minikube 時，系統會先生成一個叢集根憑證，也就是 ca.crt 和 ca.key（a）。這個根憑證就像整個叢集的信任中心，它負責簽發所有其他憑證。Cluster CA 簽發了 API Server 的伺服器憑證，以及用戶端的 client.crt（c）。前者保證 API Server 對外提供安全的 TLS 連線，後者則用於本機的 kubectl 連線，並放入 kubeconfig 中。</p><p>當我們使用 kubectl 發送指令時，它會用 client.crt 和對應的私鑰 client.key 建立安全連線，API Server 會檢查憑證是否由叢集 CA 簽發。如果通過驗證，API Server 會讀取憑證裡的 CN 作為使用者名稱，O 則對應到使用者所屬的群組，這就是 Kubernetes 的 RBAC 授權依據。</p><p>除了這組主要的使用者憑證，Minikube 還生成了一組用於內部組件的代理憑證，也就是 proxy-client-ca.crt 和 proxy-client-ca.key（b），以及由它簽發的 proxy-client.crt 和 proxy-client.key（d）。這些憑證主要是提供聚合 API 或其他內部組件（例如 metrics-server）與 API Server 之間的雙向 TLS 認證，確保內部元件之間的通信也是安全的。</p><p>總結來說，整個 Minikube 的 TLS 架構可以想像成兩個平行的憑證簽發鏈：一個是叢集根 CA 管理外部使用者與 API Server 的安全連線，另一個是代理 CA 管理內部組件之間的安全通信。這樣即使是一個單節點的 Minikube，也能完整地模擬 Kubernetes 真實叢集中的憑證安全機制。</p><pre>根 CA → 簽發所有憑證<br>client.crt/key → 使用者身份，用於 kubectl<br>proxy-client.crt/key → 內部元件身份，用於元件間安全連線<br>CN/O → 決定使用者名稱與群組，給 RBAC 授權</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*Ra4dlpFiHVU-eEmc4kmn6w.png" /></figure><p>當你輸入kubectl指令, 查看目前有哪些pod存在這一顆node上</p><pre>kubectl get pods -n kube-system</pre><p>會顯示類似以下內容,</p><pre>coredns-66bc5c9577-qhpmt 1/1 Running 4 (36m ago) 55d<br>etcd-minikube 1/1 Running 4 (36m ago) 55d<br>kube-apiserver-minikube 1/1 Running 4 (36m ago) 55d<br>kube-controller-manager-minikube 1/1 Running 4 (36m ago) 55d<br>kube-proxy-9jmtv 1/1 Running 4 (36m ago) 55d<br>kube-scheduler-minikube 1/1 Running 4 (36m ago) 55d<br>registry-66898fdd98-mlb6v 1/1 Running 3 (36m ago) 55d<br>registry-proxy-nsmqb 1/1 Running 3 (36m ago) 55d<br>storage-provisioner 1/1 Running 6 (36m ago) 55d</pre><p>他其實是分別由4個東西組成</p><ul><li><strong>Control Plane（大腦）</strong></li></ul><pre>- etcd-minikube (唯一真實狀態資料庫, 只能由api server access)<br>- kube-apiserver-minikube (所有kubectl指令入口)<br>- kube-controller-manager-minikube (Controller大禮包, 可以更新etcd內的Deployment/ReplicaSet/Node/Job 狀態)<br>- kube-scheduler-minikube (幫「還沒家」的 Pod 找 node)</pre><ul><li><strong>Node-level（每台機器的代理）</strong></li></ul><pre>- kube-proxy-9jmtv</pre><ul><li><strong>Cluster Add-ons（功能模組）</strong></li></ul><pre>- coredns-66bc5c9577-qhpmt<br>- storage-provisioner</pre><ul><li><strong>Image infra（Minikube 附加, 可略）</strong></li></ul><pre>- registry-66898fdd98-mlb6v<br>- registry-proxy-nsmqb</pre><p>這邊附上一個簡單的deployment.yaml來探討一下基本的- Deployment/Replica set/ Pod的關係</p><pre>apiVersion: apps/v1<br>kind: Deployment<br>metadata:<br>  name: demo-api # 必填<br>  namespace: {{ .Release.Namespace }}# (optional) 用Azure CD 填帶入會優先於在yaml中硬編碼<br>  labels:<br>    app: demo<br>    tier: backend<br>spec:<br>  replicas: 3 # 希望有 3 個 Pods 由此 Deployment 管理<br>  revisionHistoryLimit: 5   # 保留幾個歷史 ReplicaSet用來rollback<br>  progressDeadlineSeconds: 600 # <br>  paused: false<br>  strategy: #default就是rollinguUpate, 沒要改可以不用寫<br>    type: RollingUpdate<br>    rollingUpdate:<br>      maxSurge: 1           # 更新時最多多 1 個 Pod<br>      maxUnavailable: 1     # 最多同時少 1 個 Pod<br><br>  selector:<br>    matchLabels:<br>      app: demo  #MUST等同selector.matchLabels.app<br>      component: api #MUST等同selector.matchLabels.component<br><br>  template:<br>    metadata:  #貼到POD上的資訊<br>      labels:<br>        app: demo #MUST等同selector.matchLabels.app<br>        component: api  #selector.matchLabels.component, 可略<br>        version: v1 #⚠️ 這不是給 K8s 用，是給「人 / 觀測」用<br>    spec:<br>      containers:<br>      - name: api<br>        image: demo-api:1.0.0   # ✅ 真正的「版本定義點」<br>        imagePullPolicy: IfNotPresent <br><br>        ports:<br>        - containerPort: 8080 # container 暴露 8080 port。在 Service 裡才會用到。<br><br>        env:<br>        - name: ENV<br>          value: production<br>        - name: LOG_LEVEL<br>          value: info<br><br>        resources: # 如何讓 scheduler 做合理放置<br>          requests:<br>            cpu: &quot;200m&quot; # 最低用量 0.2 core<br>            memory: &quot;256Mi&quot;<br>          limits:<br>            cpu: &quot;500m&quot; # 最高用量<br>            memory: &quot;512Mi&quot;<br><br>        readinessProbe: 如果沒有 → Rollout還沒完成流量就進來了<br>          httpGet:<br>            path: /health<br>            port: 8080<br>          initialDelaySeconds: 5<br>          periodSeconds: 10<br><br>        livenessProbe: 如果沒有 → K8s 無法自動偵測 container 崩潰以重啟它<br>          httpGet:<br>            path: /health<br>            port: 8080<br>          initialDelaySeconds: 15<br>          periodSeconds: 20</pre><p>這邊的意思是在說</p><pre>Deployment: 「我想要 3 個 hello 的服務」<br>ReplicaSet: 「我要確保有 3 個符合條件的 Pod 存在」<br>Scheduler: 「這顆 Pod 該去哪一台 Node？」<br>kubelet:「這顆 Pod 被指派給我了」</pre><pre>                 ┌────────────────────────┐<br>                 │        etcd            │<br>                 │ (Desired State Store)  │<br>                 └──────────┬─────────────┘<br>                            │<br>                            ▼<br>                ┌─────────────────────────┐<br>                │     kube-apiserver      │<br>                │  (唯一讀寫 etcd 入口)   │<br>                └──────────┬──────────────┘<br>                           │<br>        ┌──────────────────┴──────────────────┐<br>        │                                      │<br>        ▼                                      ▼<br>┌────────────────────┐           ┌────────────────────┐<br>│ kube-controller-   │           │   kube-scheduler   │<br>│ manager            │           │                    │<br>│                    │           │ - 選 Node          │<br>│ - Deployment ctrl  │           │ - 填 nodeName      │<br>│ - ReplicaSet ctrl  │           └──────────┬─────────┘<br>│ - Node ctrl        │                      │<br>└──────────┬─────────┘                      │<br>           │                                 │<br>           ▼                                 ▼<br>   ┌────────────────────┐         ┌────────────────────┐<br>   │    ReplicaSet       │         │        Pod         │<br>   │  (desired count)    │         │  (node assigned)   │<br>   └──────────┬─────────┘         └──────────┬─────────┘<br>              │                                 │<br>              ▼                                 ▼<br>        ┌────────────┐                 ┌──────────────┐<br>        │    Pod      │               │     Node      │<br>        │ (object)    │                │              │<br>        └────────────┘                 │  kubelet     │<br>                                       │  containerd  │<br>                                       └──────┬───────┘<br>                                              ▼<br>                                        ┌────────────┐<br>                                        │ Container  │<br>                                        │ (process)  │<br>                                        └────────────┘</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd97859e872f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[學習筆記-XAI可解釋人工智慧]]></title>
            <link>https://medium.com/@shiuanshr/%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-xai%E5%8F%AF%E8%A7%A3%E9%87%8B%E4%BA%BA%E5%B7%A5%E6%99%BA%E6%85%A7-18dd141e6a5b?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/18dd141e6a5b</guid>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Sun, 11 Jan 2026 05:20:25 GMT</pubDate>
            <atom:updated>2026-01-11T05:20:25.823Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>可解釋性重要於以下幾點:</strong></h3><ol><li><strong>識別和防止偏差</strong></li></ol><blockquote>幫助複雜模型確定是依靠正確的特徵來進行預測</blockquote><p>一般來說，方差(Variance)與偏差是機器學習中重要的議題。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*vAYqkVW2Wb4LvaoIBHMrzw.png" /></figure><p>而有偏差(Bias) 的模型經常由有偏見的事實導致，例如用於招聘的模型，揭示了在特定職位上的性別偏差</p><p>E.g., 男性軟件工程師和女性護士</p><p>2. 簡化模型，降低計算成本，降低企業成本</p><p>這部分主要是關於算法透明度（Algorithm Transparency）的應用。</p><p>3. GDPR要求算法能夠解釋如何使用使用者的個人資料</p><p>4. 當維度數量大量，難以用簡易的傳統統計模型( 例如，LR, decision Tree, NB)達成兼具可解釋性與準確性的預測結果時。</p><p><strong>5. 模型的可信和透明度</strong></p><blockquote>特定產業，例如，保險、信用(是否拒絕貸款申請)、醫療(專家系統)、司法(輔助法律裁決相關系統)…</blockquote><blockquote>在一些低風險的情況下，不一定要知道決策是如何做出的，比如推薦系統，廣告、視頻或者商品推薦等。但是在其他領域，比如在金融和醫療領域，則有其必要性</blockquote><p>可解釋性涵蓋以下三部分 (不限於這三個) :</p><h3>算法透明度（Algorithm Transparency）</h3><p>— — — — — — — — — — —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/777/1*PS3aaKAM59qRIDI1TRwZqg.png" /></figure><h3>全局可解釋（Global Interpretability）</h3><p>How does the trained model make predictions?</p><pre>這種可解釋性水平是指模型如何根據整個特徵空間做出決策，以及模型結構、參數等，哪些特徵是重要的，特徵之間發生了哪些相互作用。<br><br>該模型的全局可解釋性有助於理解目標變量對於不同特徵的分佈情況。<br>在實踐中，當一個模型有大量的參數時，很難想像特徵是如何相互作用才能得到這樣的預測結果。<br>某些模型在此級別上是可解釋的。<br><br>例如，線性模型的權重是可解釋的，樹模型如何劃分分支並獲得節點預測值是可解釋的。</pre><h3>局部可解釋（Local Interpretability）</h3><p>Why did the model make a certain prediction for an/a group of instances?</p><pre>局部可解釋性更側重於單個樣本或一組樣本。<br><br>這種情況下，我們可以把模型看成一個黑盒子，不再考慮模型的複雜度。對於單個樣本，模型的預測值和某些特徵可能存在線性關係甚至單調關係。<br>因此，局部可解釋性可能比全局可解釋性更準確。</pre><p>獲得可解釋性的方法主要有三種:</p><ol><li>Model-agnostic XAI method</li><li>Model-based</li></ol><p>比如線性回歸、邏輯回歸、決策樹等可解釋模型來簡化目前模型或是直接解釋。</p><p>3. 與model無關的方法</p><p>3–1.<strong> Permutation Feature Importance</strong></p><pre>基本思想：<br><br>若要研究特徵重要性，則將該列數據隨機打亂，其他列數據保持不變，<br>觀察預測的metric(如準確率)或loss是否有變化，根據變化量來判斷特徵重要性。<br>若打亂後對準確率影響不大，則可認為該特徵重要性較低。</pre><p>現有package:</p><ul><li>Permutation Feature Importance-slearn</li></ul><pre>from sklearn.inspection import permutation_importance<br>result = permutation_importance(clf, train_x, train_y, n_repeats=10,random_state=42)<br>featImp = pd.DataFrame()<br>featImp[&#39;feat&#39;] = feature_names<br>featImp[&#39;importance&#39;] = result.importances_mean<br>featImp = featImp.sort_values(&#39;importance&#39;,ascending = False)<br><br>plt.figure(figsize=[20,10])<br>sns.barplot(x = &#39;importance&#39;, y = &#39;feat&#39;,data = featImp[:20],orient=&#39;h&#39;)<br>plt.show</pre><p>此外，python中的xgb包生成的特徵重要性和scikit-learn中的Permutation Feature Importance可能會有所不同，因為它們使用不同的方法來計算特徵重要性。 XGBoost 使用基於每個特徵增益的內置特徵重要性計算，而 Permutation Feature Importance 打亂單個特徵的值並觀察它如何影響模型的性能。</p><p>這兩種方法可以提供互補的信息，將它們結合起來有助於特徵選擇。</p><p>但是，值得注意的是，不建議將 sklearn 的排列特徵重要性用於基於樹的模型，例如 xgboost，因為它不是為這些類型的模型設計的。</p><p>關於你提供的說法，它正確地指出Permutation Feature Importance可以用來刪除干擾後提高預測效果的特徵。然而，並不總是應該刪除較小的特徵重要性。特徵重要性應與其他因素一起考慮，例如特徵之間的相關性和模型的整體性能。</p><p>3–2. <strong>Permutation Feature Importance-eli5</strong></p><pre>import eli5<br>from eli5.sklearn import PermutationImportance<br>perm = PermutationImportance(clf, n_iter=10)<br>perm.fit(train_x, train_y)<br>eli5.show_weights(perm, feature_names=train_x.columns.tolist())</pre><p>3–3. PDP ( Partial Dependency Plots)</p><p>上述的Feature importance無法得知不同features如何影響結果，只知道重要程度。PDP可以求特徵和預測結果的關係。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18dd141e6a5b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[關於Spark的小知識]]></title>
            <link>https://medium.com/@shiuanshr/%E9%97%9C%E6%96%BCspark%E7%9A%84%E5%B0%8F%E7%9F%A5%E8%AD%98-92be69567a09?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/92be69567a09</guid>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Fri, 24 Mar 2023 18:07:24 GMT</pubDate>
            <atom:updated>2023-03-24T18:08:18.436Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W43faqY8CEQlYokqC5XFlA.png" /></figure><p>這一篇不是要聊什麼艱深的問題，單純是最近和朋友聊天時，因為領域不同，他問我這個專案關於Spark的問題，我發先自己竟然答不上來，因此，速速來寫寫一篇懺悔，寫一篇簡單直白，面試能說嘴的文章。</p><blockquote>Q1. PySpark跟Spark差別在哪?</blockquote><p>PySpark可以視作Spark的Python版本，他是 Apache Spark 的 Python API。像是SparkR就是同一種概念，可以用輕量的方式在R語言中用Spark</p><blockquote>Q2. RDD是什麼？為什麼RDD很重要？</blockquote><p>RDD是Resilient Distributed Dataset的意思。他是一種高容錯能力的資料結構，可以分散式處理巨量資料。</p><p>RDD的特性是</p><p>(1). immutable: 他是不可變的，樣子像是tuple。</p><p>(2). 可以進行partition 分區</p><p>(3). 可以透過checkpoint進行緩存，讓運算速度加快</p><p>理論上，RDD因為是很底層的資料結構，在多種任務上，讓Spark很有彈性，並且低延遲、高速度。</p><blockquote>Q3. 為何你專案選擇用RDD不是dataframe?</blockquote><p>RDD的優勢在</p><ol><li>高容錯: 數據丟失時，窄依賴只需要重新計算丟失的那一塊來恢復</li><li>批量操作: 切割Spark Streaming成Batch</li><li>緩和straggler問題(某個node拖後腿)</li></ol><p>當某些節點處理速度較慢或發生故障時，RDD會傳輸重複的任務給其他節點執行，以確保作業效率。</p><p>4. Crash時能夠degrade gracefully優雅降級，保留核心功能</p><p>例如，一個電子商務網站可能會在大量流量下調整其頁面的外觀和交互方式，以便快速響應和維護核心購買功能。在這種情況下，如果網站無法處理所有的請求，它仍然可以顯示核心的產品和價格信息，以確保其關鍵業務功能的可用性。</p><p>理論上的速度、和可容錯性，我的專案中建立的pipeline優點是在可擴充性和高容錯，所以完全RDD進行。</p><blockquote>Q4. (追問) Dataframe其實比RDD更快，你知道位什麼嗎?</blockquote><p>實際上，Spark中的DataFrame通常會比RDD更快，因為DataFrame在許多方面都是被優化過的。只有在某些特定場景下，如需要對每個欄位進行複雜操作，或在分散式環境中需要更細緻的控制時，使用RDD可能會更好。</p><p>但是就開發與資料操作上，我個人覺得dataframe大勝。</p><blockquote>Q4. (追問) 你剛剛提及容錯性，為麼RDD容錯性高?</blockquote><p>我覺得是因為窄依賴與寬依賴的存在，</p><p>a. 窄依賴，例如map，是母RDD只有部分被子RDD依賴，例如map</p><p>b. 寬依賴則相反，是母RDD多個部分被子RDD依賴，例如join</p><p>進行窄依賴後，可以透過對部分RDD的反向得回原本的母RDD，因此可以回溯，並且RDD可以透過checkpoint來暫存，這也是回溯點</p><blockquote>Q5. 問個大問題，Spark 的架構與運作方式是甚麼?</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/838/1*2eTxcEtsz3-GpROY7wnjYQ.png" /></figure><p>譬喻:</p><p>Client式客戶端，提交任務Job</p><p>Master是老闆，控制Worker(員工)與Executor</p><p>Master會靠Driver進行專案管理，包含解析工作、生成每個Stage</p><p>Worker: 員工</p><p>Executor:機械手臂 (被worker操作)</p><p>Task: 運行在Executor上的unit</p><blockquote>Q6. 比較Dataframe與RDD</blockquote><p>Dataframe是抽象化有schema的RDD，可以用SparkSSQL進行操作，開發上比較有效率</p><p>RDD是Dataframe組成的最小單位，不可變動，也無法得知內部結構，除非用action把他print出來，理論上，進行複雜運算時，比較有計算上的效率。</p><blockquote>Q7. Standalone模式是甚麼?</blockquote><p>我們通常用本機跑local就是standalone，通常用來練習、測試，或是小dataset的操作。基本上在Standalone模式下，Master負責管理Worker節點、分配計算資源、調度Task，而Worker負責接收Master的指令並運行Executor進行Task。</p><blockquote>Q8. Driver是scheduler?</blockquote><p>一般常見有DAG Scheduler和Task Scheduler</p><p>DAG負責有向無環的將作業(一次action就有一個job)拆解成各個Stage，每個Sep rm4</p><p>RDD切多細(partition數量)決定跑多少個Task</p><blockquote>Q9. 常見操作有哪些類型?</blockquote><p>分為兩種，一種叫Transformation，一種是action</p><p>Transformation操作只會得到RDD，你無法看到這個RDD尊容(特性)，因此所有Transformation都是lazy</p><p>Action得到結果存入內存(不是RDD)，驅動前面pipeline上所有的Transformation，簡而言之，計算只有下action時才會開始跑</p><blockquote>Q10. 進入困難一點的比較，通常實時處理的情境我們會用Spark Streaming，缺點有哪些?</blockquote><p>進行microbatch微批次處理，所以勢必還是有延遲，只能是&quot;準實時&quot;。例如，噴射機系統這種對realtime要求高的應用大概率是不可能被導入的。</p><blockquote>還有嗎?</blockquote><p>想不太到</p><blockquote>Q11. 那聽過Hive嗎? 通常我們會透過Hive下SQL指令從HDFS來操作。</blockquote><blockquote>跟我說說Hive跟RDB差別</blockquote><p>Hive 是一個類似 SQL 的工具，RDB式資料庫，如果更好的比較應該是HBase跟RDB。</p><p>我覺得HBase有點像是NoSQL的資料庫，例如firebase，所以可擴展性高、數據規模大。</p><p>Hive不適合頻繁的寫入，有點像是非關聯式的資料庫，例如Firebase，RDB沒差。</p><p>Hive沒有index，RDB有，並且….RDB擴展要考慮關聯和schema</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=92be69567a09" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Decision Tree Pruning: How to Be a Good Gardener?]]></title>
            <link>https://medium.com/@shiuanshr/decision-tree-pruning-how-to-be-a-good-gardener-67fb0109cfff?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/67fb0109cfff</guid>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Wed, 08 Mar 2023 09:51:18 GMT</pubDate>
            <atom:updated>2023-03-08T10:15:33.485Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*eU8bSZJdp2ljj8wMHdPSMw.png" /></figure><p>Prior Knowledge: <a href="https://www.mihaileric.com/posts/decision-trees/">https://www.mihaileric.com/posts/decision-trees/</a></p><p>Most of the time, we choose to do <strong>pre-pruning</strong> instead of <strong>post-pruning</strong> to avoid overfitting, which frequently happens when adopting a decision tree.</p><h3>What are the <strong>pre-pruning and post-pruning?</strong></h3><blockquote><strong>Well, basically, pre-pruning is you trim your tree as it’s growing, and post-pruning indicates that you only prune the branches as the whole tree is completely built.</strong></blockquote><h3>But why?</h3><p><strong>The reason is as follows:</strong><br>1. Post-Pruning is more <strong>computationally expensive</strong></p><p>2. Post-pruning is less <strong>effective for avoiding over-fitting</strong> because some over-fitting may already be present in the full tree.</p><p>Post-pruning does the simplification after the full tree is built. (Some noise caught by overfitting is not in the leaf but in the middle layer of the node, which is bare to cut off)</p><p>3. Post-pruning is preferred when the <strong>data is noisy or the tree is very large.</strong></p><p>Note:</p><p>some ways to tell if a dataset is noisy</p><blockquote>Keywords:</blockquote><blockquote>(1). Outliers</blockquote><blockquote>(2). Inconsistencies (Try: PYOD package in python)</blockquote><p><a href="https://getliner.com/zh/picked-by-liner/reader-mode?url=https%3A%2F%2Fzhuanlan.zhihu.com%2Fp%2F58313521">LINER | Search Less, Learn More</a></p><blockquote>(3). Variability</blockquote><blockquote>(4). Expert knowledge</blockquote><blockquote>(5). Model performance</blockquote><h3>About pre-pruning</h3><p>There are several pre-pruning strategies that can be used to prevent overfitting and simplify the tree.</p><h4>Here is the summary if you have the prior knowledge</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*kJzcr2fotg9CsfFZvyVMZQ.png" /></figure><h4>For more details and explanation:</h4><ol><li><strong>Maximum depth:</strong></li></ol><p>This strategy limits the maximum depth of the decision tree. The advantage is that it can help prevent overfitting by forcing the decision tree to stop growing before it becomes too complex. The disadvantage is that it may not be the most effective strategy for all datasets, as some datasets may require deeper trees to achieve good accuracy</p><p><strong>2. Maximum number of leaf nodes</strong></p><p>It prevent overfitting by limiting the complexity of the tree. The disadvantage is that it may not be the most effective strategy for all datasets, as some datasets may require more leaf nodes to achieve good accuracy.</p><p><strong>3. Minimum number of samples per leaf:</strong></p><p>This pre-pruning strategy sets a minimum threshold for the number of samples that must be present in each leaf node. This can help prevent overfitting by ensuring that each leaf node represents a meaningful subset of the data and reducing the impact of noise and outliers.</p><p><strong>4. Minimum impurity decrease:</strong></p><p>This strategy stops the tree construction process if splitting a node does not result in a minimum decrease in “impurity”. This strategy stops the tree construction process when a leaf node contains fewer than a certain number of samples. It can be effective in preventing overfitting, especially when the dataset is small or the classes are imbalanced.The disadvantage is that setting the minimum number of samples per leaf too high can result in underfitting and reduced accuracy.</p><p><strong>5. post-pruning strategy: pruning and cost complexity pruning</strong></p><p><strong>(Not discussed here)</strong></p><p>In addition to these pre-pruning strategies, there are also post-pruning strategies that can be used to simplify the tree after it has been trained, such as reduced error pruning and cost complexity pruning.</p><p>Also, some data scientist tend to use “Random Forest Classifier” to avoid the overfitting problem that decision tree models tend to have. However, it’s another black box which loosing the transparency of the model.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=67fb0109cfff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[2022年末] 清大服科生活復盤]]></title>
            <link>https://medium.com/@shiuanshr/2022%E5%B9%B4%E6%9C%AB%E5%BE%A9%E7%9B%A4-5c36ba2ed94e?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/5c36ba2ed94e</guid>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Tue, 20 Dec 2022 04:32:45 GMT</pubDate>
            <atom:updated>2022-12-20T04:33:44.714Z</atom:updated>
            <content:encoded><![CDATA[<p>在清大的最後一年，之前曾經前輩提及過可能發生的阻礙都逐漸浮現，手上5個專案、2個TA，一個改了又改的碩論題目的，讓我工作的喘不過氣來。</p><p>往往越沒空運動時，越需要運動。我想壓力處理也是這樣。</p><p>靜下來，讓我想好好想想，事情怎麼變成這樣的。</p><p>在服科兼顧技術沒有一學期是輕鬆、沒有機會好好生活。<strong>直到昨天 和所上朋友討論專案事項，驚覺自己會因為對方對某些話題的冷處理、或是馬虎帶過感到不耐煩，</strong>這時我才了解到前幾週去看醫生時，醫生說的:</p><blockquote>「你的免疫系統不好，一天睡幾個小時?」</blockquote><blockquote>「4~6個」</blockquote><blockquote>「半夜玩遊戲?」</blockquote><blockquote>「我不打遊戲，只工作」</blockquote><blockquote>「有必要嗎? 這樣算是生活還是活著?」</blockquote><h4>是的，沒有必要。</h4><ol><li>沒有工作值得我們拿生活與對待朋友時的態度去交換</li><li>生活是生活，工作是工作</li></ol><p>嚴謹嚴肅的工作態度是我剛出社會時學到的教訓，但是目前看下來，這是有毒的。做事嚴謹，態度泰然，也許才是我想追求的。</p><p>一直以來，可能自己憑著不怕累，不怕挫折的態度，再硬的專案、課都接，能力自然會培養，但是為何不選擇能夠讓自己有餘俗溫柔待人的工作負擔?</p><p>我散步時對自己說，</p><blockquote>「沒有假日的衝刺第3年了，給自己喘口氣吧?」</blockquote><blockquote>「否則，過度的壓力只會推著朋友離開你，但是你很清楚你不想要這樣，閒暇時你再好相處，工作時，面對多工的工作負荷，你只會因為一些小事變成王八蛋，無論你貢獻再多，幫別人團隊擦多少屁股，大家對你的最後印象只會是- 他很難搞、總是有哪邊不滿意，這不是我樂見的，也不是我希望留在真正朋友心底的印象」</blockquote><blockquote>「你到底要錯過多少人只為了追求內心那個虛榮感?」</blockquote><blockquote>「更何況，有時根本不是對方的問題，那問題是誰造成的?」</blockquote><blockquote>「只能是我自己了，不是嘛?」</blockquote><blockquote>是哪邊做錯了嗎?</blockquote><blockquote>「個性使然? 生活品質? 」</blockquote><p><strong>如果是個性，我能清楚認知到，工作上的我，是個心機的混蛋。</strong></p><p>心機是求自保，混蛋是對別人苛責，兩個概念都是希望控制事情不要往自己不樂見的狀況發展。</p><p>混蛋有一部分是必要的，他保護我們自己不要成為職場上的好好先生，避免被占便宜而不自知，因為只要有人的地方，占了便宜還賣乖的人比比皆是。</p><blockquote>但是很大一部分的混蛋，沒有必要。</blockquote><p>他只是一個宣洩、控制的出口，沒有人喜歡被控制，所以最終結果只會不受控制。當然，當下自己能否覺察自己的惱火有沒有必要，是不是對方造成的?這是一件事。問題點是，當下自己還是會感到惱火，儘管轉念間就會平復。</p><p>所以該如何好好處理這個問題呢?</p><h4>1. EQ?</h4><p>以前覺得，EQ對我來說，不是不生氣，而是在對的時候生氣。</p><p>只要，只要生氣，就代表內心某部分失望了、不滿了，過去忍著等待時機，忽視了內心到底好不好受這件事。</p><p>我們常常認識有些人，遇到讓人容易感到壓力、Emo的事情往往如何處理?</p><blockquote>雖然很像很不正確，不過許多人就是選擇逃避了，而最後還活著不錯?</blockquote><h4>2. Work Loading</h4><p>這半年讓我知道自己的器在哪裡，明年會減少成原本的1/3，花更多心思在人生上。少專心在幫別人賺錢的事上，多做點幫自己賺錢的事。</p><h4>3. 適度休息，大量運動</h4><p>從1月4號開始，一週運動4天，週休2日的計畫必須開始!!</p><h4>4. 工作內容</h4><p>有人覺得我很喜歡寫程式、喜歡DS。</p><p>只有我是這樣說服自己的，說服自己接受討厭的東西只是取得最終更大利益的過程。</p><p>現在我不相信這個，我只是個學美術的。</p><p>更高的視野能夠讓我選擇更快樂的人生。</p><p>可能在新手村磨劍跟在野外闖蕩都是徒勞，當對目標與全局有更高的認識後，才能讓我們更靠近想要的生活。</p><blockquote>最後虎頭蛇尾的結尾是，對今年忍受我的朋友，無論有沒有被我推走，我深感歉意，並且能跟朋友保證，明年會好好生活，當個溫暖的人。</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5c36ba2ed94e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[類別變數的信度指標]]></title>
            <link>https://medium.com/@shiuanshr/%E9%A1%9E%E5%88%A5%E8%AE%8A%E6%95%B8%E7%9A%84%E4%BF%A1%E5%BA%A6%E6%8C%87%E6%A8%99-4ea72434ec52?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/4ea72434ec52</guid>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Fri, 29 Apr 2022 17:35:12 GMT</pubDate>
            <atom:updated>2022-04-29T17:35:12.451Z</atom:updated>
            <content:encoded><![CDATA[<h3>- Cohen’s Kappa Coefficient</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FfOR_8gkU3UE%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DfOR_8gkU3UE&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FfOR_8gkU3UE%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ca78975ac50361e4405c9dae47ba592e/href">https://medium.com/media/ca78975ac50361e4405c9dae47ba592e/href</a></iframe><p>kappa係數值落於-1~1之間，為<strong>效標參照測驗之信度的</strong>信度指標，用來表示兩位Reviewer之間評分的一致程度，也可以用於當研究者有新的matrix指標時，想跟過去經典的Gold standard比較一致程度時使用。</p><p>公式: Kappa=(觀察機率-期望機率)/(1-期望機率)</p><ul><li>觀察機率就是雙方結果相同</li><li>期望機率是每次其中一方(己方)判定時，有多少可能性對方也判定結果與自己相同</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*Dk-rThA8Ekln-9d8If2ORg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*YYFefDAl6_p58mMQB_fsqA.png" /></figure><p>優點:</p><ul><li>具有優秀的inter-rater reliability (評分之一致性)</li><li>kappa值愈接近1，代表新方法與比較標的越一致</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/514/1*WIw-sLru-DjJm53xSiS5Qw.png" /></figure><h3>- Cronbach’s α</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*S05Ps-A3Xk44ZZ-DMUEEFA.jpeg" /></figure><p>想必各位一定聽過這個信度指標，因此這邊整理3個最容易被問到的問題:</p><p>(1). 甚麼時候要用?</p><p>(2). 怎麼計算?</p><p>(3). 如何Interpret 分析結果</p><h3>甚麼時候要用?</h3><p>一般情況，我們無法直接衡量某個假說想驗證的抽象目標時(latent variable)(例如心情的改變)，我們會透過問卷內一組一組的問題，針對同一個概念讓受測者選填，常見的方式是李克特量表等。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/615/1*DwauyIOOLHz_y-X-ahtk6w.png" /></figure><p>假設，以上四題都是在測試心情改變程度，我們會期待它們的結果具備高度的相關性(Highly correlated)，也就是內部一致性高(Internal consistency reliability)，而如何衡量內部一致性，便是Cronbach’s α的工作。</p><blockquote>簡單來說，測量內部一致性時使用</blockquote><h3>(2). 怎麼計算?</h3><p>先上公式:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*sKbtt0xxYs_JqbMPqoyIgQ.png" /></figure><p>如果看不懂，沒關係，因為我們不會手算。</p><p>R 的指令是alpha(df)，對於不會寫R、Python、excel、SPSS的朋友，Datatab提供很好可以用的平台。</p><p>(3). 如何Interpret 分析結果</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FW9uPvAmtTOk%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DW9uPvAmtTOk&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FW9uPvAmtTOk%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/927f8d600f95bf2379cf5f1e04a59e29/href">https://medium.com/media/927f8d600f95bf2379cf5f1e04a59e29/href</a></iframe><p>在基礎研究中，信度至少應達到 0.80 才可接受。</p><p>在探索性研究中，信度至少須達到 0.70。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4ea72434ec52" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[BACS] Applied Regression]]></title>
            <link>https://medium.com/@shiuanshr/bacs-applied-regression-%E7%AD%86%E8%A8%98-c1d76fd363fd?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/c1d76fd363fd</guid>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Fri, 29 Apr 2022 14:47:53 GMT</pubDate>
            <atom:updated>2022-05-15T01:05:41.622Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>課程筆記清大雷松亞教授的<strong>計算統計於商業分析，</strong>我不具有任何課程材料的知識產權。若有錯誤，是筆者個人所學淺薄，歡迎指正，讓我能從中進步。</blockquote><h3>標準化</h3><h3><strong>1. 直覺與解釋</strong></h3><blockquote><strong>了解何者參數應該標準化，哪些不應該。</strong></blockquote><blockquote><strong>並且，應該清楚認知，標準化會使各feature失去&quot;量詞&quot;</strong></blockquote><blockquote>ex. 把時間標準化- &gt; 大多數沒什麼意義，而且喪失解讀性</blockquote><blockquote>ex. 把dummy variable標準化，首先大多對預測幫助不大，又喪失解讀性</blockquote><ul><li><strong>沒有標準化變量的解釋方法是</strong></li></ul><p>每x變一特定單位(例如per pound)，y會改變 (看slope正負) (例如.kg其單位)</p><ul><li><strong>標準化的變量解釋方法是:</strong></li></ul><p>每x的標準差改變1%，y的標準差改變xx(看slope)</p><h3>2. Linear Regression</h3><p>2–1. 前提:</p><blockquote>殘差符合隨機test, 常態test</blockquote><p>否則，會有特定部分低估或高估問題 (通常首尾無法fit得很好) ，因此每次做lm都要看residual density plot來決定進一步的shapiro</p><p>3–2. 學會用segment() + predict() 視覺化residual</p><h3>3. 圖永遠不會騙人(比起coefficiency)</h3><p>如果我們資料為非線性，可以進行轉換使其符合linear，但是:</p><ul><li>結果之後要轉換回去</li><li>會損失一些資訊 類似降維</li><li>每個資料點必須都同時受到同一個轉換方式，不能對特定資料點單獨進行操作，例如log() (但是會降低解釋性，之後也不能這樣解釋資料)</li></ul><h3>4. Transformation:</h3><p>時常我們遇到資料不常態時，我們會想要將其normalize，也就是試圖取得其邊際分布(marginal distribution)，並試圖讓其常態。</p><p>通常許多統計方法的假設是機率分布為常態，而非邊際機率分布為常態，ex, ANOVA假設 residual須為常態.</p><p>並且，我們要思考變數若進行轉換，例如log轉換得到的邊際 (%)是否合乎該變量代表的意義，例如，該變量為天數，log天數，，研究者該思考 1%天是否具有解釋意義? 並且，資料中有0(天)或是近似於0的值的話該如何處理(是否該無條件取代成1?加一常數 (c )再取成對數?)</p><p>因此，如果會使資料解釋失去意義的轉換是沒有必要並應該避免的。同時，當統計方法的假設為達到時，也應該避免透過大量手段強硬使其符合先定假設，例如ANOVA需要服從常態性，不探究細節就對資料本質進行轉換是錯誤的行為，這時可以考慮其他assumptions的統計方法，例如quantile regression, robust regression, regression trees等等。</p><blockquote>切記，不要一昧地追逐常態(normality)而失去Power</blockquote><h4>4.1. Log Transformation</h4><h4>- Q1. When to use:</h4><p>資料skewness時，把她bent集中一點</p><p>想像兩手用力往下扳一根湯匙</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*3G60-i1c4plSOEuCot3hdA.png" /><figcaption>原本資料X長這副德性</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*v9y2oqqjU69rb2SQAv3R7Q.png" /><figcaption>用log扳回來的資料X</figcaption></figure><h4>Q2. 但是究竟要對aX+b = Y的誰做LOG?log(X)?log(y)?還是lm(log(y)~log(x))?</h4><blockquote>好問題!!答案是，看何時能使residual更符合常態性或是R更大(不增加feature情況下)</blockquote><h4>Q3. 要如何解釋轉換過的模型?</h4><h3>Scenaro 1: Log Both 代表百分改變</h3><p><strong>lm(log(y)~log(x))</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/766/1*Mg04hKlqsjB0qdsSqeEthg.png" /></figure><blockquote>% change in X leads to β% change in Y</blockquote><blockquote>馬力改變1%，會使mpg減少0.8418%</blockquote><h3><strong>Scenaro 2: 對x log</strong></h3><p>lm(y ~log(x))</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*jshkp9vGZH8hJEfb5ZMuIw.png" /></figure><blockquote>1% change in X leads to β/100 units change in Y</blockquote><blockquote>1% change in 馬力 leads to -18.58/100 units change in mpg (per gallon)</blockquote><h3><strong>Scenaro 3: 對y log</strong></h3><p>lm(log(y) ~x)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*_3zpmAoe3ODKiMEaaIKDRg.png" /></figure><blockquote>1 unit changes in X leads to β*100 % change in Y</blockquote><blockquote>馬力每增加1匹，mpg減少 (0.007334 * 100)%</blockquote><blockquote><strong>Summary:</strong></blockquote><blockquote>總之對誰做log ，解釋時，誰就變%</blockquote><h3>5. 多重共線性Multicollinearity</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/323/1*wMkMGaTj6e0zxNP5uA26Ng.png" /></figure><p>當多元線性方程式的自變數β1與自變數β2高度正相關 (correlation 接近1)，</p><p>linear model無法判別究竟是β1還是β2對Y造成影響，因此將credit都給稍微correlation較高的那一個自變數。</p><p>因此，當有新的獨立變數加入lm model時，</p><ul><li>改變coefficient的size</li><li>改變標準差, t- value, p-value</li></ul><h3>5–1. Diagnosing Multicollinearity?</h3><p>計算x2對其他自變數的 VIF (Variance Inflation Factor)</p><blockquote>x2_regression &lt;- lm(x2~ other x, data= dataset, na.action = na.exclude)</blockquote><blockquote>x2_r2 &lt;- summary(x2_regression)$r.squared</blockquote><blockquote>x2_vif &lt;- 1/(1–x2_r2)</blockquote><blockquote>x2_vif</blockquote><p>通常VIF &gt;4~5或是 sqrt(VIF) &gt;2 表示該獨立變數與其他自變數共線超過50%，but why sqrt(VIF) &gt;2 ?</p><p><strong>Google for yourself.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c1d76fd363fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Feature Engineering特徵工程- Matrix 評價指標- 學習紀錄]]></title>
            <link>https://medium.com/@shiuanshr/feature-engineering%E7%89%B9%E5%BE%B5%E5%B7%A5%E7%A8%8B-matrix-%E8%A9%95%E5%83%B9%E6%8C%87%E6%A8%99-%E5%AD%B8%E7%BF%92%E7%B4%80%E9%8C%84-dfa850658177?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/dfa850658177</guid>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Fri, 29 Apr 2022 08:26:35 GMT</pubDate>
            <atom:updated>2022-04-29T17:38:34.901Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/577/0*5AYr0Qn3cYdJeQKL.jpg" /></figure><p>簡單紀錄常見的評價指標:</p><ul><li>混淆矩陣</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*uyl3PChdcgSoXken" /><figcaption><a href="https://www.csdn.net/tags/MtTaIg4sNTY2OTYyLWJsb2cO0O0O.html">https://www.csdn.net/tags/MtTaIg4sNTY2OTYyLWJsb2cO0O0O.html</a></figcaption></figure><p>而關於Precision, Recall, 和Accuracy(ACC)公式背法:</p><p>這邊紀錄幾張網路上得來的整理:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*C9qY8MSanko1trWS.png" /><figcaption><a href="https://geek.digiasset.org/pages/nlp/nlpinfo/ml-precision-recall/">https://geek.digiasset.org/pages/nlp/nlpinfo/ml-precision-recall/</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/0*PT211Au-X5uhlFIE.png" /><figcaption><a href="https://en.wikipedia.org/wiki/Statistical_population">https://en.wikipedia.org/wiki/Statistical_population</a></figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/541/1*JRUMGzuVDmfz0QRy5Xr3kw.jpeg" /></figure><h3>ROC（Receiver Operating Characteristic) 曲線</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/1*JQtrbgaWBP01Lhr9gOUJCw.png" /><figcaption>如果綠線低於<strong>45度對角線</strong>，落在右下三角形，還不如擲銅板瞎猜</figcaption></figure><p>綠線為ROC曲線，綠色線下面積為AUC，AUC越大，線越靠近左上角越好。</p><p>比單純看precision與Recall等衡量指標好，但是若資料不平衡，仍然可能高估表現。</p><blockquote><em>有任何有問題，歡迎透過LinkedIn或是Email聯繫我。</em></blockquote><blockquote><em>如果你喜歡這篇文章，幫我拍手追蹤，我會更有動力撰文 !</em></blockquote><blockquote><em>感謝你的閱讀。</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dfa850658177" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Feature Engineering特徵工程- 資料缺失處理 -1 學習筆記]]></title>
            <link>https://medium.com/@shiuanshr/%E7%89%B9%E5%BE%B5%E5%B7%A5%E7%A8%8B-missing-data%E8%B3%87%E6%96%99%E7%BC%BA%E5%A4%B1%E8%99%95%E7%90%86-1-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-15ce56b0adbf?source=rss-30ea9c5a5779------2</link>
            <guid isPermaLink="false">https://medium.com/p/15ce56b0adbf</guid>
            <dc:creator><![CDATA[靈芝 LING-ZHI]]></dc:creator>
            <pubDate>Fri, 29 Apr 2022 07:37:52 GMT</pubDate>
            <atom:updated>2022-04-29T07:52:44.737Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Hkwjq0sen86Lcck3Cr-rUA.jpeg" /></figure><h3>▲ Question: 為甚麼需要討論Missing Data:</h3><blockquote>原因一，怕資料受缺失的資料影響，降低統計上的檢力（power），使得標準誤變大，而失去代表性。</blockquote><blockquote>Ex. 如果你直接使用別人寫的package，許多分群或是聚類的演算法package (例如，線性回歸、SVM…)會直接將缺失值不計入計算（listwise deletion）。但是，這時候你的sample size 就降低了，隨機抽樣的樣本代表性可能受到破壞，如此一來，如果我們繼續傻傻地用既有的資料寫論文，在口試當天，就很可能被挑戰～</blockquote><blockquote>原因二，訓練模型時，不優質的訓練資料品質導致模型拙劣</blockquote><h3>▲ Mechanism of missingness 造成缺失值的機制</h3><p>在處理缺失值前，必須先探討該缺失值是屬於甚麼類型。</p><p>統計學家將造成缺失資料的機制分成三大類：</p><ul><li>Missing completely at random (MCAR)</li><li>Missing at random (MAR)</li><li>Not missing at random (NMAR)</li></ul><h4>MCAR</h4><p>觀察到的資料或是缺失的資料，彼此關係是獨立可控的，屬於可忽略的（ignorable）缺失機制。</p><p>這種情況相當少見，該缺失值不破壞資料分布形狀，也不減少樣本代表性，若是資料遺失較少，可以直接進行listwise deletion.</p><h4>MAR</h4><p>這是所謂系統因素造成的缺失，缺失資料的發生與觀察到的資料有關，也就是大多數會遇到的情況之一，例如，</p><p>在進行實驗時，私生活特別豐富的朋友比較不願意坦白回答個人的性伴侶數量。</p><p>因此，通常我們會透過控制變因，使個人價值觀納入實驗考量的因素，利用排除或是適合的randomize方式，使其缺失變成MCAR，如此，既使仍有缺失值，也能直接比照listwise deletion，在不降低樣本代表性情況排除缺失值。</p><p>但是，若是使用二手資料進分析呢? 請大家耐心繼續看下去~</p><h4>NMAR</h4><p>如果資料缺失不是前面兩者，這種狀況是屬於不可忽視的(Non-ignorable)</p><p>這種情況必須深入研究資料缺失原因為何。</p><h3>▲如何進行缺失值處理?</h3><p>先附上總結:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*v31669qYMDlCi_0x1GnqGg.jpeg" /><figcaption><a href="https://blog.csdn.net/weixin_33672838/article/details/113711925">https://blog.csdn.net/weixin_33672838/article/details/113711925</a></figcaption></figure><p>一般來說，當遇到缺失值時，我們會先以最糟糕的情況去猜測(NMAR)，再隨著我們對該缺失值背後機制的診斷，逐漸上調我們的猜測(NMAR⇾MAR)</p><p>以下介紹各種方法的使用情境:</p><h3>1. 刪除法Deletion</h3><p>這是最常見的方式，又分為List-wise deletion與Pair-wise deletion</p><blockquote><strong><em>(1–1). List-wise deletion</em></strong></blockquote><blockquote>一筆觀察值(單一列)可能具備多個feature(多欄位)，所謂 List-wise deletion就是只要具備缺失值，整筆資料刪除，不納入分析，又名Complete case method。</blockquote><blockquote>應用情境:</blockquote><blockquote>在MCAR時才能使用，因為該方法會造成樣本數損失。</blockquote><blockquote><strong><em>(1–2). Pair-wise deletion</em></strong></blockquote><blockquote>表示只有要使用到該特徵時，若該特徵具有缺失值，才會不計入考量。</blockquote><blockquote>但是會造成各個特徵值可用的樣本數(row數)不同，又名Available Case Method。</blockquote><h3>2. 阡插法（imputation）</h3><p><strong><em>(2–1). Single Imputation 單次插補法</em></strong></p><p>只用一次阡插，產生一組新的數據</p><p><strong><em>(2–2). 🔥Multiple Imputation 多重插補法</em></strong></p><p>用多次(通常2–6) 阡插，產生多組新的數據，再將結果合併計算。</p><p>看做是多個Single Imputation。</p><p>而每次插捕的值可以不同，根據以下條件:</p><blockquote>平均數插補法 (Mean Imputation)</blockquote><blockquote>假如變數是高斯分布(常態分布)- 用平均數(mean)來插補；</blockquote><blockquote>缺點是會使樣本變異數下降，若是缺失比例過大，整體機率分布的shape會變高聳集中的高狹峰分配(Leptokurtic)</blockquote><blockquote>中位數插補法 (Median Imputation)</blockquote><blockquote>假如變數是偏態(Skew)分布，用來插補中位數(median);</blockquote><blockquote>常見於死亡率、收入。</blockquote><blockquote>眾數插補法 (Mode Imputation)</blockquote><blockquote>對於categorical variables而言，以眾數(mode)來插補;</blockquote><p><strong>2–3. 邏輯回歸插補法(logistic regression imputation, LR)</strong></p><p>(1). 離散型變數的缺失資料插補</p><p>(2). 適用於MAR，觀測值與缺失值具備相依關係</p><p>(3). 符合單調缺失（Monotone missing）</p><h3><strong>2–4. 迴歸插補法（Regression imputation）</strong></h3><blockquote>應用情境:</blockquote><blockquote>(1). 連續型變數的缺失資料插補</blockquote><blockquote>(2). MAR 缺失值與觀測到的資料具備相依關係</blockquote><blockquote>(3). 符合單調缺失（Monotone missing）</blockquote><h3><strong>2–4. 🔥KNN</strong></h3><blockquote>根據資料類型所設定距離的衡量方式，再依照該距離找出K個近鄰的點，求其均值來插補。</blockquote><blockquote>而根據數據類型，</blockquote><blockquote>— 連續型資料採用 歐幾里德距離、餘弦距離</blockquote><blockquote>— 類別資料採用漢明距離</blockquote><p>(詳細參閱<a href="https://kknews.cc/tech/95q4rvq.html">https://kknews.cc/tech/95q4rvq.html</a>)</p><blockquote>優點:</blockquote><blockquote>— 可以處理某些形狀特別的數據</blockquote><blockquote>缺點:</blockquote><blockquote>— 不適用於高維度資料，易有維度詛咒問題</blockquote><blockquote>— 非常耗時</blockquote><h3><strong>2–5. Random Forest (</strong>RF)</h3><p>Leo Breiman大師提供的經典演算法，如何利用RandomForest預測的功能來填補遺失值，我個人覺得以下的影片詳述的非常好</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FsQ870aTKqiM%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DsQ870aTKqiM&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FsQ870aTKqiM%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff8c34088fc25713df2c1cbb6451406a/href">https://medium.com/media/ff8c34088fc25713df2c1cbb6451406a/href</a></iframe><h3><strong>2–6. XGBoost</strong></h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FOtD8wVaFm6E%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DOtD8wVaFm6E&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FOtD8wVaFm6E%2Fhqdefault.jpg&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9841f1786cc2dc0b9860d5f28c6177d2/href">https://medium.com/media/9841f1786cc2dc0b9860d5f28c6177d2/href</a></iframe><p><a href="https://www.youtube.com/watch?v=8b1JEDvenQU">https://www.youtube.com/watch?v=8b1JEDvenQU</a></p><p>ML 模型百百種，基本上根據Dataset大小</p><ol><li>數據量很小，用Naive Bayes，假設變量彼此獨立，進行貝葉氏機率計算</li><li>數據量適中或者較大，用Tree Based Model, 例如XGboost, RF</li><li>數據量較大，NN, CNN</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15ce56b0adbf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>