<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>standing-o.log</title>
        <link>https://velog.io/</link>
        <description>공부를 마무리 할 때까지  👉👉https://standing-o.github.io/👈👈</description>
        <lastBuildDate>Fri, 09 Dec 2022 07:56:10 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <image>
            <title>standing-o.log</title>
            <url>https://velog.velcdn.com/images/standing-o/profile/b7a75a91-4d59-44d2-ba62-d04a50cff1be/image.png</url>
            <link>https://velog.io/</link>
        </image>
        <copyright>Copyright (C) 2019. standing-o.log. All rights reserved.</copyright>
        <atom:link href="https://v2.velog.io/rss/standing-o" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[2022 구글 클라우드 스터디잼 수료 후기 | AI&ML 중급/심화, 쿠버네티스 입문/중급/심화 과정]]></title>
            <link>https://velog.io/@standing-o/google-cloud-study-jam</link>
            <guid>https://velog.io/@standing-o/google-cloud-study-jam</guid>
            <pubDate>Fri, 09 Dec 2022 07:56:10 GMT</pubDate>
            <description><![CDATA[<ul>
<li>본 포스팅은 2022 구글 클라우드 스터디잼의  AI&amp;ML 중급/심화, 쿠버네티스 입문/중급/심화 과정에 참가하여 배운 내용과 느낀 점을 정리한 글입니다.</li>
<li>👉 <a href="https://standing-o.github.io/posts/google-cloud-study-jam/"><code>Click</code></a></li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/43dddd91-7053-4b8e-9213-b4da32edb019/image.jpeg" width=75% height=75% style="margin-left: auto; margin-right: auto; display: block;">


<h1 id="2022-구글-클라우드-스터디잼-이란"><strong>2022 구글 클라우드 스터디잼 이란?</strong></h1>
<ul>
<li><a href="https://sites.google.com/view/studyjam-kr/home?authuser=0"><code>2022 Google Cloud Study Jam</code></a> 은 주최에서 제공하는 Qwiklabs(실습)쿠폰 및 Coursera 강의를 활용하여 Google Cloud Platform (GCP) 에 입문할 수 있도록 하는 온라인 학습 프로그램 입니다.</li>
<li>2022년에는 AI&amp;ML 입문/중급/심화, 쿠버네티스 입문/중급/심화 총 여섯 개의 과정으로 진행됐습니다.</li>
<li>정해진 수료 조건을 달성하면 Cloud Study Jam 완주 기념품을 증정합니다. </li>
</ul>
<h1 id="신청하게-된-이유"><strong>신청하게 된 이유</strong></h1>
<ul>
<li>실제 머신러닝 관련 업무는 서버에서 작업하게 된다는 사실을 알게 된 후 GCP, AWS와 같은 클라우드 서비스에 관심을 갖게 됐습니다. 클라우드를 공부하고 있었지만 실제로 실습을 해보거나 관련 프로젝트에 참여해본 경험이 없기에, 좋은 경험이 될 것 같아 신청하게 됐습니다!</li>
<li>특히 Qwiklabs 실습을 할 수 있는 Credit을 제공한다는 점이 좋았습니다.</li>
</ul>
<h1 id="수료-과정"><strong>수료 과정</strong></h1>
<h2 id="1-ai--ml-중급-과정"><strong>1. AI &amp; ML 중급 과정</strong></h2>
<ul>
<li><p>Qwiklabs 2개의 퀘스트를 완료하는것이 수료 조건이었는데요, 저는 <a href="https://www.cloudskillsboost.google/quests/23">Google Cloud Essentials</a>, <a href="https://www.cloudskillsboost.google/quests/84">Advanced ML: ML Infrastructure</a> 를 수강했습니다.</p>
<ul>
<li>구글 클라우드의 기본적인 툴과 서비스를 경험 할 수 있었고, 클라우드에 대한 지식이 없어도 쉽게 공부할 수 있을정도로 설명이 쉽고 자세했습니다. 대부분의 실습은 Cloud Shell command로 진행됐습니다.</li>
</ul>
</li>
<li><p>Qwiklabs Advanced 추천 코스</p>
<p>➔ Google Developer Essentials, Advanced ML: ML Infrastructure, Build and Deploy Machine Learning Solutions on Vertex AI, Machine Learning APIs, Integrate with Machine Learning APIs, Data Science on Google Cloud: Machine Learning</p>
</li>
</ul>
<h2 id="2-ai--ml-심화-과정"><strong>2. AI &amp; ML 심화 과정</strong></h2>
<ul>
<li><p>중급과정과 같은 수료 조건이었기에 <a href="https://www.cloudskillsboost.google/quests/83">Intermediate ML: TensorFlow on Google Cloud</a>, <a href="https://www.cloudskillsboost.google/quests/33">Baseline: Infrastructure</a> 를 수강했습니다.</p>
<ul>
<li>구글 클라우드 환경에서 텐서플로우를 활용하여 머신러닝 모델을 build, train, deploy 하는 방법을 공부했습니다.</li>
<li>클라우드 저장소와 함수 등 다양한 어플리케이션 서비스들을 배웠습니다.</li>
</ul>
</li>
<li><p>Qwiklabs Advanced 추천 코스</p>
<p>➔ Google Developer Essentials, Baseline: Data, ML, AI, Advanced ML: ML Infrastructure, Data Science on Google Cloud, ML Pipelines on Google Cloud, Google Cloud Solutions II: Data and Machine Learning</p>
</li>
</ul>
<h2 id="3-쿠버네티스-입문반"><strong>3. 쿠버네티스 입문반</strong></h2>
<ul>
<li><a href="https://www.qwiklabs.com/quests/29">Kubernetes in the Google Cloud</a> 퀘스트를 완료하는 것이 수료 조건입니다.<ul>
<li>Kubernetes는 컨테이너 배포관리 (container orchestration) 시스템 중 하나입니다. 구글 클라우드 환경에서 Kubernetes를 deploy하는 방법을 배웠습니다. </li>
<li>처음 보는 개념이라 생소했지만 설명이 친절했고, 수료 조건도 간단해서 부담없이 수강할 수 있었습니다 :)</li>
</ul>
</li>
</ul>
<h2 id="4-쿠버네티스-중급반"><strong>4. 쿠버네티스 중급반</strong></h2>
<ul>
<li><a href="https://www.coursera.org/learn/google-kubernetes-engine">Getting Started with Google Kubernetes Engine</a> 코세라 강의를 완주하는것이 수료 조건입니다.<ul>
<li>Google Cloud Computing, Container, Google Kubernetes Engine (GKE), Kubernetes 아키텍처 등을 공부했습니다.</li>
<li>평소 코세라에 익숙하기도 했고, 강좌에 Qwiklab실습이 포함되어있어 편하게 수강했습니다. </li>
</ul>
</li>
</ul>
<h2 id="5-쿠버네티스-심화반"><strong>5. 쿠버네티스 심화반</strong></h2>
<ul>
<li><p>세 개의 코세라 강좌 <a href="https://www.coursera.org/learn/foundations-google-kubernetes-engine-gke">Architecting with Google Kubernetes Engine: Foundations</a>, <a href="https://www.coursera.org/learn/deploying-workloads-google-kubernetes-engine-gke">Architecting with Google Kubernetes Engine: Workloads</a>, <a href="https://www.coursera.org/learn/deploying-secure-kubernetes-containers-in-production">Architecting with Google Kubernetes Engine: Production</a>를 수강하고, <a href="https://www.cloudskillsboost.google/quests/45">Kubernetes Solutions</a> Qwiklabs 실습을 완료하는것이 수료 조건입니다. </p>
<ul>
<li><p>초반 Foundation 강의에는 중급반 강의와 겹치는 내용이 있었고, 후반에는 Role-based access control, GKE Networking, Persistent storage, Secrets and Configmaps 등 심화적인 내용들이 포함됩니다.</p>
</li>
<li><p>다른 과정에 비해 어렵고 확실히 수강할 강좌도 많아 힘들었지만, 내용이 잘 이어지고 알찼기에 가장 남는게 많은 프로그램이었던것 같습니다!</p>
</li>
</ul>
</li>
</ul>
<ul>
<li>다섯 개 과정을 통해 얻게된 Qwiklab 뱃지들과 코세라 수료증들.. 모아두고 보니 뿌듯하네요 😂😂</li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/3208d307-f8c2-4050-9658-f512d674d561/image.png" width=75% height=75% style="margin-left: auto; margin-right: auto; display: block;">
<img src="https://velog.velcdn.com/images/standing-o/post/c631298d-f43e-49ca-ac22-b89fdbba0224/image.png" width=75% height=75% style="margin-left: auto; margin-right: auto; display: block;">




<h1 id="기념품"><strong>기념품</strong></h1>
<ul>
<li>총 다섯 개의 과정에 참여하고 다섯 개의 기념품을 받게 되었습니다. 😁😁 감사합니다!</li>
<li>Google Developer가 적힌 티셔츠 2장, 가방, 파우치, 모자를 받았습니다 :)</li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/c1d6a310-5e36-4b33-88a5-63f9c19b2a70/image.jpg" width=75% height=75% style="margin-left: auto; margin-right: auto; display: block;">


<h1 id="후기"><strong>후기</strong></h1>
<ul>
<li>무엇보다도 많은 실습을 통해 GCP 환경에 익숙해졌다는 것이 가장 큰 성과라고 생각합니다. 처음엔 실습에서 오류가 뜨면 쩔쩔매고 다시 시작하는 과정을 반복했었는데, 이제는 오류도 잘 고칠 수 있게 되었습니다. 😎😎</li>
<li>스터디 잼은 그룹 단위로 신청이 가능합니다! 저는 개인적으로 진행했지만 팀을 구성하여 함께 공부한다면 더 도움이 될 듯 합니다.</li>
<li><a href="https://www.facebook.com/groups/studyjamkorea">Google Study Jams in Korea</a> 페이스북 페이지에서 항상 소식이 올라오니 참고하시면 좋을것 같습니다.</li>
<li>좋았던 기억밖에 없어서 기회가 된다면 내년에도 참여하려고 합니다!</li>
</ul>
]]></description>
        </item>
        <item>
            <title><![CDATA[2022 데이터 크리에이터 캠프 최우수상 수상 후기]]></title>
            <link>https://velog.io/@standing-o/data-creator-camp-grand-prize</link>
            <guid>https://velog.io/@standing-o/data-creator-camp-grand-prize</guid>
            <pubDate>Thu, 08 Dec 2022 13:00:10 GMT</pubDate>
            <description><![CDATA[<ul>
<li>본 포스팅은 2022 데이터 크리에이터 캠프에 참가하여 배운 내용과 느낀 점을 정리한 글입니다.</li>
<li>👉 <a href="https://standing-o.github.io/posts/data-creator-camp-grand-prize/"><code>Click</code></a></li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/6a04f8d3-5b11-4ac7-93d5-53bfb84c951d/image.png" width=75% height=75% style="margin-left: auto; margin-right: auto; display: block;">


<ul>
<li>DAMS (Data Analysis Math Statistics) 팀 최우수상 🎉🎉 감사합니다 👍👍</li>
</ul>
<hr>
<h1 id="데이터-크리에이터-캠프란"><strong>데이터 크리에이터 캠프란?</strong></h1>
<ul>
<li><p><code>데이터 크리에이터 캠프</code>는 실제 비즈니스 환경의 문제를 데이터 분석 교육 및 멘토링을 통해 해결해 보는 데이터 분석대회입니다.</p>
</li>
<li><p>온라인 사전 학습을 제공하고 4주간의 해커톤 예선을 진행하는데 이때, 매주 멘토님에게 튜터링을 받을 수 있는 기회를 제공합니다.</p>
</li>
<li><p>과학기술정보통신부와 한국지능정보사회진흥원이 주최하며 약 2달 동안 진행됩니다.</p>
</li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/b485158e-5182-4f65-ae93-24c3c9f8a25b/image.png" width=75% height=75% style="margin-left: auto; margin-right: auto; display: block;">





<h1 id="신청하게-된-이유"><strong>신청하게 된 이유</strong></h1>
<ul>
<li>평소 저는 캐글/데이콘과 같은 머신러닝의 예측 성능을 높이는 대회에 참여해왔습니다. 리더보드에서 알 수 있는 등수와 별개로 제가 사용하는 모델과 방법론들을 이 문제에 사용하는 것이 타당한지, 옳은 근거인지가 매번 궁금했었는데요, 그렇기에 데이터 크리에이터 캠프에서 매주 멘토링을 제공한다는 점이 매력적이었습니다.</li>
<li>무엇보다도 잘 맞는 친구들과 한 달 넘게 같이 팀 프로젝트를 할 기회가 앞으로는 거의 없을 것 같아서 신청했습니다! 😀😀</li>
</ul>
<h1 id="진행-방식"><strong>진행 방식</strong></h1>
<h2 id="1-온라인-연수원을-통한-사전-학습"><strong>1. 온라인 연수원을 통한 사전 학습</strong></h2>
<ul>
<li>전반적인 인공지능 역사부터 머신러닝/딥러닝 개념까지 학습할 수 있는 온라인 강의를 제공합니다.</li>
</ul>
<h2 id="2-예선-문제-해결하기"><strong>2. 예선 문제 해결하기</strong></h2>
<ul>
<li><p>예선 문제는 아래와 같이 크게 세 가지로 나눠 집니다. 일러스트와 실사가 섞인 이미지 데이터를 제공받았습니다.</p>
<ul>
<li>EDA 과정을 통해 데이터 분포 문제 해결하기.</li>
<li>학습 데이터에서 실사 영상 제거하기 (비지도 학습).</li>
<li>일러스트 영상 분류하기 (지도 학습).</li>
</ul>
</li>
<li><p>저희 팀은 일주일에 두 번 정도 회의를 진행했는데요, 회의 때 역할 분담을 하고 다음 회의에서 분석 결과를 공유하는 식으로 진행했습니다.</p>
</li>
<li><p>Notion 페이지를 활용하여 회의 내용 및 진행 상황을 팀원들과 공유하고, 멘토링을 위한 발표 자료를 기록했습니다.</p>
<ul>
<li>아래와 같이 진행 상황과 모델 학습계획을 정리했습니다.</li>
</ul>
</li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/f0d5bc6b-193c-4a2c-a029-e9bc7707ff57/image.png" width=80% style="margin-left: auto; margin-right: auto; display: block;">

<img src="https://velog.velcdn.com/images/standing-o/post/41045367-1aee-43da-a900-c231ad46e823/image.png" width=80% style="margin-left: auto; margin-right: auto; display: block;">


<ul>
<li><p>주최 측에서 Colab Pro 환경을 제공 해주셔서 더 편하게 모델링 할 수 있었습니다.</p>
</li>
<li><p>마지막으로 소스 코드와 PPT, 최종 모델 가중치를 제출했습니다.</p>
<ul>
<li>제출 일주일 전 최종 모델을 채점할 때 사용하는 테스트 코드를 주최 측에서 제공해 주시는데요, 저희 팀에서 테스트 코드 오류를 먼저 발견하고 오류 레포트를 작성하여 주최 측에 보냈습니다. 😂😂</li>
<li>다행히도 오류가 맞아서 수정된 코드를 다시 받았습니다. 사실 저희가 틀린 것 같아서 조마조마 했었는데, 지나고 보니까 더 다양한 실험과 시도를 해보게 된 것 같아 저희 팀을 더 성장하게 해준 일이었던 것 같습니다 :)</li>
</ul>
</li>
</ul>
<h2 id="3-멘토링-활용하기"><strong>3. 멘토링 활용하기</strong></h2>
<ul>
<li><p>매주 토요일마다 담당 멘토님에게 미션을 수행하면서 궁금했던 점을 질문했습니다. </p>
</li>
<li><p>Notion 페이지에 궁금한 점도 따로 적어두고 질문했습니다 :)</p>
</li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/47300a7b-3912-4d52-94ca-d3e70184c289/image.png" width=80% style="margin-left: auto; margin-right: auto; display: block;">


<ul>
<li><p>발표 잘 들어주시고 좋은 피드백 해주신 조희승 멘토님! 감사드립니다 😀😀</p>
</li>
<li><p>멘토링 데이 외에도 매일 QnA 게시판에 질문 하나 씩 할 수 있어서 유용하게 활용했습니다. </p>
</li>
</ul>
<h2 id="4-본선"><strong>4. 본선</strong></h2>
<ul>
<li>예선 일주일 후 본선 진출 팀임을 공지 받았습니다. 🎉🎉</li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/dff20a31-78ca-4a47-8b30-1ca11180196c/image.png" width=80% style="margin-left: auto; margin-right: auto; display: block;">


<ul>
<li><p>마지막으로 발표 자료와 대본을 점검하고, 예상 질문 및 모델 개념을 공부했습니다. </p>
<ul>
<li><p>특정 방법론과 모델을 활용한 근거와 구체적인 학습 방법을 위주로 정리하여 발표 디펜스를 대비했습니다.</p>
<p>➔ (EX) L2정규화와 드롭아웃을 통해 과적합이 어느정도 보완되었나요?, 왜 기존 VGG16보다 새로 제안한 모델이 더 빠르게 수렴되나요?</p>
</li>
<li><p>학습 데이터에서 실사 영상을 제거할 때 Self supervised learning 을 활용했는데요, 이에 대한 survey 논문을 정리하여 팀원들에게 공유하고 함께 공부했습니다.</p>
</li>
</ul>
</li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/9d3cc163-6127-4bc3-8127-f64d4fdb237d/image.png" width=80% style="margin-left: auto; margin-right: auto; display: block;">



<ul>
<li>본선은 한국지능정보사회진흥원 스마트 스퀘어에서 오후 1시에 시작했습니다. 발표 순서는 당일 추첨으로 결정됐고, 저희는 대학부 7번째로 발표했습니다 :)</li>
</ul>
<p><img src="https://velog.velcdn.com/images/standing-o/post/c837fcd1-4799-4261-bcc1-7651b3b419c4/image.jpg" width=50%><img src="https://velog.velcdn.com/images/standing-o/post/3fc19568-e9a7-4970-bf31-3a95fde568d8/image.jpg" width=50%></p>
<ul>
<li>발표 시간은 5분으로 짧았지만 다행히 발표자 친구가 모든 내용을 잘 발표해 주었습니다. 😂😂 발표가 끝나면 팀원들 전체가 무대 위로 올라가서 심사위원 분들께 질문을 받게 되는데, 운좋게도 예상 질문을 벗어난 질문이 없었기에 논리적으로 잘 대답했던 것 같습니다.</li>
<li>5시 정도에 시상식이 진행되었는데 정말 너무 떨리더라구요.. <strong>DAMS 팀은 최우수상을 수상하게 되었습니다!</strong> 감사합니다 🥳🥳</li>
</ul>
<img src="https://velog.velcdn.com/images/standing-o/post/50a93e30-fa6c-48c8-9cfc-2036b0d1166b/image.jpg" width=80% style="margin-left: auto; margin-right: auto; display: block;">




<h1 id="배우고-느낀-점"><strong>배우고 느낀 점</strong></h1>
<ul>
<li><p>먼저 멘토링을 통해 많이 배웠습니다. 어떤 문제를 풀기 위해 내가 생각하는 방법론을 제안해야할 때, EDA와 모델링 실험 결과를 <strong>근거</strong>로 들어 설득하는 법을 배웠습니다. </p>
</li>
<li><p>최종 발표 내용에는 포함하지 않았지만, 사실 캠프를 진행하면서 해본 시도가 굉장히 많았습니다. 이러한 시도들을 통해 더 많이 배우게 된 것 같습니다.</p>
<p>(WARD, DBSCAN, K-means, CAE, Segmentation, AnoGAN, Efficientnet, ConvNext, Resnet, Desnet ...)</p>
</li>
<li><p>어쩌다 보니 제가 Self supervised learning 부분을 도맡아 하게 됐는데요, 덕분에 많이 배웠고 흥미로워서 추후에 여유가 되면 다른 대회에도 활용해볼까 고민 중입니다.</p>
</li>
<li><p>이렇게 해보면 좋지 않을까? 라고 막연하게 생각했던 것들을 직접 하나하나 시도해 보면서 지식으로 적립되었네요 :)</p>
</li>
</ul>
<h1 id="후기"><strong>후기</strong></h1>
<ul>
<li>열심히 준비해주신 운영진분들 덕분에 몇 달 동안 부담없이 편하게 참가했습니다. 무엇보다도 상을 받게 되어 더 뿌듯하고 즐거웠습니다! (다른 task의 문제로 캠프가 또 열리면 좋겠습니다!)</li>
<li>팀원 분들에게도 감사합니다! 두 달 동안 즐거웠고 너무 좋은 팀워크를 느낄 수 있었습니다 😀😀</li>
</ul>
]]></description>
        </item>
        <item>
            <title><![CDATA[Google Kubernetes 워크로드]]></title>
            <link>https://velog.io/@standing-o/google-kubernetes-workload</link>
            <guid>https://velog.io/@standing-o/google-kubernetes-workload</guid>
            <pubDate>Mon, 05 Sep 2022 12:26:19 GMT</pubDate>
            <description><![CDATA[<ul>
<li>본 포스팅은 Kubernetes의 배포와 포드 네트워킹, 볼륨에 대한 내용을 포함하고 있습니다.</li>
<li>Keyword : Kubernetes, GKE, deployment, pod networking, volume</li>
<li>👉 <a href="https://standing-o.github.io/posts/kubernetes-workload/"><code>Click</code></a></li>
</ul>
<h1 id="kubernetes-workload"><strong>Kubernetes Workload</strong></h1>
<h2 id="kubectl-command"><strong><code>kubectl</code> command</strong></h2>
<ul>
<li><code>Kubectl</code> : 관리자가 kubernetes cluster를 제어하는 데 사용하는 유틸리티</li>
<li><code>Kubectl</code> transforms your command-line entires into API calls<ul>
<li><code>kubectl</code>은 명령줄 입력 내용은 API 호출로 전환한 후 선택한 kubernetes cluster 내 kubeAPI 서버로 전송</li>
</ul>
</li>
<li>Use <code>kubectl</code> to see a list of Pods in a cluster<ul>
<li><code>kubectl get pods</code> : <code>kubectl</code>은 이 명령어를 API 호출로 전환하고 cluster 제어 영역 서버에서 HTTPS를 통해 <code>kubeAPI</code> 서버로 보냄</li>
<li><code>kubeAPI</code> 서버는 <code>etcd</code> 쿼리를 통해 요청을 처리 ➔ <code>kubeAPI</code> 서버는 HTTP를 통해 <code>kubectl</code>에 결과를 반환 ➔ <code>kubectl</code>은 API 응답을 해석하여 명령 프롬프트에서 관리자에게 결과를 표시</li>
</ul>
</li>
<li><code>kubectl</code> must be configured first<ul>
<li>Relies on a config file : <code>$HOME/.kube/config</code></li>
<li>Config file contains:<ul>
<li>Target cluster name, credentials for the cluster</li>
</ul>
</li>
<li>Current config: <code>kubectl</code> config view</li>
</ul>
</li>
<li>Connect to a google kubernetes engine cluster<ul>
<li><code>kubectl config view</code> : <code>kubectl</code> 명령어 자체의 구성에 대해 알려줌</li>
<li>gcloud 명령줄 도구와 <code>kubectl</code>을 설치한 다른환경에서 <code>get credentials gcloud</code> 명령어를 사용 : 사용자 인증 정보 가져오기</li>
</ul>
</li>
<li>The <code>kubectl</code> command syntax has several parts<ul>
<li>Type : 명령어가 적용되는 kubernetes 객체를 정의, command와 함께 사용되어 어떤 작업을 어떤 type의 객체에 수행하길 원하는지 <code>kubectl</code>에 알림</li>
<li>Name : type에 정의된 객체를 지정</li>
</ul>
</li>
</ul>
<hr>
<h2 id="deployments"><strong>Deployments</strong></h2>
<ul>
<li>Deployments declare th state of Pods<ul>
<li>Pod 사양을 업데이트 할 때마다 변경된 deployment 버전과 일치하는 새 ReplicaSet이 생성<br>➔ 배포가 제어된 방식으로 업데이트된 pod를 롤아웃 하는 방법 ➔ 기존 pod는 이전 ReplicaSet에서 제거되고 새 ReplicaSet의 새로운 pod로 대체</li>
<li>배포는 stateless app용으로 설계됨<br>➔ stateless app : 데이터 또는 app 상태의 cluster나 영구 스토리지에 저장X</li>
</ul>
</li>
<li>Deployment is a two-part process<ul>
<li>원하는 상태는 pod의 특성이 포함된 배포 YAML 파일에 설명되어 있으며 pod를 운영 가능하게 실행하고 수명 주기 이벤트를 처리하는 방법이 함께 제공<br>➔ 이 파일을 Kubernetes 제어 영역에 제출하면 배포 컨트롤러가 생성되며  이 컨트롤러는 원하는 상태를 실현하고 원하는 상태를 유지하는 역할을 함</li>
<li>배포 : 상태를 선언하는 pod의 상위 수준 컨트롤러</li>
</ul>
</li>
<li>Deployment has three different lifecycle states<ul>
<li>Progressing state, complete state, failed state</li>
</ul>
</li>
</ul>
<hr>
<h2 id="pod-networking"><strong>Pod networking</strong></h2>
<ul>
<li><p>Pod : a group of containers with shared storage and networking</p>
<ul>
<li>Kubernetes의 &#39;pod별 IP&#39; 모델을 기반<br>➔ 각 pod에 단일 IP 주소가 할당되고 pod 내의 container는 해당 IP 주소를 포함하여 동일한 네트워크 namespace를 공유</li>
</ul>
</li>
<li><p>Your workload doesn&#39;t run in an single pod</p>
<ul>
<li>Workload : 서로 통신해야 하는 다양한 app으로 구성됨</li>
<li>각 pod에는 고유한 IP 주소가 있고, 노드에서 pod는 노드의 루트 네트워크 namespace를 통해 서로 연결    ➔ 해당 VM에서 pod가 서로를 찾고 연결   </li>
<li>루트 네트워크 namespace는 노드의 기본 NIC에 연결 ➔ 노드의 VM NIC를 사용하여 루트 네트워크 namespace는 해당 노드에서 트래픽을 전달<br>➔ Pod의 IP 주소를 노드가 연결된 네트워크에서 라우팅할 수 있어야 한다는 뜻</li>
</ul>
</li>
<li><p>Node get pod IP addresses from address ranges assigned to your virtual private cloud</p>
<ul>
<li>GKE에서 노드는 Virtual Private Cloud 즉, VPC에 할당된 주소 범위에서 pod IP 주소를 가져옴</li>
<li>VPC : 는 GCP 내에서 배포하는 resource에 대한 연결을 제공하는 논리적으로 격리된 네트워크<br>➔ 이러한 resource에는 Kubernetes cluster Compute Engine 인스턴스 App Engine 가변형 인스턴스가 있음</li>
</ul>
</li>
<li><p>Addressing the pods</p>
<ul>
<li>GKE cluster node : GKE가 맞춤설정하고 관리하는 컴퓨팅 인스턴스 ➔ 해당 머신이 있는 VPC 서브넷의 IP 주소가 할당</li>
<li>VPC 기반 GKE 클러스터는 pod에 대해 별도의 별칭 IP 범위도 생성</li>
</ul>
</li>
</ul>
<hr>
<h2 id="volumes"><strong>Volumes</strong></h2>
<h3 id="kubernetes-offers-storage-abstraction-options"><strong>Kubernetes offers storage abstraction options</strong></h3>
<ul>
<li>Volumes<ul>
<li>Volumes are the method by which you attach storage to a pod</li>
<li>Some volumes are ephemeral</li>
<li>Some volumes are persistent</li>
</ul>
</li>
<li>Persistent storage options<ul>
<li>Are block storage, or networked file systems</li>
<li>Provide durable storage outside a pod</li>
<li>Are independent of the pod&#39;s lifecycle</li>
<li>May exist before pod creation and be claimed</li>
</ul>
</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<pre><code>[1] Getting Started with Google Kubernetes Engine, Coursera</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[Google Kubernetes의 구조]]></title>
            <link>https://velog.io/@standing-o/google-kubernetes-architecture</link>
            <guid>https://velog.io/@standing-o/google-kubernetes-architecture</guid>
            <pubDate>Mon, 05 Sep 2022 12:25:56 GMT</pubDate>
            <description><![CDATA[<ul>
<li>본 포스팅은 GKE의 구조 및 object management에 대한 내용을 포함하고 있습니다.</li>
<li>Keyword : Kubernetes, GKE, object management</li>
<li>👉 <a href="https://standing-o.github.io/posts/kubernetes-architecture/"><code>Click</code></a></li>
</ul>
<h1 id="kubernetes-architecture"><strong>Kubernetes architecture</strong></h1>
<ul>
<li>Kubernetes objects : persistent entities representing the state of the cluster<ul>
<li>Object spec : 만들려는 각 객체에 대해 객체 사양을 kubernetes에 제공</li>
<li>Object status : current state described by kubernetes</li>
</ul>
</li>
<li>Containers in a Pod share resources<ul>
<li>Pod : 표준 kubernetes 모듈의 기본 구성요소, kubernetes 시스템에서 실행 중인 모든 컨테이너<br>➔ Container가 위치한 환경을 구현하며 해당 환경은 하나 이상의 container 수용가능<br>➔ Kubernetes는 각 pod에 고유한 IP 주소를 할당; pod 내의 모든 container는 네트워크 namespaces를 공유   </li>
</ul>
</li>
<li>Desired state compared to current state<ul>
<li>Kubernetes가 원하는 상태를 지정했다고 가정했을 때, 해당 상태를 나타내는 하나 이상의 객체를 만들고 유지하도록 kubernetes에 지시하여 작업을 실행<br>➔ kubernetes는 원하는 상태를 현재 상태와 비교 ➔ kubernetes의 제어영역이 cluster 상태를 지속적으로 모니터링하여 상태를 수정</li>
</ul>
</li>
</ul>
<h3 id="cooperating-processes-make-a-kubernetes-cluster-work"><strong>Cooperating processes make a kubernetes cluster work</strong></h3>
<ul>
<li>제어영역 : 한 컴퓨터 ➔ 전체 클러스터를 조정 </li>
<li>노드 : 다른 컴퓨터 ➔ pod를 실행</li>
<li>kube-APIserver : 사용자가 직접 상호작용하는 단일 구성요소 ➔ cluster 상태를 보거나 변경하는 명령어를 수락하는 것<br>➔ <code>kubectl</code> : kube-APIserver에 연결, 통신<br>➔ <code>etcd</code> : cluster의 데이터베이스 ➔ cluster 상태를 안정적으로 저장<br>➔ <code>kube-scheduler</code> : pod를 노드에 예약<br>➔ <code>kube-controller-manager</code> : kube-APIserver를 통해 cluster 상태를 지속적으로 모니터링<br>➔ <code>kube-cloud-manager</code> : 기본 cloud 제공업체와 상호작용하는 컨트롤러 관리   </li>
<li>각 노드는 제어 영역 구성요소의 작은 그룹도 실행</li>
</ul>
<h3 id="google-kubernetes-engine"><strong>Google kubernetes engine</strong></h3>
<ul>
<li><p>GKE manages all the control plane components</p>
<ul>
<li>GKE는 사용자를 대신하여 모든 제어 영역 구성요소를 관리</li>
<li>모든 kubernetes 환경에서 노드는 kubernetes 자체가 아닌 cluster 관리자가 외부에서 만듬</li>
</ul>
</li>
<li><p>Use node pools to manage different kinds of nodes</p>
<ul>
<li>Node pool은 GKE 기능<br>➔ node pool 수준에서 자동 노드 생성, 자동 노드 복구 cluster 자동 확장을 사용 설정</li>
</ul>
</li>
<li><p>Zonal versus regional clusters</p>
<ul>
<li>더 많은 노드를 추가하고 app의 여러 복제본을 배포하면 app의 가용성이 일정 수준까지 향상</li>
<li>전체 컴퓨팅 영역이 다운된다면?<br>➔ GKE regional cluster 사용 ➔ app의 가용성이 단일 region 내의 여러 영역에서 유지되도록함</li>
<li>GKE에서 regional cluster을 구성하려는 목적 : cluster에서 실행 중인 app이 영역 손실을 견뎌낼 수 있도록 하기위함</li>
</ul>
</li>
<li><p>A regional or zonal GKE cluster can also be set up as a private cluster</p>
<ul>
<li>Google cloud 제품이 cluster 제어 영역에 access 가능</li>
<li>승인된 네트워크는 기본적으로 제어 영역에 access 하도록 신뢰를 주는 IP 주소 범위</li>
<li>노드는 제한된 outbound access 권한을 비공개 google access를 통해 보유 가능 ➔ 다른 google cloud 서비스와 통신가능</li>
</ul>
</li>
<li><p>GKE cluster vs GKE</p>
<ul>
<li>GKE cluster에서는 compute engine 가상머신으로 노드가 프로비저닝</li>
<li>GKE에서는 마스터가 google cloud 고객에게 노출되지 않는, GKE 서비스의 추상화 부분으로 프로비저닝</li>
</ul>
</li>
</ul>
<h3 id="object-management"><strong>Object management</strong></h3>
<ul>
<li>모든 kubernetes 객체는 고유한 이름과 고유 식별자로 구분됨</li>
<li>Objects are defined in a YAML file<ul>
<li>Kubernetes가 만들고 유지할 객체를 manifest 파일을 사용하여 정의</li>
<li><code>apiVersion</code> : 객체를 만드는데 사용되는 kubernetes API 버전을 나타냄</li>
<li><code>kind</code> : 원하는 객체를 정의 (pod)</li>
<li><code>metadata</code> : 객체를 식별가능하도록 이름, 고유 ID, namespace를 사용</li>
<li><code>spec</code> : pod의 manifest 파일에서 pod의 container image를 정의하는 필드</li>
<li>YAML 파일은 버전 관리 저장소에 저장<br>➔ GCP 고객은 cloud source repositories를 사용 ➔ 다른 GCP resource와 동일한 방식으로 해당 파일의 권한을 제어가능</li>
</ul>
</li>
<li>Object names<ul>
<li>Kubernetes 객체를 만들 때 이름을 고유한 문자열로 지정</li>
<li>Cluster의 수명 주기 중에 만든 모든 객체에는 kubernetes에서 생성된 고유 ID(UID)가 할당</li>
<li>Label은 key-value 쌍이며, 이를 사용하여 생성 중이나 생성 후에 객체를 태그</li>
</ul>
</li>
<li><code>nginx</code> 웹 서버 3개를 만드는 한 가지 방법<ul>
<li>Pod 객체 3개를 선언 ➔ 각 pod에 고유한 YAML 섹션이 있음</li>
<li>kubernetes의 기본 예약 알고리즘은 사용 가능한 노드에 워크로드를 균등하게 분산하는 것을 선호</li>
</ul>
</li>
<li>Allocating resource quotas<ul>
<li>Multiple projects run on a single cluster ➔ How can I allocate resources quotas?</li>
<li>Kubernetes를 사용하면 단일 물리적 cluster를 namespace라고 하는 여러 가상 cluster로 추상화 가능</li>
<li>Namespace를 사용하면 cluster 전체에 resource 할당량을 적용 가능 ➔ resource 사용량 한도를 정의</li>
</ul>
</li>
<li>배포 객체 : 지정된 시간에 정의된 포드 집합이 실행되도록함</li>
<li>Namespaces<ul>
<li>기본 namespace : 다른 namespace가 정의되지 않은 객체를 포함</li>
<li>Kube-system namespace : kubernetes 시스템 자체에서 만든 객체를 포함.</li>
<li>Kube-public namespace : 모든 사용자가 읽을 수 있도록 공개된 객체를 포함<br>➔ Namespace를 만들 때 namespace에 resource를 적용하려면 명령줄 namespace 플래그를 사용하거나 resource에 대한 YAML파일에 namespace를 지정가능<br>➔ Namespace를 사용하면 cluster 전체에 resource 할당량을 구현가능, 서로 중복되는 객체 이름 사용가능</li>
</ul>
</li>
<li>서비스의 용도<ul>
<li>Pod의 부하 분산 네트워크 엔드포인트 제공, 포드 노출 방법 선택</li>
</ul>
</li>
</ul>
<h3 id="kubernetes-architecture-1"><strong>Kubernetes architecture</strong></h3>
<ul>
<li>App을 설계하고 있으며 지연 시간을 최소화하기 위해 container가 가능한 한 서로 가까이 있기를 원함 ➔ 동일 pod에 container 배치</li>
<li>(EX) 첫 번째 영역의 기본 풀에 4개의 머신이 있는 새 kubernetes engine regional cluster를 배포하고 영역 수를 기본값으로 둠.<br>➔ 계정에 대해 배포되는 compute engine machine 개수 : 12</li>
<li>Kubernetes cluster에서 실행 중인 프로덕션 app이 test 및 staging 배포의 영향을 받지 않는지 확인해야함 ➔ production app을 위한 resource의 우선순위를 지정하려면? : test, staging, production을 위한 namespace를 구성하고, test 및 staging namespace에 특정된 kubernetes resource 할당량을 구성</li>
<li>Stateful app을 위한 스토리지를 구성할 때 pod가 실패하거나 삭제되더라도 app의 데이터가 삭제되지 않도록 container 내부에 파일 시스템 스토리지를 제공하려면? : 네트워크 기반 스토리지를 사용해 볼륨을 생성하여 pod에 원격으로 내구성 높은 스토리지를 제공하고 이를 pod에 지정</li>
<li>DaemonSet : cluster 내의 모든 nod에 배포해야하는 새로운 로깅 및 감사 유틸리티를 처리하기 위함</li>
<li>App의 여러 복사본을 배포하여 이 복사본 전체에 트래픽 부하를 분산하려고 합니다. 이 app의 pod를 cluster의 production namespace에 배포하는 방법: 실행할 복제본 수를 지정하는 배포 manifest 제작</li>
</ul>
<hr>
<h2 id="summary"><strong>Summary</strong></h2>
<ul>
<li>Kubernetes controllers keep the cluster state matching the desired state.</li>
<li>Kubernetes consists of al family of control plane components, running on the control plane and the nodes.</li>
<li>GKE abstracts away the control plane.</li>
<li>Declare the state you want using manifest files.</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<pre><code>[1] Getting Started with Google Kubernetes Engine, Coursera</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[Google Kubernetes Engine(GKE) 란?]]></title>
            <link>https://velog.io/@standing-o/google-kubernetes-engine</link>
            <guid>https://velog.io/@standing-o/google-kubernetes-engine</guid>
            <pubDate>Mon, 05 Sep 2022 12:25:34 GMT</pubDate>
            <description><![CDATA[<ul>
<li>본 포스팅은 Google Kubernetes와 cloud function 및 cloud run에 대한 내용을 포함하고 있습니다.</li>
<li>Keyword : Kubernetes, GKE, cloud function, cloud run</li>
<li>👉 <a href="https://standing-o.github.io/posts/google-kubernetes-engine/"><code>Click</code></a></li>
</ul>
<h1 id="google-kubenetes-engine"><strong>Google Kubenetes Engine</strong></h1>
<h2 id="kubernetes"><strong>Kubernetes</strong></h2>
<ul>
<li>Container 인프라를 더 효과적으로 관리하기 위함</li>
<li>Kubernetes : container 인프라를 on-premise 또는 클라우드에서 조정, 관리 가능<ul>
<li>Container 중심의 관리 환경</li>
<li>Automation : Container화된 app의 배포, 확장, 부하 분산, 로깅, 모니터링, 기타 관리 등을 자동화   ➔ platform as a service</li>
<li>Infrastructure as a service : 다양한 사용자 환경설정과 구성 유연성을 지원</li>
<li>Declarative configuration : 인프라를 선언적으로 관리 ➔ 원하는 시스템 상태가 항상 문서화됨<br>➔ Kubernetes를 사용할 때 원하는 상태를 설명하면 Kubernetes는 배포된 시스템을 원하는 상태에 맞게 만들고 장애가 발생해도 상태를 유지</li>
<li>Imperative configuration : 명령형 구성으로 명령어를 실행하여 시스템 상태를 변경</li>
</ul>
</li>
</ul>
<h3 id="kubernetes-features"><strong>Kubernetes features</strong></h3>
<ul>
<li>Supports both stateful and stateless applications : 데이터를 지속적으로 저장해야하는 app</li>
<li>Autoscaling : resource 사용률에 따라 container화된 app을 자동으로 수평확장/축소 가능</li>
<li>Resource limits : resource를 제어하여 클러스터 내의 전반적인 워크로드 성능 개선</li>
<li>Extensibility : 플러그인, 부가기능 확장</li>
<li>Portability : on-premise 또는 GCP, 여러 클라우드 서비스 제공업체 간 워크로드 이동성 (공급업체 제약X)</li>
</ul>
<hr>
<h2 id="google-kubernetes-engine-gke"><strong>Google Kubernetes engine (GKE)</strong></h2>
<ul>
<li>Kubernetes is powerful, but managing the infrastructure is a full-time job<br>➔ Google cloud 내에서 유용한 기능 : GKE</li>
<li>GKE를 사용하면 GCP에서 container화된 app을 위해 kubernetes 환경을 배포, 관리, 확장 가능</li>
<li>GKE lets you deploy workloads easily</li>
</ul>
<h3 id="gke-features"><strong>GKE features</strong></h3>
<ul>
<li>Fully managed : 기본 resource를 provisioning 할 필요X</li>
<li>Container-optimized OS : Google이 관리하는 이 운영체제는 빠른 확장에 최적화</li>
<li>Auto upgrade <ul>
<li>자동 업그레이드로 최신버전의 kubernetes 유지</li>
<li>Cluster : 인스턴스화 한 kubernetes 시스템</li>
</ul>
</li>
<li>Auto repair<ul>
<li>서비스가 비정상 노드를 자동으로 복구</li>
<li>Node : GKE 클러스터 내에서 container를 호스팅 하는 가상머신</li>
</ul>
</li>
<li>Cluster scaling</li>
<li>Seamless integration<ul>
<li>GKE는 google의 cloud build/container registry와 원활하게 통합</li>
</ul>
</li>
<li>Identity and access management</li>
<li>Integrated logging and monitoring<ul>
<li>Stackdriver : Google cloud 시스템 서비스, 컨테이너, app, 인프라를 모니터링하고 관리<br>➔ GKE는 Stackdriver monitoring과 통합되어 app 성능을 파악</li>
</ul>
</li>
<li>Integrated networking<ul>
<li>GKE는 virtual private cloud (VPC) 와 통합되며 GCP의 네트워킹 기능을 사용</li>
</ul>
</li>
<li>Cloud console<ul>
<li>GKE 클러스터와 resource에 대한 정보를 제공하며 클러스터의 resource를 확인, 검사, 삭제 가능</li>
</ul>
</li>
</ul>
<hr>
<h2 id="computing-options"><strong>Computing options</strong></h2>
<ul>
<li>Fully customizable virtual machines</li>
<li>Persistent disks and optional local SSDs</li>
<li>Global load balancing and autoscaling</li>
<li>Per-second billing</li>
</ul>
<h3 id="compute-engine-use-cases"><strong>Compute engine use cases</strong></h3>
<ul>
<li>Complete control over the OS and virtual hardware</li>
<li>Well suited for lift-and-shift migrations to the cloud</li>
<li>Most flexible compute solution, often used when a managed solution is too restrictive</li>
</ul>
<h3 id="app-engine"><strong>App engine</strong></h3>
<ul>
<li>Provides a fully managed, code-first platform.</li>
<li>Streamlines application deployment and scalability.</li>
<li>Provides support for popular programming languages and application runtimes.</li>
<li>Supports integrated monitoring, logging, and diagnostics.</li>
<li>Simplifies version control, canary testing, and rollbacks.</li>
<li>Use cases : websites, mobile app and gaming backends, RESTful APIs</li>
</ul>
<h3 id="google-kubernetes-engine"><strong>Google kubernetes engine</strong></h3>
<ul>
<li>Fully managed kubernetes platform</li>
<li>Supports cluster scaling, persistent disks, automated upgrades, and auto node repairs</li>
<li>Built-in integration with Google cloud services</li>
<li>Portability across multiple environments<ul>
<li>Hybrid computing, multi-cloud computing</li>
</ul>
</li>
<li>Use cases : Containerized applications, cloud-native distributed systems, hybrid applications</li>
</ul>
<h3 id="cloud-run"><strong>Cloud run</strong></h3>
<ul>
<li>웹 요청 또는 cloud put/sub 이벤트를 통해 stateless container를 실행할 수 있는 관리형 컴퓨팅 플랫폼</li>
<li>Serverless : 모든 인프라 관리를 추상화 ➔ app 개발에만 집중가능</li>
<li>Abstract away infrastructure management ➔ 서버 걱정없이 요청 또는 이벤트 기반 stateless 워크로드를 실행가능</li>
<li>Automatically scales up and down</li>
<li>Open API and runtime environment ➔ 일관된 개발자 환경이 포함된 stateless container를 완전 관리형 환경 또는 자체 GKE cluster에 배포하도록 선택가능</li>
<li>Use cases : <ul>
<li>HTTP 요청을 통해 전달되는 요청이나 이벤트를 수신대기하는 stateless container를 배포가능</li>
<li>Build applications in any language using any frameworks and tools.</li>
</ul>
</li>
</ul>
<h3 id="cloud-functions"><strong>Cloud functions</strong></h3>
<ul>
<li>Event-driven, serverless compute service ➔ 자바스크립트, python 또는 go로 작성한 코드를 업로드하기만 하면 GCP가 코드를 실행하는데 적합한 컴퓨팅 용량을 자동으로 배포</li>
<li>Automatic scaling with highly available and fault-tolerant design</li>
<li>Charges apply only when your code runs</li>
<li>Triggered based on events in google cloud services, HTTP endpoints, and Firebase</li>
<li>Use cases : <ul>
<li>Supporting microservice architecture</li>
<li>Serverless application backends : mobile and IoT backends, integrate with third-party services and APIs</li>
<li>Intelligent applications : virtual assistant and chat bots, video and image analysis</li>
</ul>
</li>
</ul>
<hr>
<h2 id="summary"><strong>Summary</strong></h2>
<ul>
<li>Create a container using cloud build</li>
<li>Store a container in container registry</li>
<li>Compare and contrast kubernetes and google kubernetes engine features.</li>
<li>App 배포를 위한 기술을 선택해야 하는 상황에서 resource 효율이 높고 이동성이 우수한 독립형 경량 패키지 ➔ container</li>
<li>Compute engine : 완전히 맞춤 설정 가능한 가상머신, 초당 청구 ➔ 비용을 세밀하게 제어가능</li>
<li>(EX) 컨테이너화된 app을 배포하고 있으며 컨테이너가 구성 및 배포되는 방식을 최대한 제어하려고함. 전체 container 클러스터 환경을 직접 관리하는 운영 관리 오버헤드도 피하고픈 상황 ➔ Google kubernetes engine</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<pre><code>[1] Getting Started with Google Kubernetes Engine, Coursera</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[Container와 Container image]]></title>
            <link>https://velog.io/@standing-o/container-and-container-image</link>
            <guid>https://velog.io/@standing-o/container-and-container-image</guid>
            <pubDate>Mon, 05 Sep 2022 12:25:09 GMT</pubDate>
            <description><![CDATA[<ul>
<li>본 포스팅은 container와 container image에 대한 내용들을 소개하고 있습니다.</li>
<li>Keyword : Container, container image</li>
<li>👉 <a href="https://standing-o.github.io/posts/container-container-image/"><code>Click</code></a></li>
</ul>
<h1 id="container-and-container-image"><strong>Container and Container image</strong></h1>
<h2 id="containers"><strong>Containers</strong></h2>
<ul>
<li>Dedicated server (Application code, dependencies, kernel, hardware) ➔  Virtual machine (Application code, dependencies, kernel, hardware + hypervisor)<ul>
<li>어플리케이션을 실제 컴퓨터에 배포<br>➔ resource 낭비가 크고 대규모 배포와 유지보수에 많은 시간이 소요 ➔ 가상화 필요</li>
<li>Hypervisors create and manage virtual machines</li>
</ul>
</li>
<li>Running multiple apps on an single VM<br>➔ 종속 항목을 공유하는 app이 서로 격리되지 않는 문제 발생</li>
</ul>
<h3 id="the-vm-centric-way-to-solve-this-problem"><strong>The VM-centric way to solve this problem</strong></h3>
<ul>
<li>App마다 전용 가상 머신을 실행<ul>
<li>각 app에서 고유 종속 항목을 유지 관리 ➔ 커널이 격리되어 있으므로 app끼리 성능에 영향X</li>
<li>But 대규모 시스템의 경우 전용 VM은 중복적이며 낭비, VM 시작속도가 느림</li>
</ul>
</li>
</ul>
<h3 id="user-space-abstraction-and-containers"><strong>User space abstraction and containers</strong></h3>
<ul>
<li>종속 항목 문제 해결 : app과 종속 항목 수준에서 추상화를 구현<ul>
<li>전체 머신이 아니라 사용자 공간만 가상화</li>
<li>사용자 공간 : 커널 위에 있는 모든 코드; app과 종속항목 포함</li>
<li>운영체제 전체를 실행하지 않아 가벼움 ➔ 빠르게 만들고 종료 가능</li>
<li>기본 시스템 위에서 예약/패키징 하므로 효율적<br>➔ container를 만든다</li>
</ul>
</li>
<li>Container (Application code, dependencies) : 단일 app 코드를 생성하는 격리된 사용자 공간</li>
</ul>
<h3 id="why-developers-like-containers"><strong>Why developers like containers</strong></h3>
<ul>
<li>App 중심으로 확장성 높은 고성능 app을 제공, 기본 하드웨어와 소프트웨어를 전제로 작업가능</li>
<li>App을 쉽게 빌드 가능 ➔ 느슨하게 결합되고 세분화된 구성요소 사용 (모듈식 설계)</li>
<li>App의 종속 항목을 서로에게서 격리할 방법이 필요, 가상머신에서 app을 패키징하는것은 낭비</li>
<li>개발자의 노트북에서는 작동하지만 production에서는 실패하는 app의 문제를 해결하기가 어려움</li>
</ul>
<hr>
<h2 id="container-image"><strong>Container image</strong></h2>
<ul>
<li>Image : app과 종속 항목<br>➔ Container : 실행중인 image instance<br>➔ 소프트웨어를 container image로 빌드하면 개발자는 손쉽게 app을 패키징/제공 가능<br>➔ 소프트 웨어가 필요 (Docker)    </li>
</ul>
<h3 id="containers-use-a-varied-set-of-linux-technologies"><strong>Containers use a varied set of Linux technologies</strong></h3>
<ul>
<li>Processes : Linux process마다 서로 분리된 고유 가상 메모리 주소 공간이 존재, 빠르게 생성/삭제 가능</li>
<li>Linux namespaces : process ID 번호, directory tree, IP 주소를 제어 (<strong>≠</strong> kubernetes namespaces)</li>
<li>cgroups : app이 사용가능한 CPU시간, 메모리, I/O 대역폭, 기타 resource의 최대 사용량을 제어</li>
<li>Union file systems : app과 종속항목을 간결한 최소 레이어 모음으로 캡슐화</li>
</ul>
<h3 id="containers-are-structured-in-layers"><strong>Containers are structured in layers</strong></h3>
<ul>
<li>Container manifest 파일로 image build</li>
<li>Docker 형식의 container image ➔ Dockerfile (Container image 내부 레이어가 지정됨)<ul>
<li><code>FROM ubuntu:18.04</code> : FROM 문으로 기본 레이어를 공개 저장소에서 가져와 생성</li>
<li><code>COPY ./app</code> : COPY 명령어로 빌드 도구의 현재 디렉토리에서 복사된 파일이 포함된 레이어를 추가</li>
<li><code>RUN make /app</code> : RUN 명령어는 make 명령어를 사용하여 app을 빌드하고, 빌드 결과를 세번째 레이어에 배치</li>
<li><code>CMD python /app/app.py</code> : 마지막 레이어는 실행 시 container 내에 실행할 명령어를 지정</li>
</ul>
</li>
<li>요즘은 배포 및 실행하는 container에 app을 빌드 권장 X ➔ app 패키징에 다단계 빌드 process를 이용</li>
<li>Image에서 새 container를 만들면, container 런타임에서는 쓰기 가능한 레이어를 기본 레이어 위에 추가 (container layer)</li>
</ul>
<h3 id="containers-promote-smaller-shared-images"><strong>Containers promote smaller shared images</strong></h3>
<ul>
<li>여러 container가 동일한 기본 image에 access권한을 공유하면서 자체 데이터 상태를 보유</li>
<li>Container를 실행하면 container 런타임에서 필요한 레이어를 가져옴 ➔ 업뎃시, 차이 나는 항목만 복사</li>
</ul>
<h3 id="how-can-you-get-containers"><strong>How can you get containers?</strong></h3>
<ul>
<li>Download containerized software from a container registry such as <code>gcr.io.</code><ul>
<li>공개 오픈소스 image가 다수 포함, google cloud 고객도 이를 사용하여 비공개 image를 cloud IAM과 잘 통합되는 방식으로 저장</li>
</ul>
</li>
<li>Docker : bulid your own container using the open-source docker command<ul>
<li>반드시 신뢰가능한 컴퓨터에 빌드해야함</li>
</ul>
</li>
<li>Build your own container using Cloud bulid<ul>
<li>빌드에 필요한 소스코드를 다양한 스토리지 위치에서 검색가능 (Cloud storage, git repo, cloud source repo)</li>
<li>빌드 단계를 구성하여 종속 항목 가져오기 ➔ 소스코드 컴파일 ➔ 통합 테스트 (Docker container에서 실행)</li>
<li>빌드한 image를 다양한 실행환경에 제공 (GKE, app engine, cloud functions)</li>
</ul>
</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<pre><code>[1] Getting Started with Google Kubernetes Engine, Coursera</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[Cloud Computing과 Google Cloud]]></title>
            <link>https://velog.io/@standing-o/cloud-computing-google-cloud</link>
            <guid>https://velog.io/@standing-o/cloud-computing-google-cloud</guid>
            <pubDate>Mon, 05 Sep 2022 12:23:54 GMT</pubDate>
            <description><![CDATA[<ul>
<li>본 포스팅은 compute engine, resource, 그리고 GCP billing에 대한 내용을 포함하고 있습니다.</li>
<li>Keyword : Google cloud, compute engine, resource, billing</li>
<li>👉 <a href="https://standing-o.github.io/posts/cloud-computing-google-cloud/"><code>Click</code></a></li>
</ul>
<h1 id="cloud-computing-and-google-cloud"><strong>Cloud Computing and Google Cloud</strong></h1>
<ul>
<li>Computing resources가 On-demand self-service로 제공됨<br>➔ 사람 개입없이 필요한 처리 능력, 스토리지 네트워크 확보 가능</li>
<li>Broad network access</li>
<li>Resource pooling
➔ 고객은 resource의 물리적 위치를 파악할 필요 없음</li>
<li>Rapid elasticity</li>
<li>Measured service
➔ 사용한 만큼만 지불</li>
</ul>
<h3 id="google-cloud-offers-a-range-of-services"><strong>Google cloud offers a range of services:</strong></h3>
<ul>
<li>Compute engine : 클라우드에서 주문형 가상머신을 실행하게 해 주는 Google cloud infrastructure-as-a-service 솔루션</li>
<li>Google Kubernetes engine (GKE) : google이 관리하는 클라우드 환경에서 컨테이너화된 어플을 실행하며 사용자에게 관리 권한을 부여<br>➔ 컨테이너화 : 이동성을 극대화 하고 resource를 효율적으로 사용할 수 있도록 코드를 패키지화 하는 방식</li>
<li>App engine : GCP의 관리형 Platform-as-a-service 프레임워크, 인프라 걱정없이 클라우드에서 코드를 실행</li>
<li>Cloud functions : 서비스로서의 기능, 이벤트 발생 빈도에 상관없이 이벤트에 응답하여 코드를 실행</li>
</ul>
<h3 id="build-your-own-database-solution"><strong>Build your own database solution</strong></h3>
<ul>
<li>Compute engine, Google Kubernetes engine (GKE)</li>
</ul>
<h3 id="use-a-managed-service"><strong>Use a managed service</strong></h3>
<ul>
<li>Storage : Cloud bigtable, cloud storage, cloud SQL, cloud spanner, datastore</li>
</ul>
<h3 id="google-cloud-offers-a-range-of-services-1"><strong>Google cloud offers a range of services</strong></h3>
<ul>
<li>Big data : Bigquery, pub/sub, dataflow, dataproc, AI platform notebooks</li>
<li>Machine learning : Vision API, AI platform, speech-to-text API, cloud translation API, cloud natural language API</li>
</ul>
<hr>
<h2 id="resource-management"><strong>Resource management</strong></h2>
<ul>
<li>Google cloud는 multi-region, region, zone을 통해 resource를 제공<ul>
<li>Multi-region : America, Europe, Asia-Pacific ➔ divided into regions</li>
<li>Region : 같은 대륙 내에서 독립된 지리적 위치, region 내에서 네트워크 연결이 빠름<br>➔ divided into zones</li>
<li>Zones : 집중된 지리적 위치 내에 있는 GCP resource의 배포 위치</li>
</ul>
</li>
<li>인터넷 사용자가 구글 resource로 트래픽을 전송하면, google은 지연시간이 가장 낮은 edge network 위치에서 응답함</li>
</ul>
<h3 id="zonal-resources-operate-exclusively-in-a-single-zone"><strong>Zonal resources operate exclusively in a single zone</strong></h3>
<ul>
<li>GCP service와 resource를 이용하면 resource의 지리적 위치를 지정 가능</li>
<li>Zonal resource는 단일 영역 내에서 실행됨 ➔ 해당 영역이 사용 불가능해지면 resource 역시 사용 불가능<ul>
<li>Compute Engine Virtual Machine instance, 영구디스크, GKE의 노드</li>
</ul>
</li>
</ul>
<h3 id="regional-resources-span-multiple-zones"><strong>Regional resources span multiple zones</strong></h3>
<ul>
<li>하나의 region 내에 여러 영역에 걸쳐 실행됨 ➔ 중복배포가능</li>
</ul>
<h3 id="global-resources"><strong>Global resources</strong></h3>
<ul>
<li>Multi-region을 통해 관리 가능<ul>
<li>HTTPS load balancers, VPC, Virtual Private Cloud networks</li>
</ul>
</li>
</ul>
<h3 id="resources-sit-in-projects-resources-have-hierarchy"><strong>Resources sit in projects, Resources have hierarchy</strong></h3>
<ul>
<li>사용자가 사용하는 GCP resource는 위치와 상관없이 프로젝트에 속해야함<ul>
<li>프로젝트 : 항목을 논리적으로 구성, 고유 ID와 번호로 식별, 폴더로 그룹핑 가능</li>
</ul>
</li>
<li>GCP resource 계층 구조는 조직 내 여러 부서와 팀의 resource를 관리할 수 있도록 도와줌</li>
<li>Cloud IAM (Cloud identity and access management) : 사용자가 사용하는 모든 GCP resource에 세부적인 엑세스 제어를 가능케함</li>
<li>사용자가 선택한 수준에서 적용한 정책은 낮은 수준으로 상속됨 (조직 ➔ 폴더 ➔ 프로젝트 ➔ 리소스).</li>
<li>결제는 프로젝트 수준에서 누적됨</li>
</ul>
<hr>
<h2 id="billing"><strong>Billing</strong></h2>
<ul>
<li>GCP 결제는 GCP 프로젝트 수준에서 설정함</li>
<li>결제 계정은 하나 이상의 프로젝트에서 연결 가능</li>
<li>결제 계정은 매월 또는 기준액 도달 시 자동으로 청구 및 invoice 되도록 설정가능</li>
<li>GCP service를 재판매하는 GCP 고객들은 하위 계정을 사용하기도함</li>
<li>How to keep your billing under control :<ul>
<li>Budgets and alerts, billing export, reports<br>➔ Budgets and alerts keep your billing under control; 결제 알림 기반으로 자동화 제어<br>➔ Billing export allows you to send your billing data to a BigQuery dataset; 결제 세부정보 저장<br>➔ Reports is a visual tool to monitor expenditure; 지출 모니터링<br>➔ Quotas are helpful limits; 할당량은 오류나 악성 공격으로 인해 resource를 과소비하는 것을 방지하기 위해 설계됨   </li>
<li>할당량 <sup>Quotas</sup><ul>
<li>비율 할당량 : 특정 시점을 기준으로 재설정, GKE 클러스터 자체에서 받는 호출을 제한하는 방식, 정기적으로 재설정</li>
<li>배정 할당량 : 프로젝트 내에서 보유할 수 있는 resource 수를 제어, 시간이 지나도 재설정X</li>
</ul>
</li>
</ul>
</li>
<li>측정형 서비스 : 사용한 resource에 대해서만 비용 지불</li>
</ul>
<hr>
<h2 id="interacting-with-google-cloud"><strong>Interacting with Google Cloud</strong></h2>
<ul>
<li>Google tools or interfaces : GCP resource를 관리하고 구성</li>
</ul>
<h4 id="ways-to-interact-with-google-cloud"><strong>Ways to interact with Google cloud</strong></h4>
<ul>
<li>Google cloud console web user interface : GCP resource를 관리<ul>
<li>Web-based GUI to manage all google cloud resources</li>
<li>Executes common tasks using simple mouse clicks</li>
<li>Provides visibility into google cloud projects and resources</li>
<li>Interacting with the Cloud console : <code>console.cloud.google.com</code> ➔ GCP console log-in </li>
</ul>
</li>
<li>Cloud SDK and cloud shell command-line interface<ul>
<li>Cloud SDK : gcloud, kubectl, gsutil, bq ➔ 주기적 업데이트를 위한 자동화 스크립트 작성</li>
<li>Cloud shell : access directly, constant availability of gcloud, ephemeral compute engine virtual machine instance</li>
</ul>
</li>
<li>Cloud console mobile app for iOS and android<ul>
<li>SSH to conneect to compute engine instances, up-to-date billing info and alerts, customizable graphs</li>
</ul>
</li>
<li>REST-based API for custom applications</li>
</ul>
<h3 id="google-clouds-choices-for-organizing-compute-workloads"><strong>Google Cloud&#39;s choices for organizing compute workloads</strong></h3>
<ul>
<li>Service name : description</li>
<li>Kubernetes engine : a managed environment for deploying containerized applications<br>➔ 컨테이너에 대한 기본적인 지원과 더불어 관리형 컴퓨팅 플랫폼을 제공</li>
<li>Compute engine : a managed environment for deploying virtual machines</li>
<li>App engine : a managed serverless platform for deploying applications</li>
<li>Cloud functions : a managed serverless platform for deploying event-driven functions</li>
</ul>
<hr>
<h2 id="summary"><strong>Summary</strong></h2>
<ul>
<li>Cloud computing means on-demand, pay-as-you-go resources.</li>
<li>Google cloud offers 4 compute services.</li>
<li>Google cloud is organized into regions and zones.</li>
<li>The resource hierarchy helps you manage your google cloud use.</li>
<li>Use the cloud console and cloud shell for access.</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<pre><code>[1] Getting Started with Google Kubernetes Engine, Coursera</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[인과 | Causality 와 인과추론 | Causal inference]]></title>
            <link>https://velog.io/@standing-o/causal-inference</link>
            <guid>https://velog.io/@standing-o/causal-inference</guid>
            <pubDate>Tue, 23 Aug 2022 13:23:32 GMT</pubDate>
            <description><![CDATA[<ul>
<li>본 포스팅은 인과, 인과추론의 개념과 관련 이론 (Back-door, Do-calculus) 들을 소개하고 있습니다.</li>
<li>Keyword : Causality, SCM, Back-door, Do-calculus</li>
<li>👉 <a href="https://standing-o.github.io/posts/causality-causal-inference/"><code>Click</code></a></li>
</ul>
<hr>
<h1 id="인과--causality-와-인과추론--causal-inference"><strong>인과 | Causality 와 인과추론 | Causal inference</strong></h1>
<h2 id="causality"><strong>Causality</strong></h2>
<ul>
<li>Influence by shich one event, process, state, or object a contributes to the production of another event, process, state, or object where the cause is partly responsible for the effect, and the effect is partly dependent on the cause.</li>
<li>Causality in various academic disciplines<ul>
<li>Physics, chemistry,biology, climate science</li>
<li>Psychology, social science, economics</li>
<li>Epidemiology, public health</li>
</ul>
</li>
<li>Relation to AI, ML, DS<ul>
<li>AI : a rational agent performing actions to achieve a goal (reinforcement learning)</li>
<li>ML : currently focused on learning correlations</li>
<li>DS : capture, process, analyze, communicate with data</li>
</ul>
</li>
</ul>
<h3 id="structural-causal-model-scm"><strong>Structural causal model (SCM)</strong></h3>
<ul>
<li>SCM $$M = &lt;U,V,F,P(U)&gt;$$ provides a formal framework.</li>
<li>SCM induces observational, interventional, and counterfactual distributions.</li>
<li>SCM induces a causal graph $$g$$, which implies conditional independencies testable via d-separation (blockage).</li>
<li>The underlying model $$M$$ is unknown but the causal graph $$g$$ can be given from common sense or domain knowledge.</li>
<li>Intervention do(X=x) as a submodel M<sub>x</sub>, which induces a manipulated causal graph $$g_\bar{x}$$.</li>
<li>Causal effect of $$X=x$$ on $$Y=y$$ is defined as $$P(y\mid{do(x)})$$.</li>
</ul>
<h4 id="remark"><strong>Remark</strong></h4>
<ul>
<li>Identifiability : causal effect may be computable from existing observational data for some causal graphs.</li>
<li>In a Markovian case an singleton X, a causal effect can be easily derivable by canceling output $$P(x\mid{pa_x})$$</li>
</ul>
<hr>
<h3 id="back-door-criterion"><strong>Back-door Criterion</strong></h3>
<ul>
<li><p><strong><u>Definition</u></strong>ㅣ<strong>Back-door</strong></p>
<ul>
<li>Find a set $$Z$$ s.t. it can sufficiently explain &#39;confounding&#39; between $$X$$ and $$Y$$. Then, </li>
</ul>
<p>$$
P(y|do(x))=\sum_Z{P(y|x,z)P(z)}&quot;
$$</p>
</li>
<li><p><strong><u>Definition</u>ㅣBack-door criterion</strong></p>
<ul>
<li>A set $$Z$$ satisfies the back-door criterion with respect to a pair of variables $$X, Y$$ in causal diagram $$g$$ if;<ul>
<li>(i) no node in $$Z$$ is a descendant of $$X$$; and</li>
<li>(ii) $Z$ blocks every path between X ∈ $$X$$ and Y ∈ $$Y$$ that contains an arrow into X.</li>
</ul>
</li>
</ul>
</li>
<li><p>A back-door adjustment formula is simple and widely used but limited.</p>
</li>
</ul>
<h4 id="back-door-sets-as-substitutes-of-the-direct-parents-of-x"><strong>Back-door sets as substitutes of the direct parents of X</strong></h4>
<ul>
<li>Rain satisfies the back-door criterion relative to Sprinkler ans Wet:<ul>
<li>(i) Rain is not descendant of Sprinkler, and</li>
<li>(ii) Rain blocks the only back-door path from Sprinkler to Wet.</li>
</ul>
</li>
<li>Adjusting for the direct parents of Sprinkler, we have:</li>
</ul>
<p>$$
P(\text{wt}|do(\text{sp}))=\sum_\text{sn}P(\text{wt}|\text{sp,sn})P(\text{sn})=...=\sum_\text{rn}P(\text{wt}|\text{sp,rn})P(\text{rn})
$$</p>
<hr>
<h3 id="rules-of-do-calculus"><strong>Rules of Do-calculus</strong></h3>
<ul>
<li><p>Backdoor criterion results in a very specific form of indentification formula.</p>
</li>
<li><p>Do-calculus (Pearl, 1995) provides general machinery to manipulate observational and interventional distributions.</p>
</li>
<li><p><strong><u>Theorem</u>ㅣRules of Do-calculus (simplified)</strong></p>
<ul>
<li>Rule 1 : Adding/removing observations</li>
</ul>
<p>$$
P(y|do(x),z)=P(y|do(x)),,,\text{if},,(Z\perp{Y|X}),,\text{in},,g_{\bar{X}}
$$</p>
<ul>
<li>Rule 2 : Action/observation exchange</li>
</ul>
<p>$$
P(y|do(x),do(z))=P(y|do(x),z),,,\text{if},,(Z\perp{Y|X}),,\text{in},,g_{\bar{X}\underline{Z}}
$$</p>
<ul>
<li>Rule 3 : Adding/removing actions</li>
</ul>
<p>$$
P(y|do(x),do(z))=P(y|do(x)),,,\text{if},,(Z\perp{Y|X}),,\text{in},,g_{\bar{X}\bar{Z}}
$$</p>
</li>
</ul>
<ul>
<li><p>Do-calculus is sound and complete but it has no algorithmic insight</p>
</li>
<li><p>A graphical condition and an efficient algorithmic procedure have developed for identifiability.</p>
</li>
<li><p>Do-calculus is a set of rules to manipulate observational or interventional probabilites. (Do-calculus is complete)</p>
</li>
</ul>
<hr>
<h3 id="modern-identification-tasks"><strong>Modern identification tasks</strong></h3>
<ul>
<li><p>Experimental conditions ➔ <strong>Generalized identification</strong></p>
<ul>
<li><p>Combining datasets of different experimental conditions</p>
</li>
<li><p>The identifiability of any expression of the form $$P(y\mid{do(x), z})$$ can be determined given any causal graph $$g$$ and an arbitrary combination of observational and experimental studies.</p>
</li>
<li><p>If the query is identifiable, then its estimand can be derived in polynomial time.</p>
</li>
</ul>
</li>
<li><p>Environmental conditions ➔ <strong>Transportability</strong></p>
<ul>
<li><p>Combining datasets from different sources</p>
</li>
<li><p>Non-parametric transportability can be determined provided that the problem instance is encoded in selection diagrams.</p>
</li>
<li><p>When transportability is feasible, the transport formula can be derived in polynomial time.</p>
</li>
<li><p>The causal calculus and the corresponding transportation algorithm are complete.</p>
</li>
</ul>
</li>
<li><p>Sampling conditons ➔ Recovering from <strong>selection bias</strong></p>
<ul>
<li>Nonparametric recoverability of selection bias from causal and statistical settings can be determined provided that an augmented causal graph is available.</li>
<li>When recoverability is feasible, the estimated can be derived in polynomial time.</li>
<li>The result is complete for pure recoverability, and sufficient for recoverability with external information.</li>
</ul>
</li>
<li><p>Responding conditons ➔ Recovering from <strong>missingness</strong></p>
</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<ul>
<li>본 포스팅은 <code>LG Aimers</code> 프로그램에 참가하여 학습한 내용을 기반으로 작성된것입니다. (전체내용 X)</li>
</ul>
<p>➔ <a href="https://www.lgaimers.ai/"><code>LG Aimers</code> 바로가기</a></p>
<pre><code>[1] LG Aimers AI Essential Course Module 5.인과추론, 서울대학교 이상학 교수 </code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[자율주행과 레이더센서]]></title>
            <link>https://velog.io/@standing-o/autonomous-driving-radar</link>
            <guid>https://velog.io/@standing-o/autonomous-driving-radar</guid>
            <pubDate>Tue, 23 Aug 2022 13:22:41 GMT</pubDate>
            <description><![CDATA[<h1 id="자율주행과-레이더센서"><strong>자율주행과 레이더센서</strong></h1>
<ul>
<li>본 포스팅은 자율주행과 radar에 대한 기술과 동향을 소개하고 있습니다.</li>
<li>Keyword : 자율주행, Radar</li>
<li>👉 <a href="https://standing-o.github.io/posts/autonomous-driving-radar/"><code>Click</code></a></li>
</ul>
<hr>
<h2 id="자율주행-시장동향"><strong>자율주행 시장동향</strong></h2>
<h3 id="미래-모빌리티-메가-트렌드"><strong>미래 모빌리티 메가 트렌드</strong></h3>
<ul>
<li>Autonomous driving ➔ 운전자 개입없이 스스로 안전하게 주행이 가능한 자율주행 고도화</li>
<li>Connectivity ➔ 고도화된 연결형 자율주행을 통한 탑승자의 안전 및 교통관리 효과성 극대화</li>
<li>Electrification ➔ 높은 에너지 효율성 기반 1회 충전으로 최대 주행거리 확보</li>
</ul>
<h3 id="자율주행-단계-고도화"><strong>자율주행 단계 고도화</strong></h3>
<ul>
<li>자율주행 단계<ul>
<li>수동운전 ➔ 주행보조 ➔ 부분적 자율주행 ➔ 조건적 자율주행 ➔ 고도 자율주행 ➔ 완전 자율주행</li>
</ul>
</li>
</ul>
<h3 id="자율주행-자동자-시장-동향"><strong>자율주행 자동자 시장 동향</strong></h3>
<ul>
<li>2025년 시장 점유율<ul>
<li>부분 자율주행 : 12.4% / 완전 자율주행 : 0.5%</li>
</ul>
</li>
<li>2035년 시장 점유율<ul>
<li>부분 자율주행 : 15% / 완전 자율주행 : 9.8%</li>
</ul>
</li>
</ul>
<h3 id="자율-주행-센서"><strong>자율 주행 센서</strong></h3>
<ul>
<li>Camera<ul>
<li>장거리 및 인식률 개선을 위한 고화소화, 픽셀 사이즈 소형화, 저조도 개선</li>
<li>고온 동작의 품질 확보를 위한 Lens/Housing 구조 최적화</li>
<li>생산에서 Active alignment와 calibration 공정 기술 차별화</li>
</ul>
</li>
<li>Radar<ul>
<li>고해상도 4D Imaging radar 구현을 위한 안테나 및 신호처리 S/W 기술 발전</li>
<li>Perception SW 고도화로 사물의 형상구분 및 상황예측까지 성능 발전</li>
<li>생산에서는 평탄도 관리 및 Calibration 및 EOL 공정 기술 고도화</li>
</ul>
</li>
<li>LiDAR<ul>
<li>ADAS용 LiDAR는 차량신뢰성, 디자인, Cost 우선 순위로 진화</li>
<li>Lv4/5를 위해 Redundancy를 고려한 Sensor fusion 핵심 부품으로 성장</li>
</ul>
</li>
</ul>
<h3 id="자율주행-soc-동향"><strong>자율주행 SoC 동향</strong></h3>
<ul>
<li>Tesla<ul>
<li>카메라 2D 이미지만으로 실시간 3D 이미지 합성하는 기술</li>
<li>Edge case 중심의 서버를 통한 딥러닝과 시뮬레이션으로 정확도 향상</li>
</ul>
</li>
<li>엔디비아<ul>
<li>ADAS 시스템에서 자율주행용 Hyperion 시스템 발전</li>
<li>2D 카메라 중심에서 초음파, LiDAR, Radar 병행하는 3D 방식으로 전환</li>
</ul>
</li>
<li>모빌아이<ul>
<li>자율주행 EyeQ 시리즈 + 인포테인먼트 인텔 Atom C3000 솔루션</li>
<li>SD맵과 HD맵의 하이브리드 방식인 Autonomous vehicle 방식</li>
</ul>
</li>
</ul>
<h4 id="remark"><strong>Remark</strong></h4>
<ul>
<li>미래 모빌리티 메가 트렌드는 A.C.E</li>
<li>자율주행 단계는 현재 Lv3, 2025년 Lv4, 2030년 이후 Lv5 완전 자율 주행화 예상</li>
<li>자율주행 완성차는 2035년까지 CAGR 3%, 자율주행 센서는 CAGR 7% 성장 예상</li>
<li>자율주행 센서는 카메라, 레이다, 라이다, 5G C-V2X 통신, 오디오 등이 필요</li>
<li>기존 개별 센서의 역량의 한계를 극복하기 위해 센서 Pod 기술로 발전</li>
<li>자율주행 솔루션 업체별 Lv4/Lv5의 상용차 중심의 자율주행을 개발중</li>
</ul>
<hr>
<h2 id="radar"><strong>Radar</strong></h2>
<ul>
<li>Radio detection and ranging</li>
<li>Radio wave를 이용한 사물 감지 기술</li>
<li>차량 radar는 차량/보행자/도로 인프라를 인식하여 차량과의 거리, 상대속도, 각도, 높이 등의 정보를 수집</li>
</ul>
<h4 id="거리-측정"><strong>거리 측정</strong></h4>
<ul>
<li>Measure the time of flight (ToF) in order to calculate the distance : $$d = \frac{c_0t}{2}$$</li>
<li>With c<sub>0</sub> being the speed of light and t the ToF</li>
</ul>
<h4 id="속도-측정"><strong>속도 측정</strong></h4>
<ul>
<li>Pulsed radar : two succesive measurements</li>
<li>FMCW radar : exploit the Doppler shift</li>
</ul>
<h3 id="radar-필요-기술"><strong>Radar 필요 기술</strong></h3>
<ul>
<li>Antenna<ul>
<li>High gain, 광각, 고해상도, peak gain, 방사패턴 최적화, array 안테나 설계</li>
</ul>
</li>
<li>mmWave 회로<ul>
<li>저손실, EMC 최소화 설계, Main IC 기반 플랫폼 설계, Transition 최소화 및 RF 매칭</li>
</ul>
</li>
<li>SW<ul>
<li>System SW, radar 신호처리, perception 알고리즘</li>
</ul>
</li>
<li>기구<ul>
<li>Radome 전파 투과율 최적화, 고신뢰성 및 방수, 방진, 방열 설계, simulation</li>
</ul>
</li>
</ul>
<h3 id="radar-기술-동향"><strong>Radar 기술 동향</strong></h3>
<ul>
<li>2D ADAS Basic (X, Y, Doppler) ➔ 2D ADAS improved (X, Y, Doppler) ➔ 3D (X, Y, Z, Doppler) ➔ 4D HR (High resolution; X, Y, Z, Doppler, depth)</li>
</ul>
<p>➔ 4D UHR (Ultra high resolution; X, Y, Z, Doppler, depth) ➔ Imaging (X, Y, Z, Doppler, depth, AI/Deeplearning)</p>
<h3 id="radar-시장-동향"><strong>Radar 시장 동향</strong></h3>
<ul>
<li>차량 제어를 위해 AEB 기능 채용 확대</li>
<li>Front radar의 고해상도로 채용률 성장</li>
<li>Corner radar의 low cost ➔ 차량당 4개 이상 적용되어 360도 서라운드 센싱</li>
<li>안전과 편의 기능으로 강화를 위한 In-cabin용 신규 application 개화</li>
</ul>
<h4 id="remark-1"><strong>Remark</strong></h4>
<ul>
<li>Radar 필요기술은 안테나, mmWave 회로, SW, 기구, PCB, 공정설계</li>
<li>Radar 종류는 SRR, MRR, LRR이고 향후 4D Imaging radar로 고도화</li>
<li>차량용 radar는 Infineon, TI, NXP가 주로 사용</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<ul>
<li>본 포스팅은 <code>LG Aimers</code> 프로그램에 참가하여 학습한 내용을 기반으로 작성되었습니다. (전체내용 X)</li>
</ul>
<p>➔ <a href="https://www.lgaimers.ai/"><code>LG Aimers</code> 바로가기</a></p>
<pre><code>[1] LG Aimers AI Essential Course Module 6.자율주행과 레이더센서의 이해, LG이노텍 김경석 연구위원</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[XAI | Explainable AI 의 개념과 분류]]></title>
            <link>https://velog.io/@standing-o/xai</link>
            <guid>https://velog.io/@standing-o/xai</guid>
            <pubDate>Tue, 23 Aug 2022 13:21:57 GMT</pubDate>
            <description><![CDATA[<h1 id="xai--explainable-ai-의-개념과-분류"><strong>XAI | Explainable AI 의 개념과 분류</strong></h1>
<ul>
<li>본 포스팅은 설명가능한 인공지능 (XAI) 의 개념과 분류 방법에 대한 내용을 포함하고 있습니다.</li>
<li>Keyword :  XAI, CAM, LIME, RISE</li>
<li>👉 <a href="https://standing-o.github.io/posts/xai/"><code>Click</code></a></li>
</ul>
<hr>
<h2 id="supervised-deep-learning"><strong>Supervised (deep) learning</strong></h2>
<ul>
<li>Supervised (deep) learning has made a huge progress but deep learning models are extremely complex<ul>
<li>End-to-end learning becomes a black-box</li>
<li>Problem happens when models applied to make critical decisions</li>
</ul>
</li>
</ul>
<h3 id="what-is-explainability--interpretability"><strong>What is explainability &amp; Interpretability?</strong></h3>
<ul>
<li><strong>Interpretability</strong> is the degree to which a human can understand the cause of a decision</li>
<li><strong>Interpretability</strong> is the degree to which a human can consistently predict the model&#39;s resutls.</li>
<li>An <strong>explanation</strong> is the answer to why-question.</li>
</ul>
<h3 id="taxonomy-of-xai-methods"><strong>Taxonomy of XAI methods</strong></h3>
<ul>
<li>Local vs. Global<ul>
<li>Local : describes an individual prediction</li>
<li>Global : describes entire model behavior</li>
</ul>
</li>
<li>White-box vs. Black-box<ul>
<li>White-box : explainer can assess the inside of model</li>
<li>Black-box : explainer can assess only the output</li>
</ul>
</li>
<li>Intrinsic vs. Post-hoc<ul>
<li>Intrinsic : restricts the model complexity before training</li>
<li>Post-hoc : Applies after the ML model is trained</li>
</ul>
</li>
<li>Model specific vs. Model agnostic<ul>
<li>Model-specific : some methods restricted to specific model classes</li>
<li>Model-agnostic : some methods can be used for any model</li>
</ul>
</li>
</ul>
<h4 id="examples"><strong>Examples</strong></h4>
<ul>
<li>Linear model, simple decision tree</li>
</ul>
<p>➔ Global, white-box, intrinsic, model-specific</p>
<ul>
<li>Grad-CAM</li>
</ul>
<p>➔ Local, white-box, post-hoc, model-agnostic</p>
<h3 id="simple-gradient-method"><strong>Simple Gradient method</strong></h3>
<ul>
<li><p>Simple use the gradient as the explanation (importance)</p>
<ul>
<li>Interpretation of f at x<sub>0</sub> (for the i-th input/feature/pixel)</li>
</ul>
<p>$$
R_i=(\nabla{f(x)}|_{x_0})_i
$$</p>
<ul>
<li>Shows how sensitive a function value is to each input</li>
</ul>
</li>
<li><p>Examples : the gradient maps are visualized for the highest-scoring class</p>
</li>
<li><p>Strength : easy to compute (via back-propagation)</p>
</li>
<li><p>Weakness : becomes noisy (due to shattering gradient problems)</p>
</li>
</ul>
<h3 id="smoothgrad"><strong>SmoothGrad</strong></h3>
<ul>
<li><p>A simple method to address the noisy gradients</p>
<ul>
<li>Add some noise to the input and average</li>
</ul>
<p>$$
\nabla_{\text{smooth}}f(x)=\mathbb{E}_{\epsilon\sim\mathcal{N}(0,\sigma^{2}I)}[\nabla{f(x+\epsilon)}]
$$</p>
<ul>
<li>Averaging gradients of slightly perturbed input would smoothen the interpretation</li>
<li>Typical heuristics<ul>
<li>Expectation is approximated with Monte-Carlo (around 50 runs)</li>
<li>&sigma; is set to be 10~20% of x<sub>max</sub>-x<sub>min</sub></li>
</ul>
</li>
</ul>
</li>
<li><p>Strength</p>
<ul>
<li>Clearer interpretation via simple averaging</li>
<li>Applicable to most sensitive maps</li>
</ul>
</li>
<li><p>Weakness</p>
<ul>
<li>Computationally expensive</li>
</ul>
</li>
</ul>
<h3 id="class-activation-map-cam"><strong>Class activation map (CAM)</strong></h3>
<ul>
<li>Method<ul>
<li>Upsample the CAM to match the size with the input image</li>
<li>Global average pooling (GAP) should be implemented before the softmax layer</li>
</ul>
</li>
<li>Alternative view of CAM<ul>
<li>The logit of the class c for CAM is represented by:</li>
</ul>
</li>
</ul>
<p>(GAP-FC model)</p>
<p>$$
Y^c=\sum_k{w_k^c}\frac{1}{Z}\sum_{ij}{A^k_{ij}}
$$</p>
<ul>
<li>Result<ul>
<li>CAM can localize objects in image</li>
<li>Segment the regions that have the value above 20% of the max value of the CAM and take the bounding box of it</li>
</ul>
</li>
<li>Strength<ul>
<li>It clearly shows what objects the model is looking at</li>
</ul>
</li>
<li>Weakness<ul>
<li>Model-specific: it can be applied only to models with limited architecture</li>
<li>It can only be obtained at the last convolutional layer and this makes the interpretation resolution coarse</li>
</ul>
</li>
</ul>
<h3 id="grad-cam"><strong>Grad-CAM</strong></h3>
<ul>
<li>Method<ul>
<li>To calculate the channel-wise weighted sum, Grad-CAM substitute weights by average pooled gradient</li>
</ul>
</li>
<li>Strength<ul>
<li>Model agnostic: can be applied to various output models</li>
</ul>
</li>
<li>Weakness<ul>
<li>Average gradient sometimes is not accurate</li>
</ul>
</li>
<li>Result<ul>
<li>Debugging the training with Grad-CAM</li>
</ul>
</li>
</ul>
<h3 id="lime"><strong>LIME</strong></h3>
<ul>
<li>Local interpretable model-agnostic explanations (LIME)<ul>
<li>Can explain the predictions of any classfier by approximating it locally with an interpretable model</li>
<li>Model-agnostic, black-box</li>
<li>General overview of the interpretations</li>
</ul>
</li>
<li>Perturb the super-pixels and obtain the local interpretation model near the given example</li>
<li>Explaining an image classification prediction made by Google&#39;s inception neural network</li>
<li>Strength<ul>
<li>Black-box interpretation</li>
</ul>
</li>
<li>Weakness<ul>
<li>Computationally expensive</li>
<li>Hard to apply to certain kind of models</li>
<li>When the underlying model is still locally non-linear</li>
</ul>
</li>
</ul>
<h3 id="rise"><strong>RISE</strong></h3>
<ul>
<li>Randomized input sampling for explanation (RISE)<ul>
<li>Sub-sampling the input image via random masks</li>
<li>Record its response to each of the masked images</li>
</ul>
</li>
<li>Comparison to LIME<ul>
<li>The saliency of LIME is relied on super-pixels, which may not capture correct regions</li>
</ul>
</li>
<li>Strength<ul>
<li>Much clear saliency-map</li>
</ul>
</li>
<li>Weakness<ul>
<li>High computational complexity</li>
<li>Noisy due to sampling</li>
</ul>
</li>
<li>RISE, sometimes, provides noisy importance maps<ul>
<li>It is due to sampling approximation (Monte Carlo) expecially in presence of objects with varying sizes</li>
</ul>
</li>
</ul>
<h3 id="understanding-black-box-predictions-via-influence-functions"><strong>Understanding black-box predictions via influence functions</strong></h3>
<ul>
<li>Different approach for XAI<ul>
<li>Identify most influential training data point for the given prediction</li>
</ul>
</li>
<li>Influence function<ul>
<li>Measure the effect of removing a training sample on the test loss value</li>
</ul>
</li>
<li>Influence function-based explanation can show the difference between the models</li>
</ul>
<hr>
<h2 id="metrics"><strong>Metrics</strong></h2>
<h3 id="human-based-visual-assessment"><strong>Human-based visual assessment</strong></h3>
<ul>
<li>AMT (Amazon mechanical turk) test<ul>
<li>Want to know: Can human predict a model prediction via interpretation?</li>
</ul>
</li>
<li>Weakness<ul>
<li>Obtaining human assessment is very expensive</li>
</ul>
</li>
</ul>
<h3 id="human-annotation"><strong>Human annotation</strong></h3>
<ul>
<li><p>Some metrics employ human annotation (localization and semantic segmentation) as a ground truth, and compare them with interpretation</p>
<ul>
<li>Pointing game</li>
<li>Weakly supervised semantic segmentation</li>
</ul>
</li>
<li><p>Pointing game</p>
<ul>
<li>For given human annotated bounding box $${B^i}<em>{i=1,...,N}$$ and interpretations $$h^i</em>{I=1,...,N}$$, a mean accuracy of pointing game is defined by:</li>
</ul>
<p>$$
Acc=\frac{1}{N}\sum^N_{i=1}1_{[p^{(i)}\in{B^{(i)}}]}
$$</p>
<ul>
<li>Where $$p_i$$ is a pixel s.t. $$p_i=argmax_p(h_p^i)$$</li>
<li>$$1_{[p^i∈B^i]}$$ is an indicator function that the value is; if the pixel of highest interpretation score is loacted in the bounding box</li>
</ul>
</li>
<li><p>Weakly supervised semantic segmentation</p>
<ul>
<li>Setting : Pixel-level label is not given during training</li>
<li>This metric measures the mean IoU between interpretation and semantic segmentation label</li>
</ul>
</li>
<li><p>Weakness</p>
<ul>
<li>Hard to make the human annotations</li>
<li>Such localization and segmentation labels are not a ground truth of interpretation</li>
</ul>
</li>
</ul>
<h3 id="pixel-perturbation"><strong>Pixel perturbation</strong></h3>
<ul>
<li>Motivation<ul>
<li>If we remove an important area in image, the logit value for class would be decreased</li>
</ul>
</li>
<li>AOPC (Area over the MoRF perturbation curve)<ul>
<li>AOPC measures the decreases of logits from the replacement of the input patch in MoRF (most relevant first) order</li>
</ul>
</li>
</ul>
<p>$$
AOPC=\frac{1}{L+1}\mathbb{E}<em>{x\sim{p(x)}}[\sum^L</em>{k=0}f(x^{(0)}<em>{MoRF}-x^{(k)}</em>{MoRF})]
$$</p>
<p>where f is the logit for true label</p>
<ul>
<li>Insertion and deletion<ul>
<li>Both measure the AUC of each curve<ul>
<li>In deletion curve, x axis is the percentage of the removed pixels in the MoRF order, and y axis is the class probability of the model</li>
<li>In insertion curve, x axis is the percentage of the recovered pixels in the MoRF order, starting grom gray image</li>
</ul>
</li>
</ul>
</li>
<li>Weakness<ul>
<li>Violates one of the key assumptions in ML that the training and evaluation data come from the same distribution</li>
<li>The perturbed input data is different from the model of interest which is deployed and explained at test time</li>
<li>Perturbation can generate another feature for model, i.e., the model tends to predict perturbed input as Balloon</li>
</ul>
</li>
</ul>
<h3 id="roar"><strong>ROAR</strong></h3>
<ul>
<li>ROAR removes some portion of pixels in train data in the order of high interpretation values of the original model, and retrains a new model</li>
<li>Weakness<ul>
<li>Retraining everytime is computationally expensive</li>
</ul>
</li>
</ul>
<hr>
<h2 id="sanity-checks"><strong>Sanity checks</strong></h2>
<h3 id="model-randomization"><strong>Model randomization</strong></h3>
<ul>
<li>Interpretation = Edge detector?<ul>
<li>Some interpretation methods produce saliency maps that strikingly similar as the one created by edge detector</li>
</ul>
</li>
<li>Model randomization test<ul>
<li>This experiment randomly re-initialize the parameters in a cascading fashion or single independent layer fashion</li>
<li>Some interpretation does not sensitive to this randomization, i.e., Guided-backprop, LRP, and pattern attribution</li>
</ul>
</li>
</ul>
<h3 id="adversarial-attack"><strong>Adversarial attack</strong></h3>
<ul>
<li>Geometry is to blame<ul>
<li>Proposed the adversarial attack on interpretation:
$$\mathbb{L}=||h(x_{adv})-h^t||^2+\gamma||g(x_{adv})-g(x)||^2$$</li>
<li>Proposed a smoothing method to undo attack<ul>
<li>Using a softplus activation with high beta can undo the interpretation attack</li>
</ul>
</li>
<li>Provided theoretical bound of such attack</li>
</ul>
</li>
<li>Results<ul>
<li>In the right figure, the visualization of manipulated image is attacked with target interpretation h<sub>t</sub>.</li>
<li>For both gradient and LRP, the manipulated interpretation of for network with ReLU activation is similar as target interpretation, but the one with softplus is not manipulated.</li>
</ul>
</li>
</ul>
<h3 id="adversarial-model-manipulation"><strong>Adversarial model manipulation</strong></h3>
<ul>
<li>Adversarial model manipulation<ul>
<li>Two models could produce totally different interpretations, while have similar accuracy.</li>
</ul>
</li>
<li>Attack on the input<ul>
<li>Negligible model accuracy drop</li>
<li>Fooling generalizes across validation set</li>
<li>Fooling transfers to different interpretations</li>
<li>AOPC analysis confirms true foolings</li>
</ul>
</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<ul>
<li>본 포스팅은 <code>LG Aimers</code> 프로그램에 참가하여 학습한 내용을 기반으로 작성되었습니다. (전체내용 X)</li>
</ul>
<p>➔ <a href="https://www.lgaimers.ai/"><code>LG Aimers</code> 바로가기</a></p>
<pre><code>[1] LG Aimers AI Essential Course Module 4.설명가능한 AI(Explainable AI), 서울대학교 문태섭 교수</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[비지도학습 | Unsupervised learning 과 딥러닝]]></title>
            <link>https://velog.io/@standing-o/unsupervised-learning</link>
            <guid>https://velog.io/@standing-o/unsupervised-learning</guid>
            <pubDate>Tue, 23 Aug 2022 13:21:06 GMT</pubDate>
            <description><![CDATA[<h1 id="비지도학습--unsupervised-learning-과-딥러닝"><strong>비지도학습 | Unsupervised learning 과 딥러닝</strong></h1>
<ul>
<li>본 포스팅은 인공지능의 비지도 학습 개념과 종류를 설명하고 있습니다.</li>
<li>Keyword :  Unsupervised learning</li>
<li>👉 <a href="https://standing-o.github.io/posts/unsupervised-learning/"><code>Click</code></a></li>
</ul>
<hr>
<h2 id="in-traditional-machine-learning"><strong>In traditional machine learning</strong></h2>
<ul>
<li>K-means clustering</li>
<li>Hierarchical clustering</li>
<li>Density estimation</li>
<li>PCA</li>
</ul>
<h4 id="특징"><strong>특징</strong></h4>
<ul>
<li>Low dimensional data</li>
<li>Simple concepts</li>
</ul>
<h2 id="in-deep-learing"><strong>In Deep learing</strong></h2>
<h3 id="feature-engineering-vs-representation-learning"><strong>Feature engineering vs. Representation learning</strong></h3>
<ul>
<li>Feature engineering<ul>
<li>By human</li>
<li>Domain knowledge &amp; Creactivity</li>
<li>Brainstorming</li>
</ul>
</li>
<li>Representation learning<ul>
<li>By machine</li>
<li>Deep learning knowledge &amp; coding skill</li>
<li>Trial and error</li>
</ul>
</li>
</ul>
<h3 id="modern-unsupervised-learning"><strong>Modern unsupervised learning</strong></h3>
<ul>
<li>High dimensional data</li>
<li>Difficult concepts ➔ Not well understood, but surprisingly good performance</li>
<li>Deep learning</li>
<li>Unsupervised representation learning</li>
</ul>
<h3 id="representation-in-deep-learning"><strong>Representation in deep learning</strong></h3>
<ul>
<li><p>Deep learning representation is under constrained</p>
<ul>
<li>Simple SGD can find one of the useful networks</li>
<li>Representation characteristics can be adjusted if needed</li>
<li>Learned representation becomes difficult to understand</li>
</ul>
</li>
<li><p>Disentangled representation</p>
<ul>
<li>Alinged</li>
<li>Independent</li>
<li>Subspaces</li>
<li>Possible because severaly underconstrained</li>
</ul>
</li>
</ul>
<h3 id="angle-information"><strong>Angle information</strong></h3>
<ul>
<li>0 ~ 2&pi;<ul>
<li>Algorithm thinks : 0 and 2&pi; are different / 0 and 1.9&pi; are far</li>
</ul>
</li>
<li>(x<sub>1</sub>, x<sub>2</sub>) = (cos(&theta;), sin(&theta;))<ul>
<li>0 and 2&pi; are the same</li>
<li>0 and 1.9&pi; are close</li>
</ul>
</li>
</ul>
<h3 id="spatial-information"><strong>Spatial information</strong></h3>
<ul>
<li>Goal : Represent as mathematical object</li>
</ul>
<h3 id="human-representation-problems"><strong>Human representation problems</strong></h3>
<ul>
<li>Human can understand</li>
<li>Human can design with a goal</li>
</ul>
<p>➔ Good representation in deep learning? : Useful and irrelevant</p>
<h3 id="a-well-defined-task"><strong>A well defined task</strong></h3>
<ul>
<li>Typically, only on attribute of interest is considered as y<ul>
<li>Imagenet - class</li>
<li>y is well defined because it is simply defined as human selected label</li>
</ul>
</li>
<li>Good representation - a vague concept (Supervised)<ul>
<li>Even when y is well defined, what do we want for h<sub>i</sub> and h<sub>2</sub>?</li>
<li>Simply say &quot;representation learning successful&quot;  if good performance?</li>
<li>But then there is almost nothing we can sy about h<sub>i</sub> and h<sub>2</sub></li>
<li>Other than saying &quot;useful information has been well curated&quot;</li>
<li>Is there anything we can say or pursue?</li>
<li>For a general purpose, what is a good representation?</li>
</ul>
</li>
</ul>
<h3 id="information-bottleneck"><strong>Information bottleneck</strong></h3>
<ul>
<li>For a well defined supervised task, what should h<sub>i</sub> and h<sub>2</sub> satisfy?</li>
<li>Good representation - a vague concept (Unsupervised)<ul>
<li>For a general purpose, whawt is a good representation?</li>
<li>General purpose often defined as a list of downstream tasks?</li>
<li>So, we go back to good performance for the tasks of interest?</li>
</ul>
</li>
</ul>
<h3 id="representation"><strong>Representation</strong></h3>
<ul>
<li>What we want: a formal definition and evaluation metrics for representation</li>
<li>Reality : No definition, task dependent evaluation methods</li>
</ul>
<hr>
<h2 id="unsupervised-representation-learning"><strong>Unsupervised representation learning</strong></h2>
<ul>
<li>Unsupervised performance ≈ supervised performance<ul>
<li>For linear evaluation</li>
<li>Thanks to instance discrimination, contrastive loss, and aggressive augmentation</li>
</ul>
</li>
<li>As in supervised learning<ul>
<li>Performance metric can be unclear</li>
<li>Design of surrogate loss is an art (some principled; some hueristics based)</li>
<li>Training techinique development continuing (but augmentation methods are dominating)</li>
</ul>
</li>
<li>NLP<ul>
<li>Masked language modeling</li>
<li>What next?</li>
</ul>
</li>
<li>Unsupervised representation learning<ul>
<li>Still a long way to go...</li>
</ul>
</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<ul>
<li>본 포스팅은 <code>LG Aimers</code> 프로그램에 참가하여 학습한 내용을 기반으로 작성되었습니다. (전체내용 X)</li>
</ul>
<p>➔ <a href="https://www.lgaimers.ai/"><code>LG Aimers</code> 바로가기</a></p>
<pre><code>[1] LG Aimers AI Essential Course Module 3.비지도학습, 서울대학교 이원종 교수 </code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[지도학습 | Supervised learning 이란? (SVM, ANN, Ensemble)]]></title>
            <link>https://velog.io/@standing-o/supervised-learning</link>
            <guid>https://velog.io/@standing-o/supervised-learning</guid>
            <pubDate>Tue, 23 Aug 2022 13:20:15 GMT</pubDate>
            <description><![CDATA[<h1 id="지도학습--supervised-learning-이란-svm-ann-ensemble"><strong>지도학습 | Supervised learning 이란? (SVM, ANN, Ensemble)</strong></h1>
<ul>
<li>본 포스팅은 인공지능의 지도학습 개념과 그 종류 (선형모델, SVM, ANN) 에 대한 내용을 포함하고 있습니다.</li>
<li>Keyword : Supervised learning, overfitting, underfitting, linear model, SVM, ANN, ensemble</li>
<li>👉 <a href="https://standing-o.github.io/posts/supervised-learning/"><code>Click</code></a></li>
</ul>
<hr>
<h2 id="지도학습-supsupervised-learningsup"><strong>지도학습 <sup>Supervised learning</sup></strong></h2>
<ul>
<li><p>Given a set of labeled examples $$(x^1, y^1),...,(x^N, y^N)$$, learn a mapping function g : X ➔ Y, s.t. given an unseen sample x&#39;, associated output y&#39; is predicted.</p>
</li>
<li><p>Supervised learning relies on the sizes of dataset; what if we have no sufficient data?</p>
<ul>
<li>Data augmentation, learning from insufficient labels (weak supervision)</li>
</ul>
</li>
<li><p>What if the data properties are different between datasets?</p>
<ul>
<li>Domain adaptation, transfer learning</li>
</ul>
</li>
</ul>
<h3 id="problem-formulation"><strong>Problem formulation</strong></h3>
<ul>
<li>$$X = R^d$$ is an input space<ul>
<li>$$X = R^d$$ : a d-dimensional Euclidean space</li>
<li>Input vector $$x ∈ X : x = (x_1,...,x_d)$$</li>
</ul>
</li>
<li>Y is an output space (binary decision)</li>
<li>We want to approximate a target function f<ul>
<li>f : X ➔ Y (unknown ideal function)</li>
<li>Data $$(x^1, y^1),...,(x^N, y^N)$$; dataset where $$y^N = f(X^N)$$</li>
<li>Correct label is ready for a training set</li>
<li>Hypothesis $$g : X ➔ Y$$ (ML model to approximate $$f$$) : $$g ∈ H$$</li>
</ul>
</li>
<li>Learning model : feature selection, model selection, optimization</li>
</ul>
<h3 id="model-generalization"><strong>Model generalization</strong></h3>
<ul>
<li>Learning is an ill-posed problem; data is limited to find a unique solution</li>
<li>Generalization (goal) : a model needs to perform well on unseen data<ul>
<li>Generalization error E<sub>gen</sub>; the goal is to minimize this error, but it is impractical to compute in the real world</li>
<li>Use training/validation/test set errors for the proxy</li>
</ul>
</li>
</ul>
<h3 id="errors"><strong>Errors</strong></h3>
<ul>
<li>Pointwise error is measured on an each input sample : $$e(h(x), y)$$</li>
<li>From a pointwise error to overall errors: $$E[h(x^i) - y^i)^2]$$<ul>
<li>If an input sample is chosen from training, validation, and testing datasets, the errors are called a training error (E<sub>train</sub>), a validation error (E<sub>val</sub>), and a testing error (E<sub>test</sub>).</li>
</ul>
</li>
<li>Training error E<sub>train</sub> measured on a training set, which may or may not represent E<sub>gen</sub>; used for fitting a model</li>
<li>Testing error E<sub>test</sub> (not used in training), which can be used for a proxy of E<sub>gen</sub>.</li>
<li>Goal :  E<sub>test</sub> <strong>≈</strong> E<sub>gen</sub> <strong>≈</strong> 0</li>
</ul>
<h3 id="overfitting-and-underfitting"><strong>Overfitting and Underfitting</strong></h3>
<ul>
<li>Underfitting problem because of using too simpler model than actual data distribution (<strong>high bias</strong>)</li>
<li>Overfitting problem because of more complex model than actual data distribution (<strong>high variance</strong>)<ul>
<li><strong>Avoid overfitting</strong><ul>
<li>Problem : In today&#39;s ML problems, a complex model tends to be used to handle high-dimensional data (and relatively insufficient number of data); prone to an overfitting problem</li>
<li>Curse of dimension : Will you increase the dimension of the data to improve the performance as well as maintain the density of the examples per bin? If so, you need to increase the data exponentially.</li>
<li>Remedy : Data augmentation, regularization, ensemble</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="bias-and-variance"><strong>Bias and Variance</strong></h3>
<ul>
<li>Bias : error because the model can not represent the concept</li>
<li>Variance : error because a model overreacts to small changes (noise) in the training data</li>
</ul>
<p>➔ Total loss = Bias + Variance (+ noise)</p>
<p>➔ Bias-variance trade-off : the two objective have trade-off between approximation and generalization w.r.t model complexity</p>
<h3 id="cross-validation-cv"><strong>Cross-validation (CV)</strong></h3>
<ul>
<li>CV allows a better model to avoid overfitting (but more complexity)</li>
</ul>
<hr>
<h2 id="linear-regression"><strong>Linear Regression</strong></h2>
<ul>
<li>Hypothesis set H, model parameter &theta;</li>
</ul>
<p>$$
h_\theta(x)=\theta_0+\theta_1x_1+...+\theta_dx_d=\theta^Tx
$$</p>
<ul>
<li>Good for a first try : simplicity, generalization</li>
<li>L<sub>2</sub> cost function (Goal : minimizing MSE)</li>
</ul>
<p>$$
J(\theta_0,\theta_1)=\frac{1}{2m}\sum_{i=1}^{m}(h_\theta(x^{(i)})-y^{(i)})^2
$$</p>
<p>➔ $$minimize_{\theta_0, \theta_1} J(\theta_0, \theta_1)$$</p>
<h3 id="optimization"><strong>Optimization</strong></h3>
<ul>
<li>Data matrix X ∈ R<sup>Nx(d+1)</sup>, target vector y ∈ R<sup>N</sup>, weight vector &theta; ∈ R<sup>d+1</sup></li>
<li>In-sample error : $$\mid\mid{y - X\theta}\mid\mid_2$$</li>
<li>Normal equation<ul>
<li>(Least square) E is continuous, differentiable, and convex</li>
<li>Need to compute an inverse matrix and slow if the number of samples is very large</li>
</ul>
</li>
</ul>
<p>$$
\theta^{*}=\text{argmin}_{\theta}E(\theta)
$$</p>
<p>$$
=\text{argmin}_{\theta}[\frac{1}{N}(\theta^TX^TX\theta-2\theta^TX^Ty+y^Ty)]
$$</p>
<p>$$
\therefore\theta^{*}=(X^TX)^{-1}X^Ty=X^{+}y
$$</p>
<p>➔ Problem : huge computational complexity, non-invertible matrix ➔ needs iterative algorithm (gradient descent)   </p>
<ul>
<li>To avoid overfitting<ul>
<li>If we have too many features, the hypothesis may fit the training set very well. However, it may fail to generalize to new samples.</li>
<li>More features ➔ more parameters ➔ need more data; (in practive) less data ➔ overfitting
➔ Reduce the number of features, regularization</li>
</ul>
</li>
</ul>
<h4 id="gradient-descent-algorithm"><strong>Gradient descent algorithm</strong></h4>
<p>$$
\theta_{new}=\theta_{old}-\alpha\frac{\partial}{\partial\theta}J(\theta)
$$</p>
<ul>
<li>J is the objective function that we want to optimize. &alpha; : the step size to control the rate to move down the error surface (hyper parameter).<ul>
<li>If &alpha; is too small, gradient descent can be slow.</li>
<li>If &alpha; is too large, gradient descent can overshoot the minimum.</li>
</ul>
</li>
<li>Gradient descent works well even when n large</li>
<li>All examples (batch) are examined at each iteration : Use stochastic gradient descent or mini batch.</li>
<li>Advances : AdaGrad, RMSProp, Adam</li>
<li>Limitation : local optimum ➔ cannot guarantee global minimum but attempt to find a good local minimum<ul>
<li>To avoid local minimum<ul>
<li>Momentum : designed to speed up learning in high curvature and small/noise gradients ➔ exponentially weighted moving average of past gradients (low pass filtering)</li>
<li>SGD + momentum : use a velocity as a weighted moving average of previous gradients</li>
<li>Nesterov momentum : difference from standard momentum where gradient g is evaluated (lookahead gradient step)</li>
<li>AdaGrad : adapts an indibidual learning rate of each direction</li>
<li>RMSProp : attempts to fix the drawbacks of AdaGrad, in which the learning rate becomes infinitesimally small and the algorithm is no longer able learning when the accumulated gradient is large.</li>
<li>Adam : RMSProp + momentum</li>
</ul>
</li>
</ul>
</li>
<li>Learning rate scheduling</li>
</ul>
<p>➔ Need to gradually decrease learning rate over time</p>
<hr>
<h2 id="linear-classification"><strong>Linear Classification</strong></h2>
<ul>
<li>Uses a hyperplane as a decision boundary to classify samples based on a linear combination of its explanatory variables</li>
<li>The linear formula g ∈ H can be written as:</li>
</ul>
<p>$$
h(x)=sign((\sum^{d}_{i=1}w_ix_i)+w_0)
$$</p>
<p>➔ $$x_0 = 1, w_0$$ : a bias term, $$sign(x) = 1{\quad}if{\quad}x&gt;0;0{\quad}if{\quad}x&lt;0;$$</p>
<ul>
<li>Sigmoid function<ul>
<li>Used to map a score value into a probability value.</li>
<li>Squash the output of the linear function:</li>
</ul>
</li>
</ul>
<p>$$
\sigma(-w^Tx)=\frac{1}{1+e^{-w^Tx}}
$$</p>
<ul>
<li>Advantage : simplicity and interpretability</li>
</ul>
<h3 id="loss"><strong>Loss</strong></h3>
<ul>
<li>Zero-one loss</li>
</ul>
<p>$$
\text{Loss}_{0-1}(x,y,w)=1[(w\cdot\phi(x))0\ge{y}]
$$</p>
<ul>
<li>Hinge loss</li>
</ul>
<p>$$
\text{Loss}_\text{hinge}(x,y,w)=\text{max}{1-(w\cdot\phi(x))y,0}
$$</p>
<ul>
<li>Cross-entropy loss<ul>
<li>Considers two probability mass functions (pmf) {p, 1-p} and {q, 1-q} with a binary outcomes:</li>
<li>Cross-entropy loss measures the performance of a classification model whose output is a probability value between 0 and 1.</li>
</ul>
</li>
</ul>
<p>$$
CE(S,Y)=-\sum_{\forall{i}}Y_i\text{log}(S_i))
$$</p>
<hr>
<h2 id="support-vector-machine-svm"><strong>Support vector machine (SVM)</strong></h2>
<ul>
<li>Choose the linear separator (hyperplane) with the largest margin on either side<ul>
<li>Maximum margin hyperplane with support vectors</li>
<li>Robust to outliers</li>
</ul>
</li>
</ul>
<h3 id="support-vector"><strong>Support Vector</strong></h3>
<ul>
<li>an instance with the minimum margin, which will be the most sensible data points to affect the performance</li>
</ul>
<h3 id="margin"><strong>Margin</strong></h3>
<ul>
<li><p>twice the distance from the hyperplane to the nearest instance on either side</p>
</li>
<li><p>w : orthogonal to the hyperplane</p>
</li>
</ul>
<h3 id="optimization-1"><strong>Optimization</strong></h3>
<ul>
<li>Optimal weight w and bias b</li>
<li>Classifiers points correctly as well as achieves the largest possible margin</li>
<li>Hard margin SVM assumes linear separability</li>
<li>Soft margin SVM extends to non-separable cases</li>
</ul>
<p>➔ Nonlinear transformation and kernel trick</p>
<ul>
<li>Constraints : linearly separable, hard-margin linear SVM</li>
</ul>
<p>$$
h(x)=w^Tx+b\geq1\text{ for }y=1
$$</p>
<p>$$
h(x)=w^Tx+b\leqq-1\text{ for }y=-1
$$</p>
<p>$$
y(w^Tx+b)\geq1\text{ for all samples}
$$</p>
<ul>
<li>Objective function : linearly separable, hard-margin linear SVM<ul>
<li>Distance from a support vector to the hyper plane:</li>
</ul>
</li>
</ul>
<p>$$
\frac{w^Tx+b}{\mid\mid{w}\mid\mid}=\frac{\pm1}{\mid\mid{w}\mid\mid}\longrightarrow\frac{2}{\mid\mid{w}\mid\mid}
$$</p>
<h3 id="kernel-trick-not-linearly-separable"><strong>Kernel trick (not linearly separable)</strong></h3>
<ul>
<li>Polynomials:</li>
</ul>
<p>$$
K(x,y)=(x\cdot{y}+1)^p
$$</p>
<ul>
<li>Gaussian radial basis function (RBF):</li>
</ul>
<p>$$
K(x,y)=e^{-\mid\mid{x-y}\mid\mid^2/2\sigma^2}
$$</p>
<ul>
<li>Hyperbolic tangent (multilayer perceptron kernel):</li>
</ul>
<p>$$
K(x,y)=\text{tanh}(kx\cdot{y}-\delta)
$$</p>
<hr>
<h2 id="artificial-neural-network-ann"><strong>Artificial neural network (ANN)</strong></h2>
<ul>
<li><p>Needs elaborated training schemes to improve performance</p>
</li>
<li><p>Activation functions</p>
<ul>
<li>Sigmoid neurons give a real-valued output that is a smooth and bounded function of their total input</li>
<li>Non-linearity due to the activation functions</li>
</ul>
</li>
<li><p>Deep neural network can represent more complex (non-linear) boundaries with increasing neurons</p>
</li>
<li><p>Multilayer perceptron (MLP)</p>
<ul>
<li>can solve XOR problem</li>
</ul>
</li>
<li><p>ANN for non-linear problem</p>
<ul>
<li>There exists cases when the accuracy is low even if the number of layers is high</li>
</ul>
</li>
</ul>
<p>➔ The result of one ANN is the result of sigmoid function</p>
<p>➔ The numerous multiplication of this result converges to near zero ➔ Gradient vanishing problem</p>
<h3 id="back-propagation"><strong>Back propagation</strong></h3>
<ul>
<li>Back propagation barely changes lower-layer parameters (vanishing gradient)</li>
<li>Breakthrough<ul>
<li>Pre-training + fine tuning</li>
<li>CNN for reducing redundant parameters</li>
<li>Rectified linear unit (constant gradient propagation)</li>
<li>Dropout</li>
</ul>
</li>
</ul>
<hr>
<h3 id="performance-evaluation"><strong>Performance evaluation</strong></h3>
<ul>
<li>Accuracy = (TP+TN)/ALL</li>
<li>Precision = TP/(TP+FP)</li>
<li>Recall = TP/(TP+FN)</li>
<li>F1 = PxR/(P+R)</li>
<li>TPR = R = TP/(TP+FN)</li>
<li>TNR = TN/(TN+FP)</li>
<li>False positive error : predict = positive, actual = negative</li>
<li>False negative error : predict = negative, actual = positive</li>
<li>ROC Curve : performance comparisons between different classifiers in different true positive rates (TPR) and true negative rates (TNR).</li>
</ul>
<h3 id="error-measure"><strong>Error measure</strong></h3>
<ul>
<li>The error measure should be specified by the user ➔ Not always given but needs to be carefully considered</li>
</ul>
<hr>
<h2 id="ensemble-learning"><strong>Ensemble learning</strong></h2>
<ul>
<li>Predict class label for unseen data by aggregating a set of predictions : different classifiers (experts) learned from the training data</li>
<li>Make a decision with a voting</li>
<li>Bagging and boosting : improving decision tree<ul>
<li>By bagging : random forest (inherently boosting)</li>
<li>By boosting : gradient boosting machine as generalized Adaboost</li>
</ul>
</li>
<li>Advantages<ul>
<li>Improve predictive performance, Other types of classifiers can be directly included</li>
<li>Easy to implement, No too much parameter tuning</li>
</ul>
</li>
<li>Disadvantages<ul>
<li>Not a compact representation</li>
</ul>
</li>
</ul>
<h4 id="bagging"><strong>Bagging</strong></h4>
<ul>
<li>Bootstrapping + aggregating (for more robust performance; lower variance)</li>
<li>Train several models in parallel</li>
<li>Bagging works because it reduces variance by voting/averaging (robust to overfitting)<ul>
<li>Learning algorithm is unstable; if small changes to the training set cause large changes in the learned classifier.</li>
<li>Usually, the more classifiers the better</li>
</ul>
</li>
</ul>
<h4 id="boosting"><strong>Boosting</strong></h4>
<ul>
<li>Cascading of week classifiers ➔ training multiple models in sequence, adaboost<ul>
<li>Adaboost : trained on weighted form of the training set, weight depends on the performance of the previous classifier, combined to give the final classifier</li>
</ul>
</li>
<li>Simple and easy, flexible, Versatile, non-parametric</li>
<li>No prior knowledge needed about week learner</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<ul>
<li>본 포스팅은 <code>LG Aimers</code> 프로그램에 참가하여 학습한 내용을 기반으로 작성되었습니다. (전체내용 X)</li>
</ul>
<p>➔ <a href="https://www.lgaimers.ai/"><code>LG Aimers</code> 바로가기</a></p>
<pre><code>[1] LG Aimers AI Essential Course Module 2.지도학습(분류/회귀), 이화여자대학교 강제원 교수 </code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[신뢰성 | Reliability 이해하기]]></title>
            <link>https://velog.io/@standing-o/reliability</link>
            <guid>https://velog.io/@standing-o/reliability</guid>
            <pubDate>Tue, 23 Aug 2022 13:19:05 GMT</pubDate>
            <description><![CDATA[<h1 id="신뢰성--reliability-이해하기"><strong>신뢰성 | Reliability 이해하기</strong></h1>
<ul>
<li>본 포스팅은 신뢰성의 개념과 척도, 그리고 여러 분포들에 대한 내용을 포함하고 있습니다.</li>
<li>Keyword : 신뢰성, 신뢰성 척도, 보전도</li>
<li>👉 <a href="https://standing-o.github.io/posts/reliability/"><code>Click</code></a></li>
</ul>
<hr>
<h2 id="신뢰성-supreliabilitysup"><strong>신뢰성 <sup>Reliability</sup></strong></h2>
<ul>
<li>주어진 작동 환경에서 주어진 시간 동안 시스템이 고유의 기능을 수행할 확률</li>
<li>중요성 : 제품 라이프 사이클 관점의 total cost 관리 필요</li>
</ul>
<p>➔ 품질 비용은 잠재 risk</p>
<p>➔ 개발 단계에서 시장품질은 예측 가능하고 control 되어야함</p>
<h3 id="품질-vs-신뢰성"><strong>품질 vs 신뢰성</strong></h3>
<table>
<thead>
<tr>
<th align="center">항목</th>
<th align="center"><strong>품질</strong></th>
<th align="center"><strong>신뢰성</strong></th>
</tr>
</thead>
<tbody><tr>
<td align="center">시간</td>
<td align="center">정적(현시점에서 제품의 특성)</td>
<td align="center">동적(미래의 성능과 고장)</td>
</tr>
<tr>
<td align="center">추진</td>
<td align="center">전사적 추진, 주로 생산단계</td>
<td align="center">전문 기술자 팀에 의해 추전, 설계 및 개발단계</td>
</tr>
<tr>
<td align="center">자료</td>
<td align="center">완전자료</td>
<td align="center">불완전자료(관측 중단자료)</td>
</tr>
<tr>
<td align="center">개선 tool</td>
<td align="center">SPC, RQC, 식스시그마</td>
<td align="center">FMEA, FTA, 고장해석</td>
</tr>
<tr>
<td align="center">시험 시간</td>
<td align="center">단기</td>
<td align="center">장기</td>
</tr>
<tr>
<td align="center">척도</td>
<td align="center">불량율, 평균, 분산</td>
<td align="center">고장률, 수명, 신뢰도</td>
</tr>
<tr>
<td align="center">분포</td>
<td align="center">이항분포, 정규분포</td>
<td align="center">지수분포, 와이블분포, 대수정규분포</td>
</tr>
</tbody></table>
<h3 id="신뢰성-분석"><strong>신뢰성 분석</strong></h3>
<ul>
<li>필요성 : 패러다임의 변화<ul>
<li>고장의 원인 : 취약한 설계, 과부하, 강도와 부하의 산포, 마모, 시간 매커니즘, 잠재된 오작동, 오류</li>
</ul>
</li>
</ul>
<h3 id="신뢰성-공학의-학문적-발전"><strong>신뢰성 공학의 학문적 발전</strong></h3>
<ul>
<li>1950년대 후반 ~ 1960년대<ul>
<li>확률분포로 고장현상 모형화, 지수분포에 관한 통계적 분석법 개발, NASA 창설, FMEA/FTA 개발 및 활용</li>
<li>국제전기기술위원회 내에 장치와 부품의 신뢰성 기술위원회 발족, 와이블 분포에 관한 통계적 분석법 개발, 신뢰성 샘플링 검사방식 개발</li>
</ul>
</li>
<li>1970년대<ul>
<li>원자력 발전소를 비롯한 복합시스템에 초점을 맞춘 신뢰도와 안전을 고려한 위험분석</li>
<li>RCM을 비롯한 설비보전, 소프트웨어 신뢰도 분야 등으로 확대</li>
<li>FTA가 항공우주분야나 핵발전소 분야에서 활발히 연구되고 활용</li>
<li>충격 모형의 연구와 네트워크 신뢰도 연구 시작</li>
</ul>
</li>
<li>1980년대<ul>
<li>가속 수명 시험의 설계, 분석 방법론 연구 및 개발, 신뢰성 데이터 분석에서의 베이지안 방법론 응용, 네트워크 신뢰성 분야 연구</li>
<li>공통원인 고장 모형 및 분석이 대형 복잡시스템에서 활발하게 연구</li>
</ul>
</li>
<li>1990년대<ul>
<li>열화시험방법과 분석법 개발, 초가속수명시험과 강건설계, 실험계획법 도입, 부하분석 및 열화과정 해석을 통한 정확한 의사결정</li>
</ul>
</li>
<li>2000년대<ul>
<li>고장물리 분야가 더욱 다양하게 연구, 대형 시스템 분석, 시뮬레이션 방법론의 응용, 안전성 및 위험분석과의 통합화</li>
<li>재료, 부품, 장비 고장 예측진단 기술의 필요에 관한 PHM가 매우 크게 대두</li>
</ul>
</li>
<li>21세기<ul>
<li>기계학습과 강화학습 등을 통한 신뢰성 분석과 예측 연구가 활발</li>
<li>신뢰성 공학의 대상이 거대한 네트워크 시스템으로 확대됨 ➔ 긴급회복성 <sup>resilience measure</sup>과 같은 일시적 돌발현상에서의 대응능력에 관한 지표들이 정의</li>
<li>RAM ➔ RAMS 로 확대적용</li>
<li>기능안전성 <sup>Functional safety</sup> : 위험사건을 예방하기 위해 매우 높은 안전 방호시스템이 요구되는 산업부분에서 일반화됨</li>
</ul>
</li>
</ul>
<h3 id="신뢰성-척도"><strong>신뢰성 척도</strong></h3>
<ul>
<li>신뢰도 <sup>Reliability</sup> : 부품, 제품, 시스템 등이 주어진 사용 조건에서 일정 기간동안 요구되는 기능을 고장 없이 수행할 확률</li>
</ul>
<p>➔ 시구간 $$[0, t]$$ 동안 고장나지 않을 확률</p>
<ul>
<li>고장률 <sup>Failure rate</sup><ul>
<li>순간 고장율 <sup>Instantaneous failure rate, hazard rate</sup> : 어떤 시점까지 동작하고 있는 시스템이 계속되는 단위시간 동안 고장을 일으킬 비율</li>
<li>평균 고장율 <sup>Average failure rate</sup> : 총 동작시간 동안의 고장개수</li>
</ul>
</li>
</ul>
<p>➔ 시점 t에서 작동하는 부품이 시구간 $$[t, t+\Delta]$$에서 고장날 확률</p>
<ul>
<li>평균 고장시간 <sup>Mean-time-to-failure</sup> : 수리불가시스템 <sup>Non-repairable system</sup>에서 고장이 발생하기 까지의 평균시간</li>
<li>평균 고장간격 <sup>Mean-time-between-failure</sup> : 수리가능시스템 <sup>Repairable system</sup>에서 고장간격 간의 평균 동작시간</li>
<li>평균 잔여수명 <sup>Mean residual life</sup> : 자동차, 선박, 항공기 등의 중고제품을 구입할 경우, 향후 얼마나 더 사용할 수 있는지 평가하는 척도</li>
<li>보전도 <sup>Maintainability</sup> : 고장난 시스템이 주어진 조건 하에서 규정된 시간 내에 수리(보전)을 완료할 확률</li>
<li>가용도 <sup>Availability</sup> : 수리 가능한 시스템이 어떤 특정 시점에 기능을 유지하고 있을 확률</li>
</ul>
<h3 id="신뢰성-데이터"><strong>신뢰성 데이터</strong></h3>
<ul>
<li>수명데이터 : 의도된 기능을 제대로 수행하고 있거나 고장인지의 여부로 판정 (binary data)</li>
<li>성능데이터 : 시간 경과에 따른 제품의 성능을 측정 (continuous data)</li>
</ul>
<h3 id="연속형-수명분포"><strong>연속형 수명분포</strong></h3>
<ul>
<li><p>지수 분포 <sup>Exponential distribution</sup></p>
<ul>
<li>$$f(t) = {\lambda}e^{-\lambda{t}}$$, $$F(t) = 1-e^{\lambda{t}}$$ (연속형)</li>
<li>시간 t에 관계없이 원래의 평균수명과 동일 ➔ 지수분포를 따르는 제품은 작동하는 동안에는 늘 새것과 같음 ➔ 망각성 <sup>memoryless property</sup></li>
<li>Relation to the poisson process : 지수분포는 일반적으로 사건이 1건 발생하는데 걸리는 시간에 대한 분포로 사용됨</li>
<li>지수분포를 따르는 제품에 대한 고려사항<ul>
<li>사용된 제품은 확률적으로 새 것과 같기 때문에 작동하고 있는 부품을 예방보전의 목적으로 미리 교체할 아무런 이유가 없다.</li>
<li>신뢰도 함수, 고장까지의 평균시간 등의 추정은 관측시점에서 부품들의 총 작동시간과 고장의 수에 대한 데이터를 수집하는것으로 충분</li>
<li>Drenick thm : 복잡한 기기나 시스템의 수명분포는 비교적 넓은 조건하에서 근사적으로 지수분포를 따옴</li>
</ul>
</li>
</ul>
</li>
<li><p>감마 분포 <sup>Erlang distribution</sup></p>
<ul>
<li><p>서로 독립인 확률변수 $$X_1,...,X_k$$가 모수 $$\lambda&gt;0$$인 지수분포를 따를때 $$Y = \sum{X_i}$$는 감마분포를 따른다.</p>
</li>
<li><p>$$Y ~ Gamma(k, \lambda)$$ where K is positive integer, $$\lambda&gt;0$$, $$f(y) = \frac{\lambda^k{y^{k-1}}e^{-\lambda{y}}}{(k-1)!}$$ if $$y&gt;0$$; $$0{\quad}otherwise$$</p>
</li>
<li><p>지수분포의 일반화 형태 (poisson 분포를 따를때 연속적으로 k개의 사건이 발생할때까지 걸린 시간)</p>
</li>
</ul>
</li>
<li><p>와이블 분포 <sup>Weibull distribution</sup></p>
<ul>
<li>$$X ~ weibull(\alpha, \beta)$$, $$\alpha&gt;0$$ and $$\beta&gt;0$$, $$f(x) = \beta\alpha^{\beta}x^{\beta-1}e^{-(\alpha{x})^\beta}$$ if $$x&gt;0$$, $$0{\quad}otherwise$$</li>
<li>If $$\beta=1$$, 지수분포와 동일</li>
<li>Reliability information : 시간에 따른 제품의 동작확률을 와이블 분포로 모형화 함으로써 제품의 수명에 대한 다양한 정보를 획득</li>
<li>최약 연결 <sup>weakest link</sup> 의 법칙 : 독립적이고 동일한 분포를 따르는 여러 개의 비음의 확률변수들이 있을 때, 이 중 최소인 확률변수의 분포는 와이블 분포를 따름</li>
<li>부품 <sup>components or parts</sup>의 수명분포에 주로 사용됨</li>
</ul>
</li>
<li><p>정규 분포 <sup>Normal distribution</sup></p>
<ul>
<li>중심이 &mu;이고 좌우대칭인 종모양의 형태, 분산 &sigma;<sup>2</sup>는 분포의 넓고 좁은 정도(산포)를 결정하는 모수</li>
<li>표준정규분포 : 평균이 0이고 분산이 1인 정규분포</li>
<li>정규분포를 따르는 n개의 표본으로부터 얻어진 평균의 분포는 평균 &mu;, 분산 &sigma;<sup>2</sup>/n인 정규분포를 따름 i.e., n개의 표본이 추출되는 데이터 분포가 정규분포가 아닌 다른 임의의 분포로 가정하여도 그 표본평균의 분포는 정규분포를 따름</li>
<li>표본이 추출된 분포의 평균 &mu;, 분산 &sigma;<sup>2</sup>이 존재할 때, 표본 수 n이 충분히 크면 표본의 평균 또는 표본의 합은 근사적으로 정규분포를 따름</li>
</ul>
</li>
<li><p>대수정규 분포 <sup>Lognormal distribution</sup></p>
<ul>
<li>다양한 형태의 분포를 표현할 수 있기 때문에 고장 데이터 등을 모형화 하는 경험적 모형으로 폭넓게 사용됨</li>
<li>곱셈형 충격의 누적효과로 인해 고장이 발생하는 현상에 대해서 대수 정규 분포가 유도됨을 보일 수 있음</li>
</ul>
</li>
</ul>
<h3 id="이산형-분포"><strong>이산형 분포</strong></h3>
<ul>
<li>이항 분포 <sup>Binomial distribution</sup><ul>
<li>n개의 독립적인 베르누이 시행을 $$X_1,...,X_n$$라 하고, 각각 X가 1일 확률을 P라 하면, $$Y = X_1 + ... +  X_n$$은 모수 n과 p를 갖는 이항분포를 가짐</li>
<li>$$P(Y = y) = \binom{n}{y}{p^y}(1-p)^{n-y}$$, $$x = 0,1,2,...,n$$</li>
</ul>
</li>
<li>포아송 분포 <sup>Poisson distribution</sup><ul>
<li>단위시간동안 발생빈도가 &lambda;인 포아송 프로세스를 따르는 사건의 발생 횟수를 X, 주어진 t시간동안 발생하는 사건의 수는 평균 &mu; = &lambda;t인 포아송 분포를 따름</li>
<li>$$P(X = x) = \frac{\mu^{x}e^{-\mu}}{x!}$$, where $$x=1,2,...$$</li>
</ul>
</li>
</ul>
<h3 id="보전도-supmaintainabilitysup"><strong>보전도 <sup>Maintainability</sup></strong></h3>
<ul>
<li>보전의 목적 : 안전하고 경제적으로 운전될 수 있는 조건으로 장비유지</li>
<li>사후보전 <sup>Breakdown maintenance</sup> : 점검 및 정기교환을 전혀 하지 않고 장비고장 후 수리</li>
<li>시간 기준보전 <sup>Time based maintenance</sup> : 장비의 열화에 가장 비례하는 파라미터 (생산성, 작동회수 등)로서 수리주기 (이론값, 경험값)을 정하고 주기까지 사용 시 무조건 수리함</li>
<li>상태 기반보전 <sup>Condition based maintenance</sup> : 장비 열화 상태를 각 측정 데이터와 그 해석에 따라서 오프라인 혹은 온라인 상태로 파악하며, 열화를 나타내는 값이 미리 정한 열화 기준에 달하면 수리<ul>
<li>목적, 유닛/부품 단위 ➔ 성능열화상태 ➔ 파라미터 ➔ 파라미터 측정법 ➔ 정기적 장비 이상 측정 ➔ 파라미터 기능 열화간 상관관계 ➔ threshold 설정 ➔ 현물 분해조사 ➔ 상관관계 입증 ➔ 경향관리 시스템 구축</li>
</ul>
</li>
</ul>
<hr>
<h4 id="references"><strong>References</strong></h4>
<ul>
<li>본 포스팅은 <code>LG Aimers</code> 프로그램에 참가하여 학습한 내용을 기반으로 작성되었습니다. (전체내용 X)</li>
</ul>
<p>➔ <a href="https://www.lgaimers.ai/"><code>LG Aimers</code> 바로가기</a></p>
<pre><code>[1] LG Aimers AI Essential Course Module 1.품질과 신뢰성, 한양대학교 배석주 교수</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[품질 | Quality 의 모든 것]]></title>
            <link>https://velog.io/@standing-o/quality</link>
            <guid>https://velog.io/@standing-o/quality</guid>
            <pubDate>Tue, 23 Aug 2022 13:17:50 GMT</pubDate>
            <description><![CDATA[<h1 id="품질--quality-의-모든-것"><strong>품질 | Quality 의 모든 것</strong></h1>
<ul>
<li>본 포스팅은 품질의 개념과 유형, 그리고 품질비용에 대한 내용을 포함하고 있습니다.</li>
<li>Keyword : 품질, 품질비용, SPC, 품질경영</li>
<li>👉 <a href="https://standing-o.github.io/posts/quaility/"><code>Click</code></a></li>
</ul>
<hr>
<h2 id="품질-quality"><strong>품질 (Quality)</strong></h2>
<ul>
<li>규격에 부합하는 것 (in 전통적 품질관리)</li>
<li>제품특징 (판매 수익의 증대에 기여), 무결함 (원가절감에 기여) 로 구성됨</li>
<li>개념의 변화</li>
</ul>
<p>➔ 요구조건의 만족도 or 용도에 대한 적합성 <sup>fitness for use</sup>, 고객 기대에의 적응도</p>
<h3 id="품질의-유형"><strong>품질의 유형</strong></h3>
<ul>
<li>요구품질 <sup>Requirement of quality</sup></li>
</ul>
<p>➔ 제품/서비스를 사용하는 사람 입장에서 요구, 추상적 개념의 품질</p>
<ul>
<li>설계품질 <sup>Quality of design</sup></li>
</ul>
<p>➔ 기업의 품질방침 및 제조역량을 고려하여 추상적 요구 품질을 구체적으로 명문화</p>
<p>➔ 제약조건 (기술,비용) 과 경쟁제품의 품질 및 가격을 종합적으로 고려하여 결정됨</p>
<ul>
<li>제조품질 or 적합품질 <sup>Quality of manufacturing or conformance</sup></li>
</ul>
<p>➔ 제조시스템의 다양한 원천에서 발생하는 변동성과 불확실성에 의해 결정됨</p>
<ul>
<li>사용품질 or 시장품질 <sup>Quality of use or market</sup></li>
</ul>
<p>➔ 고객이 제품/서비스를 사용한 후 기본적 욕구의 충족, 애프터서비스, 보전, 신뢰성 등에 대한 만족/불만을 인식함으로써 결정됨</p>
<h3 id="품질의-차원"><strong>품질의 차원</strong></h3>
<ul>
<li>성능 <sup>Performance</sup>, 특징 <sup>Features</sup>, 신뢰성 <sup>Reliability</sup>, 적합성 <sup>Conformance</sup>, 내구성 <sup>Durability</sup>, 서비스성 <sup>Serviceability</sup>, 심미성 <sup>esthetics</sup>, 인지품질 <sup>Perceived quailty</sup></li>
</ul>
<h3 id="종합적-품질-suptotal-qualitysup"><strong>종합적 품질 <sup>Total Quality</sup></strong></h3>
<ul>
<li>고객을 만족시킬 수 있는 품질을 달성하기 위한 제조시스템의 가치사슬 <sup>value chain</sup>을 고려</li>
<li>QCD (Quality, cost, delivery)도 총체적 품질에 포함되어야함</li>
</ul>
<h3 id="저품질비용-supcopq-cost-of-poor-qualitysup"><strong>저품질비용 <sup>COPQ: Cost of poor quality</sup></strong></h3>
<ul>
<li>기업 내에서 불필요하게 발생하는 이익손실비용을 측정하는 재무적 척도, 기업이익에 기여하지 않는 모든 것</li>
<li>6시그마에서 개선 프로젝트의 대상</li>
<li>품질비용 + 숨겨진비용 <sup>Q-Cost + Hidden Cost</sup></li>
</ul>
<h3 id="품질로-인한-상승효과"><strong>품질로 인한 상승효과</strong></h3>
<ul>
<li>서비스 품질에 대한 소비자의 인식이 시장에서 거래될 수 있는 판매가격 결정</li>
<li>고품질 제공 ➔ 브랜드 인지도 상승 ➔ 높은 가격</li>
</ul>
<h3 id="품질비용"><strong>품질비용</strong></h3>
<h4 id="영향"><strong>영향</strong></h4>
<ul>
<li>품질 불만족으로 초래되는 고객 이탈율의 증가로 인한 기회손실 비용 발생</li>
<li>고객 이탈률 5% 줄이면 기업의 수익이 업종에 따라 25~82%까지 증가</li>
</ul>
<h4 id="종류"><strong>종류</strong></h4>
<ul>
<li><p>생산자 품질비용 (예방비용, 평가비용, 실패비용)</p>
<ul>
<li>예방비용 : 처음부터 불량이 발생하지 않도록 하는데 소요</li>
<li>평가비용 : 소정의 품질 수준을 유지하는데 소요</li>
<li>실패비용 : 소정의 품질 수준을 유지하는데 실패하여 발생</li>
</ul>
</li>
<li><p>사용자 품질비용 (소비자 부담비용, 소비자 불만비용, 명성상실 비용)</p>
</li>
<li><p>사회적 품질비용</p>
</li>
</ul>
<h4 id="새로운-개념"><strong>새로운 개념</strong></h4>
<ul>
<li>기존의 품질개념 : 검사에 의존하여 출하 품질을 보증함으로서 고품질을 확보하기 위해서는 검사, 재작업, 폐기 비용들의 loss 발생</li>
<li>새로운 품질개념: 불량을 만들지않는 프로세스를 구축함으로서 검사, 재작업, 폐기 비용등의 loss 발생X</li>
</ul>
<h3 id="산포"><strong>산포</strong></h3>
<ul>
<li>부적절한 제품 설계 ∩ 불안정한 원재료 ∩ 불충분한 공정능력</li>
</ul>
<h3 id="변동"><strong>변동</strong></h3>
<ul>
<li><p>잠재적 변동 이해를 위한 지침</p>
<ul>
<li>각각의 변동요인은 서로 동일하지 않음</li>
<li>제품 및 공정에서의 변동은 측정 가능해야함</li>
<li>개별적 출력의 결과는 예측 불가</li>
<li>원인에 대한 형태를 특정 출력 특성으로 계통화해야함</li>
<li>변동은 우연 <sup>random</sup>과 이상 <sup>assignable</sup> 원인으로 구분됨</li>
</ul>
</li>
<li><p>변동 원인에 따른 구분</p>
<ul>
<li>이상원인 <sup>chance cause</sup> : 비정상적인 요인에 의함, 각각의 개별적 요인에 의함, 큰 변동</li>
</ul>
<p>➔ 공정상에 이상변동만 존재하면, 분포상태는 시간에 대해 불안정되고 예측불가</p>
<ul>
<li>우연원인 <sup>assignable cause</sup> : 정상적인 운전상태에서는 존재하는 공정의 고유한 변동, 많은 개별적 요인에 의함, 작은 변동폭</li>
</ul>
<p>➔ 공정상에 우연변동만 존재하면, 분포상태는 시간에 대해 안정되고 예측가능</p>
</li>
</ul>
<h3 id="spc-statistical-process-control"><strong>SPC (Statistical, process, control)</strong></h3>
<ul>
<li>공정에서 요구되는 품질이나 생산성 목표를 달성하기 위하여 통계적 방법으로 공정을 효율적으로 운영해나가는 관리방법</li>
<li>장점<ul>
<li>결함방지에 효과적, 불필요한 공정조정 방지</li>
<li>공정 능력에 대한 정보를 제공, 입증된 생상성향상 기술</li>
<li>계량치(변수)데이터와 계수치(속성)데이터 모두에 사용가능</li>
</ul>
</li>
<li>단점<ul>
<li>데이터의 정확한 수집 및 올바른 관리도가 필요</li>
<li>관리도에 대한 올바른 분석과 패턴에 대한 적절한 조치가 필요</li>
<li>모든사람이 교육받을 필요성 존재</li>
</ul>
</li>
<li>SPC에서 사용되는 통계적 기법 : 평균, 분산 및 확률분포, 관리도 및 공정능력 지수, QC 7가지 기본도구</li>
</ul>
<p>➔ QC 7가지 기본도구 : 파레토차트 <sup>Stratification</sup>, 특성요인도<sup>Cause-and-effect-diagram</sup>, 체크시트<sup>Check sheet</sup>, 히스토그램<sup>Histogram</sup>, 산점도<sup>Scatter diagram</sup>, 그래프<sup>Graph</sup>, 관리도</p>
<h3 id="품질-40과-스마트-품질경영"><strong>품질 4.0과 스마트 품질경영</strong></h3>
<ul>
<li>전사적 품질관리 <sup> Total quality management</sup></li>
</ul>
<p>➔ 우수한 제품/서비스 등을 고객에게 제공하기 위해 품질에 중점을 두고 기업 전 부분의 참여를 통해 회사의 장기적성공에 목표를 두는 조직 전체의 노력</p>
<ul>
<li>품질 4.0<ul>
<li>빅데이터 : 크기 <sup> Volume</sup>,다양성 <sup>Variety</sup>,속도 <sup>Velocity</sup>, 정확성 <sup>Veracity</sup></li>
<li>Analytics : 설명적 <sup>Descriptive</sup>, 진단적 <sup>Diagnostics</sup>, 예측적 <sup> Predictive</sup>, 처방적 or 규범적 <sup>Prescriptive</sup></li>
<li>연결성 : IoT를 기반으로 실시간 작업자, 제품, 설비 및 프로세스들의 연결성 보장가능</li>
</ul>
</li>
</ul>
<p>➔ ICT 융합을 통해 종전의 사후검사 및 보증에서 벗어나 사전에 수집, 분석된 빅데이터를 활용하여 선제적 불량예지 및 보전 중심으로 진화된 품질경영시스템</p>
<ul>
<li>스마트 품질경영 혁신방안<ul>
<li>실시간 커뮤니티 피드백을 제공,원격진단 및 유지보수, 고도화된 공급망 품질관리</li>
</ul>
</li>
</ul>
<h3 id="빅데이터를-활용한-스마트-품질-경영"><strong>빅데이터를 활용한 스마트 품질 경영</strong></h3>
<ul>
<li>공정 모니터링 시스템의 품질 예측 및 불량요인 분석 알고리즘 개발<ul>
<li>공정변수와 품질 계측치의 상관관계를 파악할 수 있는 지표 도출 (원 공정변수를 군집분석 ➔ 변수선택법 ➔ 기여도 분석)</li>
<li>공정변수를 통하여 품질 계측치를 예측할 수 있는 가상 계측 시스템 구축 (회귀분석 ➔ 변수선택법 ➔ 주성분회귀 및 부분최소제곱회귀)</li>
<li>공정의 이상감지 및 진단 모니터링 기법</li>
</ul>
</li>
</ul>
<h3 id="스마트-공장"><strong>스마트 공장</strong></h3>
<ul>
<li>환경을 고려하고 안전성을 확보하면서 빠르고 역동적인 시장변화에 대하여 능동적으로 대응할 수 있는 지능형 디지털 시스템</li>
</ul>
<h3 id="품질관리-개선영역"><strong>품질관리 개선영역</strong></h3>
<ul>
<li>예방적 <sup>Preventive</sup> 품질관리</li>
</ul>
<p>➔ 전 과정 상에서 제품의 품질을 보장하기 위해 설계됨</p>
<ul>
<li>반응적 <sup>Reactive</sup> 품질관리</li>
</ul>
<p>➔ 제품 판매 이후의 품질관리</p>
<ul>
<li>품질관리 문화 : 기업 내 여러 부서간의 협업과 대화를 통해 합의를 이루어내야함</li>
</ul>
<h3 id="품질-vs-신뢰성"><strong>품질 VS 신뢰성</strong></h3>
<p>➔ <a href="https://standing-o.github.io/posts/reliability/#%ED%92%88%EC%A7%88-vs-%EC%8B%A0%EB%A2%B0%EC%84%B1">바로가기</a></p>
<hr>
<h4 id="references">References</h4>
<ul>
<li>본 포스팅은 <code>LG Aimers</code> 프로그램에 참가하여 학습한 내용을 기반으로 작성되었습니다. (전체내용 X)</li>
</ul>
<p>➔ <a href="https://www.lgaimers.ai/"><code>LG Aimers</code> 바로가기</a></p>
<pre><code>[1] LG Aimers AI Essential Course Module 1.품질과 신뢰성, 한양대학교 배석주 교수</code></pre>]]></description>
        </item>
        <item>
            <title><![CDATA[[Dacon] 음성 분류 경진대회]]></title>
            <link>https://velog.io/@standing-o/speech-classification</link>
            <guid>https://velog.io/@standing-o/speech-classification</guid>
            <pubDate>Tue, 23 Aug 2022 13:07:59 GMT</pubDate>
            <description><![CDATA[<h1 id="음성-분류"><strong>음성 분류</strong></h1>
<ul>
<li>본 포스팅은 음성 데이터에 대한 data augmentation과 feature extraction 등의 내용을 포함하고 있습니다.  </li>
<li>코드실행은 Google Colab의 CPU, Standard RAM 환경에서 진행했습니다.  </li>
<li>Keyword : 음성분류, classification, data-augmentation, feature-extraction
➔ <a href="https://dacon.io/competitions/official/235905/codeshare/5209">데이콘에서 읽기</a></li>
<li>👉<a href="https://standing-o.github.io/posts/speech-classification/"><code>Click</code></a></li>
</ul>
<pre><code class="language-python">from google.colab import drive
drive.mount(&#39;/content/drive&#39;)</code></pre>
<pre>
Mounted at /content/drive
</pre>

<h2 id="0-import-packages"><strong>0. Import Packages</strong></h2>
<pre><code class="language-python">import numpy as np
import pandas as pd
import random as rn
import os

from scipy.io import wavfile
import librosa

import matplotlib.pyplot as plt
import seaborn as sns
import IPython.display as ipd
import librosa.display

import warnings
warnings.filterwarnings(&quot;ignore&quot;)
%matplotlib inline</code></pre>
<pre><code class="language-python"># reproducibility

def all_seed(seed_num):
    np.random.seed(seed_num)
    rn.seed(seed_num)
    os.environ[&#39;PYTHONHASHSEED&#39;]=str(seed_num)
    # tf.random.set_seed(seed_num)

seed_num = 42
all_seed(seed_num)</code></pre>
<h2 id="1-load-and-explore-dataset"><strong>1. Load and explore dataset</strong></h2>
<pre><code class="language-python">train = pd.read_csv(&#39;/content/drive/MyDrive/Speech_classification/train.csv&#39;)
test = pd.read_csv(&#39;/content/drive/MyDrive/Speech_classification/test.csv&#39;)</code></pre>
<ul>
<li>📝 한 음성의 waveplot을 확인해보겠습니다.</li>
</ul>
<pre><code class="language-python">a_filename = &#39;/content/drive/MyDrive/Speech_classification/dataset/train/001.wav&#39;
samples, sample_rate = librosa.load(a_filename)

plt.figure(figsize=(10, 7))

# plt.plot(np.linspace(0, sample_rate/len(samples), len(samples)), samples)
librosa.display.waveplot(samples, sr=40000)

plt.xlabel(&#39;time&#39;, fontsize = 14)
plt.ylabel(&#39;amplitude&#39;, fontsize = 14)
plt.title(&#39;001.wav | Length : &#39; + str(len(samples)))

plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/05ae2fa8-fe7f-42b3-901e-3697fc0716f8/image.png" alt=""></p>
<pre><code class="language-python">print(sample_rate)
print(samples)</code></pre>
<pre>
22050
[0.00013066 0.00016804 0.00014106 ... 0.00017342 0.00017514 0.        ]
</pre>

<p>📝 한 음성의 spectrogram을 생성하겠습니다.<br>↪ Short term Fourier transform (STFT)의 magnitude를 db 스케일로 변환하여 spectrogram을 생성합니다.</p>
<pre><code class="language-python">samples, sample_rate = librosa.load(a_filename)
X = librosa.stft(samples)  # data -&gt; short term FT
Xdb = librosa.amplitude_to_db(abs(X))

plt.figure(figsize=(12, 3))
plt.title(&#39;001.wav spectrogram | Length : &#39; + str(len(samples)))
librosa.display.specshow(Xdb, sr = sample_rate, x_axis=&#39;time&#39;, y_axis=&#39;hz&#39;)   
plt.colorbar()
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/93a3fde1-d611-4dac-92a9-4db87e484587/image.png" alt=""></p>
<p>📝 <code>train.csv</code>에는 <code>train</code> 폴더의 음성 파일 이름과 라벨 컬럼이 포함되어 있습니다. <code>label</code> 컬럼은 0~9 정수로 구성됩니다.</p>
<pre><code class="language-python">train.head()</code></pre>
<pre>
  file_name  label
0   001.wav      9
1   002.wav      0
2   004.wav      1
3   005.wav      8
4   006.wav      0
</pre>

<pre><code class="language-python">print(train[&#39;label&#39;].unique())</code></pre>
<pre>
[9 0 1 8 7 4 5 2 6 3]
</pre>

<p>📝 데이터가 클래스 균형을 이루고 있습니다.</p>
<pre><code class="language-python">plt.figure(figsize=(12, 8))
sns.countplot(train[&#39;label&#39;])

plt.title(&quot;The number of recordings for each label&quot;)
plt.ylabel(&quot;Count&quot;, fontsize = 14)
plt.xlabel(&quot;Label&quot;, fontsize = 14)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/66ffa87d-b90a-4c73-b92d-b4fb00ec302e/image.png" alt=""></p>
<pre><code class="language-python">file_name = train[&#39;file_name&#39;]
train_path = &#39;/content/drive/MyDrive/Speech_classification/dataset/train/&#39;</code></pre>
<p>📝 데이터들의 길이가 모두 다릅니다.</p>
<pre><code class="language-python">all_shape = []
for f in file_name:
  data, sample_rate = librosa.load(train_path + f, sr = 20000)
  all_shape.append(data.shape)</code></pre>
<pre><code class="language-python">print(all_shape[:5])
print(&quot;Max :&quot;, np.max(all_shape, axis = 0))
print(&quot;Min :&quot;, np.min(all_shape, axis = 0))</code></pre>
<pre>
[(12740,), (13126,), (12910,), (9753,), (17572,)]
Max : [19466]
Min : [7139]
</pre>

<h2 id="2-data-augmentation"><strong>2. Data augmentation</strong></h2>
<p>📝 원래의 음성데이터에 새로운 perturbation 들을 추가하여 새로운 음성데이터를 생성합니다. (모델의 일반화 능력 향상을 위함)<br>↪ Noise 추가, time stretching, pitch 변환</p>
<pre><code class="language-python"># noise 추가
def noise(sample):
    noise_amp = 0.01*np.random.uniform()*np.amax(sample)
    sample = sample + noise_amp*np.random.normal(size = sample.shape[0])
    return sample

# time stretching
def stretch(sample, rate = 0.8):
    stretch_sample = librosa.effects.time_stretch(sample, rate)
    return stretch_sample

# pitch 변환
def pitch(sample, sampling_rate, pitch_factor = 0.8):
    pitch_sample = librosa.effects.pitch_shift(sample, sampling_rate, pitch_factor)
    return pitch_sample</code></pre>
<h2 id="3-feature-extraction"><strong>3. Feature Extraction</strong></h2>
<p>📝 모델링에 사용하면 도움이 될만한 몇가지 feature extraction 방법을 소개하겠습니다.</p>
<h4 id="1-zero-crossing-rate-zcr"><strong>1. Zero Crossing Rate (ZCR)</strong></h4>
<p>↪ 특정 프레임이 지속 기간 동안의 신호의 부호(sign) 변화율 i.e. 신호의 부호가 바뀌는 비율</p>
<h4 id="2-chroma_shift"><strong>2. Chroma_shift</strong></h4>
<p>↪ Waveform 또는 power spectrogram으로 생성한 chromagram. </p>
<h4 id="3-mel-spectrum"><strong>3. Mel spectrum</strong></h4>
<p>↪ 오디오 신호(time domain)에 Fast Fourier Transform (FFT) -&gt; Spectrum (frequency domain)  </p>
<p>↪ Spectrum + 필터링 (Mel filter bank) -&gt; Mel spectrum  </p>
<h4 id="4-mfcc-mel-frequency-cepstral-coefficient"><strong>4. MFCC (Mel-Frequency Cepstral Coefficient)</strong></h4>
<p>↪ Mel spectrum에서 Cepstral 분석을 통해 고유한 특성을 추출함</p>
<h4 id="5-rms-root-mean-square"><strong>5. RMS (Root Mean Square)</strong></h4>
<p>↪ 오디오 평균 음량 측정</p>
<pre><code class="language-python">def extract_features(sample):
    # ZCR
    result = np.array([])
    zcr = np.mean(librosa.feature.zero_crossing_rate(y = sample).T, axis=0)
    result=np.hstack((result, zcr)) 

    # Chroma_stft
    stft = np.abs(librosa.stft(sample))
    chroma_stft = np.mean(librosa.feature.chroma_stft(S = stft, sr = sample_rate).T, axis=0)
    result = np.hstack((result, chroma_stft))

    # MelSpectogram
    mel = np.mean(librosa.feature.melspectrogram(y = sample, sr = sample_rate).T, axis=0)
    result = np.hstack((result, mel)) 

    # MFCC
    mfcc = np.mean(librosa.feature.mfcc(y = sample, sr = sample_rate).T, axis=0)
    result = np.hstack((result, mfcc)) 

    # Root Mean Square Value
    rms = np.mean(librosa.feature.rms(y = sample).T, axis=0)
    result = np.hstack((result, rms)) 

    return result</code></pre>
<p>📝 Noise 추가, time stretching, pitching 방법들을 통해 음성 데이터 하나 당 (1, 162) 크기의 feature를 (3, 162) 로 증강합니다.</p>
<pre><code class="language-python">def get_features(path):

    sample, sample_rate = librosa.load(path)

    # without augmentation
    res1 = extract_features(sample)
    result = np.array(res1)

    # sample with noise
    noise_sample = noise(sample)
    res2 = extract_features(noise_sample)
    result = np.vstack((result, res2)) 

    # sample with stretching and pitching
    str_sample = stretch(sample)
    sample_stretch_pitch = pitch(str_sample, sample_rate)
    res3 = extract_features(sample_stretch_pitch)
    result = np.vstack((result, res3)) 

    return result</code></pre>
<pre><code class="language-python">labels = train[&#39;label&#39;]
x, y = [], []
for f, label in zip(file_name, labels):
    feature = get_features(train_path + f)
    for fe in feature:
        x.append(fe)
        y.append(label)</code></pre>
<pre><code class="language-python">X = np.array(x)
Y = np.array(y)

print(&quot;Shape of X:&quot;, np.shape(X))
print(&quot;Shape of Y:&quot;, np.shape(Y))</code></pre>
<pre>
Shape of X: (1200, 162)
Shape of Y: (1200,)
</pre>

<h4 id="reference"><strong>Reference</strong></h4>
<pre><code>[1] Speech Emotion Recognition by SHIVAM BURNWAL, https://www.kaggle.com/code/shivamburnwal/speech-emotion-recognition</code></pre><hr>
<p>읽어주셔서 감사합니다 :)<br>도움이 됐길 바랍니다 👍👍</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[Dacon] 중고차 가격 예측 경진대회]]></title>
            <link>https://velog.io/@standing-o/forecasting-used-car-price</link>
            <guid>https://velog.io/@standing-o/forecasting-used-car-price</guid>
            <pubDate>Tue, 23 Aug 2022 13:05:16 GMT</pubDate>
            <description><![CDATA[<h1 id="중고차-가격-예측"><strong>중고차 가격 예측</strong></h1>
<ul>
<li>본 포스팅은 feature engineering과 ensemble (catboost, random forest, gradient boosting) 등의 내용을 포함하고 있습니다.  </li>
<li>코드실행은 Google Colab의 CPU, Standard RAM 환경에서 진행했습니다.  </li>
<li>Keyword : 중고차가격예측, regression, catboost, randomforest, gradientboosting, ensemble, pycaret
➔ <a href="https://dacon.io/competitions/official/235901/codeshare/5089?page=1&amp;dtype=recent">데이콘에서 읽기</a></li>
<li>👉 <a href="https://standing-o.github.io/posts/forecasting-used-car-price/"><code>Click</code></a></li>
</ul>
<h2 id="0-import-packages"><strong>0. Import Packages</strong></h2>
<pre><code class="language-python">from google.colab import drive
drive.mount(&#39;/content/drive&#39;)</code></pre>
<pre>
Mounted at /content/drive
</pre>

<pre><code class="language-python">!pip install h5py
!pip install typing-extensions
!pip install wheel
!pip install folium==0.2.1
!pip install markupsafe==2.0.1
!pip install -U pandas-profiling
!pip install catboost
!pip install pycaret==2.3.10 markupsafe==2.0.1 pyyaml==5.4.1 -qq</code></pre>
<pre><code class="language-python">import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import sklearn
import pandas_profiling
import seaborn as sns
import random as rn
import os
import scipy.stats as stats
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder

from collections import Counter
from pycaret.regression import *

%matplotlib inline
warnings.filterwarnings(action=&#39;ignore&#39;)</code></pre>
<pre><code class="language-python">print(&quot;numpy version: {}&quot;. format(np.__version__))
print(&quot;pandas version: {}&quot;. format(pd.__version__))
print(&quot;matplotlib version: {}&quot;. format(matplotlib.__version__))
print(&quot;scikit-learn version: {}&quot;. format(sklearn.__version__))</code></pre>
<pre>
numpy version: 1.21.6
pandas version: 1.3.5
matplotlib version: 3.2.2
scikit-learn version: 0.23.2
</pre>

<pre><code class="language-python"># reproducibility
seed_num = 42 
np.random.seed(seed_num)
rn.seed(seed_num)
os.environ[&#39;PYTHONHASHSEED&#39;]=str(seed_num)</code></pre>
<h2 id="1-load-and-check-dataset"><strong>1. Load and Check Dataset</strong></h2>
<pre><code class="language-python">train = pd.read_csv(&#39;/content/drive/MyDrive/Forecasting_price/dataset/train.csv&#39;)
test = pd.read_csv(&#39;/content/drive/MyDrive/Forecasting_price/dataset/test.csv&#39;)

print(train.shape)
train.head()</code></pre>
<pre>
(1015, 11)
</pre>
<pre>
   id                          title  odometer location    isimported  \
0   0                   Toyota RAV 4     18277   Lagos   Foreign Used   
1   1            Toyota Land Cruiser        10    Lagos          New    
2   2  Land Rover Range Rover Evoque     83091    Lagos  Foreign Used   
3   3                   Lexus ES 350     91524    Lagos  Foreign Used   
4   4                   Toyota Venza     94177    Lagos  Foreign Used   

           engine transmission    fuel  paint  year    target  
0  4-cylinder(I4)    automatic  petrol    Red  2016  13665000  
1  4-cylinder(I4)    automatic  petrol  Black  2019  33015000  
2  6-cylinder(V6)    automatic  petrol    Red  2012   9915000  
3  4-cylinder(I4)    automatic  petrol   Gray  2007   3815000  
4  6-cylinder(V6)    automatic  petrol    Red  2010   7385000  
</pre>

<pre><code class="language-python">pr = train.profile_report()
pr.to_file(&#39;/content/drive/MyDrive/Forecasting_price/pr_report.html&#39;)
pr</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/397ffead-3b0d-4914-ae35-f4eca339195a/image.png" alt=""></p>
<h3 id="summary-of-pandas-profiling--alert"><strong>Summary of Pandas profiling : Alert</strong></h3>
<h4 id="high-correlation"><strong>High correlation</strong></h4>
<p><code>odometer</code>-<code>year</code>-<code>target</code>-<code>paint</code>-<code>fuel</code>-<code>transmission</code>-<code>engine</code></p>
<h4 id="high-cardinality"><strong>High cardinality</strong></h4>
<p><code>title</code>, <code>paint</code>  </p>
<p>↪ 중복도가 낮은 데이터</p>
<h4 id="high-skewness"><strong>High skewness</strong></h4>
<p>Skewness of <code>year</code> : -21.68</p>
<h4 id="odometer-has-21-zeros"><strong><code>odometer</code> has 21 zeros</strong></h4>
<p>↪ 주행거리가 0인 중고차가 21대 (2.1%)</p>
<h2 id="2-eda"><strong>2. EDA</strong></h2>
<p><code>id</code> : 샘플 아이디, <code>title</code> : 제조사 모델명, <code>odometer</code> : 주행 거리</p>
<p><code>location</code> : 판매처(나이지리아 도시), <code>isimported</code> : 현지 사용 여부</p>
<p><code>engine</code> : 엔진 종류, <code>transmission</code> : 트랜스미션 종류</p>
<p><code>fuel</code> : 연료 종류, <code>paint</code> : 페인트 색상, <code>year</code> : 제조년도, <code>target</code> : 자동차 가격</p>
<h3 id="data-type"><strong>Data type</strong></h3>
<ul>
<li><p>Numeric (4) : <code>id</code>, <code>odometer</code>, <code>year</code>, <code>target</code></p>
</li>
<li><p>Categorical (7) : <code>title</code>, <code>location</code>, <code>isimported</code>, <code>engine</code>, <code>transmission</code>, <code>fuel</code>, <code>paint</code></p>
</li>
</ul>
<pre><code class="language-python">train.isnull().sum()</code></pre>
<pre>
id              0
title           0
odometer        0
location        0
isimported      0
engine          0
transmission    0
fuel            0
paint           0
year            0
target          0
dtype: int64
</pre>

<pre><code class="language-python">test.isnull().sum()</code></pre>
<pre>
id              0
title           0
odometer        0
location        0
isimported      0
engine          0
transmission    0
fuel            0
paint           0
year            0
dtype: int64
</pre>
<ul>
<li>📝 결측치가 없습니다.</li>
</ul>
<pre><code class="language-python">df_train = train.copy()
df_test = test.copy()</code></pre>
<h3 id="2-1-outliers"><strong>2-(1). Outliers</strong></h3>
<pre><code class="language-python">fig, ax = plt.subplots(1, 2, figsize=(18,5))
g = sns.histplot(df_train[&#39;odometer&#39;], color=&#39;b&#39;, label=&#39;Skewness : {:.2f}&#39;.format(df_train[&#39;odometer&#39;].skew()), ax=ax[0])
g.legend(loc=&#39;best&#39;, prop={&#39;size&#39;: 16})
g.set_xlabel(&quot;Odometer&quot;, fontsize = 16)
g.set_ylabel(&quot;Count&quot;, fontsize = 16)

g = sns.histplot(df_train[&#39;year&#39;], color=&#39;b&#39;, label=&#39;Skewness : {:.2f}&#39;.format(df_train[&#39;year&#39;].skew()), ax=ax[1])
g.legend(loc=&#39;best&#39;, prop={&#39;size&#39;: 16})
g.set_xlabel(&quot;Year&quot;, fontsize = 16)
g.set_ylabel(&quot;Count&quot;, fontsize = 16)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/ffcd54a6-0676-4876-8ab3-8720790f7a9c/image.png" alt=""></p>
<pre><code class="language-python">numeric_fts = [&#39;odometer&#39;, &#39;year&#39;]
outlier_ind = []
for i in numeric_fts:
  Q1 = np.percentile(df_train[i],25)
  Q3 = np.percentile(df_train[i],75)
  IQR = Q3-Q1
  outlier_list = df_train[(df_train[i] &lt; Q1 - IQR * 1.5) | (df_train[i] &gt; Q3 + IQR * 1.5)].index
  outlier_ind.extend(outlier_list)</code></pre>
<pre><code class="language-python"># Drop outliers
train_df = df_train.drop(outlier_ind, axis = 0).reset_index(drop = True)
train_df</code></pre>
<pre>
       id                          title  odometer location    isimported  \
0       0                   Toyota RAV 4     18277   Lagos   Foreign Used   
1       1            Toyota Land Cruiser        10    Lagos          New    
2       2  Land Rover Range Rover Evoque     83091    Lagos  Foreign Used   
3       3                   Lexus ES 350     91524    Lagos  Foreign Used   
4       4                   Toyota Venza     94177    Lagos  Foreign Used   
..    ...                            ...       ...      ...           ...   
970  1010                 Toyota Corolla     46768    Lagos  Foreign Used   
971  1011                   Toyota Camry     31600    Abuja  Foreign Used   
972  1012                   Toyota Camry     96802    Abuja  Foreign Used   
973  1013                   Lexus GX 460    146275    Lagos  Foreign Used   
974  1014                         DAF CF         0    Lagos  Locally used   

             engine transmission    fuel   paint  year    target  
0    4-cylinder(I4)    automatic  petrol     Red  2016  13665000  
1    4-cylinder(I4)    automatic  petrol   Black  2019  33015000  
2    6-cylinder(V6)    automatic  petrol     Red  2012   9915000  
3    4-cylinder(I4)    automatic  petrol    Gray  2007   3815000  
4    6-cylinder(V6)    automatic  petrol     Red  2010   7385000  
..              ...          ...     ...     ...   ...       ...  
970  4-cylinder(I4)    automatic  petrol   Black  2014   5415000  
971  4-cylinder(I4)    automatic  petrol  Silver  2011   3615000  
972  4-cylinder(I4)    automatic  petrol   Black  2011   3415000  
973  6-cylinder(V6)    automatic  petrol    Gold  2013  14315000  
974  6-cylinder(V6)       manual  diesel   white  1998  10015000  

[975 rows x 11 columns]
</pre>

<pre><code class="language-python">fig, ax = plt.subplots(1, 2, figsize=(18,5))
g = sns.histplot(train_df[&#39;odometer&#39;], color=&#39;b&#39;, label=&#39;Skewness : {:.2f}&#39;.format(train_df[&#39;odometer&#39;].skew()), ax=ax[0])
g.legend(loc=&#39;best&#39;, prop={&#39;size&#39;: 16})
g.set_xlabel(&quot;Odometer&quot;, fontsize = 16)
g.set_ylabel(&quot;Count&quot;, fontsize = 16)

g = sns.histplot(train_df[&#39;year&#39;], color=&#39;b&#39;, label=&#39;Skewness : {:.2f}&#39;.format(train_df[&#39;year&#39;].skew()), ax=ax[1])
g.legend(loc=&#39;best&#39;, prop={&#39;size&#39;: 16})
g.set_xlabel(&quot;Year&quot;, fontsize = 16)
g.set_ylabel(&quot;Count&quot;, fontsize = 16)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/2d6627c6-4e54-4212-8475-9387116f2e04/image.png" alt=""></p>
<p>📝 outlier 들을 제거하여 첨도가 감소했습니다.</p>
<pre><code class="language-python">print(&quot;# outliers to drop :&quot;, len(outlier_ind))</code></pre>
<pre>
# outliers to drop : 44
</pre>

<h3 id="2-2-correlation">2-(2). Correlation</h3>
<p>📝 앞서 수행한 pandas profiling report의 alert를 참고하여 상관계수를 계산했습니다.  </p>
<p>📝 Categorical 데이터를 라벨인코더를 통해 수치형으로 변환한 후 상관관계를 확인합니다.</p>
<pre><code class="language-python">cat_fts = [&#39;title&#39;, &#39;location&#39;, &#39;isimported&#39;, &#39;engine&#39;, &#39;transmission&#39;, &#39;fuel&#39;, &#39;paint&#39;]</code></pre>
<pre><code class="language-python">la_train = train_df.copy()

for i in range(len(cat_fts)):
  encoder = LabelEncoder()
  la_train[cat_fts[i]] = encoder.fit_transform(la_train[cat_fts[i]])</code></pre>
<pre><code class="language-python">plt.figure(figsize = (10,8))
sns.heatmap(la_train[[&#39;odometer&#39;, &#39;year&#39;, &#39;paint&#39;, &#39;fuel&#39;, &#39;transmission&#39;, &#39;engine&#39;, &#39;target&#39;]].corr(), annot=True)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/dccfe408-6eea-4ada-af29-40e1e2a130ca/image.png" alt=""></p>
<h2 id="3-feature-engineering"><strong>3. Feature Engineering</strong></h2>
<h3 id="3-1-company-컬럼-생성"><strong>3-(1). <code>company</code> 컬럼 생성</strong></h3>
<p>📝 <code>title</code> 변수 값들의 앞부분에는 공통적으로 자동차 회사의 이름이 오는것을 확인할 수 있습니다.  </p>
<p>📝 split 함수를 사용하여 첫번째 띄어쓰기를 기준으로 회사명 데이터를 추출하고 새 컬럼을 생성해주겠습니다.<br>📝 <code>company</code> 컬럼의 계급을 훈련 데이터의 <code>target</code>값 기준으로 나눠주겠습니다.  </p>
<pre><code class="language-python">print(train_df[&#39;title&#39;].unique()[:20])</code></pre>
<pre>
['Toyota RAV 4' 'Toyota Land Cruiser' 'Land Rover Range Rover Evoque'
 'Lexus ES 350' 'Toyota Venza' 'Toyota Corolla'
 'Land Rover Range Rover Sport' 'Pontiac Vibe' 'Toyota Tacoma'
 'Lexus RX 350' 'Ford Escape' 'Honda Civic' 'Volvo XC90' 'BMW 750'
 'Infiniti JX' 'Honda Accord' 'Mercedes-Benz ML 350' 'Toyota Camry'
 'Hyundai Azera' 'Lexus GX 460']
</pre>

<pre><code class="language-python">train_df[&#39;company&#39;] = train_df[&#39;title&#39;].apply(lambda x : x.split(&quot; &quot;)[0])
df_test[&#39;company&#39;] = df_test[&#39;title&#39;].apply(lambda x : x.split(&quot; &quot;)[0])

print(train_df[&#39;company&#39;].unique())
print(&quot;#fts :&quot;, len(train_df[&#39;company&#39;].unique()), &#39;\n&#39;)
print(df_test[&#39;company&#39;].unique())
print(&quot;#fts :&quot;, len(df_test[&#39;company&#39;].unique()), &#39;\n&#39;)</code></pre>
<pre>
['Toyota' 'Land' 'Lexus' 'Pontiac' 'Ford' 'Honda' 'Volvo' 'BMW' 'Infiniti'
 'Mercedes-Benz' 'Hyundai' 'Jaguar' 'Mitsubishi' 'Nissan' 'Chevrolet'
 'Mazda' 'Lincoln' 'Kia' 'Acura' 'DAF' 'Man' 'Isuzu' 'IVM' 'Porsche'
 'MINI' 'GMC' 'Iveco' 'Scania' 'Volkswagen' 'GAC' 'IVECO' 'Mack' 'Peugeot'
 'Rolls-Royce' 'MAN-VOLKSWAGEN' 'Jeep' 'ALPINA' 'Bentley' 'JMC']
#fts : 39 

['Mercedes-Benz' 'Honda' 'Toyota' 'Iveco' 'Lexus' 'Nissan' 'Volkswagen'
 'Jeep' 'Ford' 'BMW' 'Mack' 'Land' 'Hyundai' 'Peugeot' 'Volvo' 'Infiniti'
 'Acura' 'Man' 'Fiat' 'MINI' 'DAF' 'Mazda' 'Porsche' 'Mitsubishi'
 'Chevrolet' 'Kia' 'Pontiac' 'Rolls-Royce']
#fts : 28 

</pre>

<pre><code class="language-python">plt.figure(figsize = (20,8))
g = sns.barplot(x = &#39;company&#39;, y = &#39;target&#39;, data = train_df)

for p in g.patches:
    left, bottom, width, height = p.get_bbox().bounds
    g.annotate(&quot;%.1f&quot;%(height/1e6), (left+width/2, height*1.01), ha=&#39;center&#39;)

g.set_xlabel(&quot;company&quot;, fontsize = 16)
g.set_ylabel(&quot;target&quot;, fontsize = 16)

plt.xticks(rotation=90)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/a6f10919-63ae-46a3-b0bb-5d283a5fb9f4/image.png" alt=""></p>
<pre><code class="language-python">company_h = np.zeros((len(g.patches)))
i = 0
for p in g.patches:
    left, bottom, width, height = p.get_bbox().bounds
    company_h[i] = (height/1e6)
    i +=1</code></pre>
<pre><code class="language-python">company_h</code></pre>
<pre>
array([  6.37849032,  29.39868421,  14.08227273,   2.715     ,
         6.31845588,   4.39417308,   4.15571429,  15.279     ,
        16.16      ,  13.37352941,   3.89282609,   2.665     ,
         3.42      ,   1.98666667,   7.233     ,   2.07875   ,
         4.415     ,   2.81785714,   4.082     ,   8.515     ,
        10.265     ,   4.015     ,   2.89      ,  14.265     ,
         5.54      ,   5.515     ,  10.015     ,   7.93      ,
         2.09409091,   1.49      ,   6.015     ,   8.015     ,
         2.125     , 150.015008  ,   6.34      ,   2.515     ,
         9.065     ,  28.015     ,   9.365     ])
</pre>

<pre><code class="language-python">companys = train_df[&#39;company&#39;].unique()</code></pre>
<pre><code class="language-python">def company_fix(train_df, df, companys):
  only_test_com = list(set(df[&#39;company&#39;])-set(train_df[&#39;company&#39;]))


  if len(only_test_com) != 0:
    for k in range(len(only_test_com)):
      print(only_test_com)
      df.loc[(df[&#39;company&#39;] == only_test_com[k]), &#39;company&#39;] = 1


  for c in range(7):
    if c==6:
      company_ind = companys[np.where(company_h&gt;=c*5)]
    elif c==0:
      company_ind = companys[np.where(company_h&lt;(c+1)*5)]
    else:  
      company_ind = companys[np.where((company_h&gt;=c*5)&amp;(company_h&lt;(c+1)*5))]

    for i in range(len(company_ind)):
      df.loc[(df[&#39;company&#39;] == company_ind[i]), &#39;company&#39;] = c+1

</code></pre>
<pre><code class="language-python">copy_train = train_df.copy()</code></pre>
<pre><code class="language-python">company_fix(copy_train, train_df, companys)</code></pre>
<pre><code class="language-python">company_fix(copy_train, df_test, companys)</code></pre>
<pre>
['Fiat']
</pre>

<pre><code class="language-python">train_df[&#39;company&#39;].unique()</code></pre>
<pre>
array([2, 6, 3, 1, 4, 7], dtype=object)
</pre>

<pre><code class="language-python">df_test[&#39;company&#39;].unique()</code></pre>
<pre>
array([3, 1, 2, 4, 6, 7], dtype=object)
</pre>
<h3 id="3-2-paint"><strong>3-(2). <code>paint</code></strong></h3>
<p>📝 뒤죽박죽인 <code>paint</code> 변수를 고쳐주겠습니다.</p>
<pre><code class="language-python">print(sorted(train.paint.unique()))</code></pre>
<pre>
[' Black', ' Black/Red', 'Ash', 'Ash and black', 'BLACK', 'Beige', 'Black', 'Black ', 'Black and silver', 'Black sand pearl', 'Black.', 'Blue', 'Blue ', 'Brown', 'Cream', 'Cream ', 'DARK GREY', 'Dark Ash', 'Dark Blue', 'Dark Green', 'Dark Grey', 'Dark ash', 'Dark blue ', 'Dark gray', 'Dark silver ', 'Deep Blue', 'Deep blue', 'GOLD', 'Gery', 'Gold', 'Gold ', 'Gray', 'Gray ', 'Green', 'Green ', 'Grey', 'Grey ', 'Ink blue', 'Light Gold', 'Light blue', 'Light silver ', 'Magnetic Gray', 'Magnetic Gray Metallic', 'Maroon', 'Midnight Black Metal', 'Milk', 'Navy blue', 'Off white', 'Off white l', 'Pale brown', 'Purple', 'Red', 'Redl', 'SILVER', 'Silver', 'Silver ', 'Silver/grey', 'Sky blue', 'Skye blue', 'Sliver', 'Super White', 'WHITE', 'WINE', 'Whine ', 'White', 'White ', 'White orchild pearl', 'Wine', 'Yellow', 'blue', 'green', 'orange', 'red', 'white', 'white-blue', 'yellow']
</pre>

<pre><code class="language-python">def color_handling(x):
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.strip()   # eliminate empty space
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.lower()    # convert to lower case
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;.&quot;, &quot;&quot;)</code></pre>
<pre><code class="language-python">color_handling(train_df)
color_handling(df_test)</code></pre>
<pre><code class="language-python">train_df[&#39;paint&#39;].unique()</code></pre>
<pre>
array(['red', 'black', 'gray', 'white', 'blue', 'redl', 'silver',
       'black/red', 'deep blue', 'dark grey', 'brown', 'grey', 'green',
       'purple', 'gold', 'dark blue', 'milk', 'midnight black metal',
       'beige', 'dark ash', 'cream', 'dark gray', 'white orchild pearl',
       'dark green', 'yellow', 'sliver', 'wine', 'white-blue',
       'magnetic gray', 'dark silver', 'silver/grey', 'ink blue',
       'light blue', 'sky blue', 'gery', 'pale brown', 'whine',
       'black and silver', 'light silver', 'black sand pearl',
       'off white', 'ash', 'maroon', 'navy blue', 'super white',
       'ash and black', 'magnetic gray metallic', 'skye blue',
       'off white l'], dtype=object)
</pre>
<ul>
<li><p><code>skye blue</code> -&gt; <code>sky blue</code></p>
</li>
<li><p><code>dark ash</code>, <code>dark grey</code>, <code>dark silver</code>, <code>ash and black</code>, <code>black and silver</code> -&gt; <code>dark gray</code></p>
</li>
<li><p><code>gery</code>, <code>grey</code>,<code>ash</code>, <code>magnetic gray metallic</code>, <code>magnetic gray</code>, <code>gray metallic</code>, <code>silver/grey</code>, <code>sliver</code>, <code>silver</code> -&gt; <code>gray</code></p>
</li>
<li><p><code>off white l</code>, <code>off white</code>, <code>super white</code>, <code>white orchild pearl</code> -&gt; <code>white</code></p>
</li>
<li><p><code>redl</code>, <code>maroon</code> -&gt; <code>red</code></p>
</li>
<li><p><code>whine</code> -&gt; <code>wine</code></p>
</li>
<li><p><code>ink blue</code>, <code>deep blue</code>, <code>navy blue</code> -&gt; <code>dark blue</code></p>
</li>
<li><p><code>sky blue</code>, <code>white-blue</code> -&gt; <code>light blue</code></p>
</li>
<li><p><code>black sand pearl</code>, <code>midnight black metal</code> -&gt; <code>black</code></p>
</li>
<li><p><code>pale brown</code> -&gt; <code>brown</code></p>
</li>
<li><p><code>milk</code> -&gt; <code>cream</code></p>
</li>
</ul>
<pre><code class="language-python">def color_fix(x):
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;skye blue&quot;, &quot;sky blue&quot;)

  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;dark ash&quot;, &quot;dark gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;dark grey&quot;, &quot;dark gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;dark silver&quot;, &quot;dark gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;ash and black&quot;, &quot;dark gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;black and silver&quot;, &quot;dark gray&quot;)

  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;gery&quot;, &quot;gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;grey&quot;, &quot;gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;ash&quot;, &quot;gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;silver/grey&quot;, &quot;gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;silver/gray&quot;, &quot;gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;sliver&quot;, &quot;gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;silver&quot;, &quot;gray&quot;)

  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;magnetic gray&quot;, &quot;gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;gray metallic&quot;, &quot;gray&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;magnetic gray metallic&quot;, &quot;gray&quot;)

  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;black sand pearl&quot;, &quot;black&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;midnight black metal&quot;, &quot;black&quot;)


  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;off white l&quot;, &quot;white&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;off white&quot;, &quot;white&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;super white&quot;, &quot;white&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;white orchild pearl&quot;, &quot;white&quot;)

  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;redl&quot;, &quot;red&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;maroon&quot;, &quot;red&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;whine&quot;, &quot;wine&quot;)

  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;ink blue&quot;, &quot;dark blue&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;deep blue&quot;, &quot;dark blue&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;navy blue&quot;, &quot;dark blue&quot;)

  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;sky blue&quot;, &quot;light blue&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;white-blue&quot;, &quot;light blue&quot;)
  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;pale brown&quot;, &quot;brown&quot;)

  x[&#39;paint&#39;] = x[&#39;paint&#39;].str.replace(&quot;milk&quot;, &quot;cream&quot;)</code></pre>
<pre><code class="language-python">color_fix(train_df)
color_fix(df_test)</code></pre>
<pre><code class="language-python">print(sorted(train_df[&#39;paint&#39;].unique()))
print(len(train_df[&#39;paint&#39;].unique()))</code></pre>
<pre>
['beige', 'black', 'black/red', 'blue', 'brown', 'cream', 'dark blue', 'dark gray', 'dark green', 'gold', 'gray', 'green', 'light blue', 'light gray', 'purple', 'red', 'white', 'wine', 'yellow']
19
</pre>

<pre><code class="language-python">print(sorted(df_test[&#39;paint&#39;].unique()))
print(len(df_test[&#39;paint&#39;].unique()))</code></pre>
<pre>
['beige', 'blac', 'black', 'blue', 'brown', 'classic gray met(1f7)', 'cream', 'dark blue', 'dark gray', 'dark green', 'gold', 'golf', 'gray', 'gray and black', 'green', 'indigo ink pearl', 'light gray', 'mint green', 'red', 'white', 'white and green', 'wine', 'yellow']
23
</pre>
<h3 id="3-3-location"><strong>3-(3). <code>location</code></strong></h3>
<p>📝 <code>location</code> 변수도 고쳐주겠습니다.</p>
<pre><code class="language-python">train_df[&#39;location&#39;].unique()</code></pre>
<pre>
array(['Lagos ', 'Lagos', 'Abuja', 'Lagos State', 'Ogun', 'FCT', 'Accra',
       'other', 'Abuja ', 'Abia State', 'Adamawa ', 'Abia', 'Ogun State'],
      dtype=object)
</pre>

<pre><code class="language-python">def location_fix(x):
  x[&#39;location&#39;] = x[&#39;location&#39;].str.replace(&quot;Lagos &quot;, &quot;Lagos&quot;)
  x[&#39;location&#39;] = x[&#39;location&#39;].str.replace(&quot;Lagos State&quot;, &quot;Lagos&quot;)
  x[&#39;location&#39;] = x[&#39;location&#39;].str.replace(&quot;Ogun State&quot;, &quot;Ogun&quot;)
  x[&#39;location&#39;] = x[&#39;location&#39;].str.replace(&quot;Abuja &quot;, &quot;Abuja&quot;)
  x[&#39;location&#39;] = x[&#39;location&#39;].str.replace(&quot;Abia State&quot;, &quot;Abia&quot;)
  x[&#39;location&#39;] = x[&#39;location&#39;].str.replace(&quot;LagosState&quot;, &quot;Lagos&quot;)</code></pre>
<pre><code class="language-python">location_fix(train_df)
location_fix(df_test)</code></pre>
<pre><code class="language-python">print(sorted(train_df[&#39;location&#39;].unique()))
print(len(train_df[&#39;location&#39;].unique()))</code></pre>
<pre>
['Abia', 'Abuja', 'Accra', 'Adamawa ', 'FCT', 'Lagos', 'Ogun', 'other']
8
</pre>

<pre><code class="language-python">print(sorted(df_test[&#39;location&#39;].unique()))
print(len(df_test[&#39;location&#39;].unique()))</code></pre>
<pre>
['Abia', 'Abuja', 'Arepo ogun state ', 'Lagos', 'Mushin', 'Ogun', 'other']
7
</pre>
<h3 id="3-4-engine"><strong>3-(4). <code>engine</code></strong></h3>
<p>📝  <code>engine</code> 변수를 수치형으로 바꿔주겠습니다.</p>
<pre><code class="language-python">plt.figure(figsize = (10,8))
sns.barplot(x = &#39;engine&#39;, y = &#39;target&#39;, data = train_df)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/81b959a8-7c6c-4fcf-8a68-f0378ded9f1a/image.png" alt=""></p>
<pre><code class="language-python">engines = train_df[&#39;engine&#39;].unique()
engines</code></pre>
<pre>
array(['4-cylinder(I4)', '6-cylinder(V6)', '8-cylinder(V8)',
       '6-cylinder(I6)', '4-cylinder(H4)', '5-cylinder(I5)',
       '3-cylinder(I3)', '2-cylinder(I2)'], dtype=object)
</pre>

<pre><code class="language-python">train_df[&#39;engine&#39;]</code></pre>
<pre>
0      4-cylinder(I4)
1      4-cylinder(I4)
2      6-cylinder(V6)
3      4-cylinder(I4)
4      6-cylinder(V6)
            ...      
970    4-cylinder(I4)
971    4-cylinder(I4)
972    4-cylinder(I4)
973    6-cylinder(V6)
974    6-cylinder(V6)
Name: engine, Length: 975, dtype: object
</pre>

<pre><code class="language-python">def engine_fix(df):
  df.loc[((df[&#39;engine&#39;] != &quot;8-cylinder(V8)&quot;) &amp; (df[&#39;engine&#39;] != &quot;4-cylinder(H4)&quot;) &amp; (df[&#39;engine&#39;] != &quot;6-cylinder(I6)&quot;) &amp; 
          (df[&#39;engine&#39;] != &quot;6-cylinder(V6)&quot;) &amp; (df[&#39;engine&#39;] != &quot;4-cylinder(I4)&quot;) &amp; (df[&#39;engine&#39;] != &quot;5-cylinder(I5)&quot;) &amp; (df[&#39;engine&#39;] != &quot;3-cylinder(I3)&quot;) &amp; (df[&#39;engine&#39;] != &quot;2-cylinder(I2)&quot;)), &#39;engine&#39;] = 2

  df.loc[(df[&#39;engine&#39;] == &quot;2-cylinder(I2)&quot;), &#39;engine&#39;] = 1
  df.loc[(df[&#39;engine&#39;] == &quot;3-cylinder(I3)&quot;), &#39;engine&#39;] = 1
  df.loc[(df[&#39;engine&#39;] == &quot;5-cylinder(I5)&quot;), &#39;engine&#39;] = 1
  df.loc[(df[&#39;engine&#39;] == &quot;4-cylinder(I4)&quot;), &#39;engine&#39;] = 2
  df.loc[(df[&#39;engine&#39;] == &quot;6-cylinder(V6)&quot;), &#39;engine&#39;] = 2
  df.loc[(df[&#39;engine&#39;] == &quot;6-cylinder(I6)&quot;), &#39;engine&#39;] = 2
  df.loc[(df[&#39;engine&#39;] == &quot;4-cylinder(H4)&quot;), &#39;engine&#39;] = 3
  df.loc[(df[&#39;engine&#39;] == &quot;8-cylinder(V8)&quot;), &#39;engine&#39;] = 4</code></pre>
<pre><code class="language-python">engine_fix(train_df)
engine_fix(df_test)</code></pre>
<pre><code class="language-python">print(sorted(train_df[&#39;engine&#39;].unique()))
print(len(train_df[&#39;engine&#39;].unique()))</code></pre>
<pre>
[1, 2, 3, 4]
4
</pre>

<pre><code class="language-python">print(sorted(df_test[&#39;engine&#39;].unique()))
print(len(df_test[&#39;engine&#39;].unique()))</code></pre>
<pre>
[1, 2, 4]
3
</pre>
<h3 id="3-5-dropping"><strong>3-(5). dropping</strong></h3>
<p>📝 train과 test 데이터의 <code>title</code>, <code>location</code>, <code>paint</code> 변수의 값 종류 및 길이가 일치하지 않습니다.</p>
<pre><code class="language-python">cat_fts2 = [&#39;title&#39;, &#39;location&#39;, &#39;isimported&#39;, &#39;transmission&#39;, &#39;fuel&#39;, &#39;paint&#39;]</code></pre>
<pre><code class="language-python">for i in range(len(cat_fts2)):
  print(cat_fts2[i], &quot;:&quot;)
  print(train_df[cat_fts2[i]].unique())
  print(&quot;#fts :&quot;, len(train_df[cat_fts2[i]].unique()), &#39;\n&#39;)</code></pre>
<pre>
title :
['Toyota RAV 4' 'Toyota Land Cruiser' 'Land Rover Range Rover Evoque'
 'Lexus ES 350' 'Toyota Venza' 'Toyota Corolla'
 'Land Rover Range Rover Sport' 'Pontiac Vibe' 'Toyota Tacoma'
 'Lexus RX 350' 'Ford Escape' 'Honda Civic' 'Volvo XC90' 'BMW 750'
 'Infiniti JX' 'Honda Accord' 'Mercedes-Benz ML 350' 'Toyota Camry'
 'Hyundai Azera' 'Lexus GX 460' 'BMW 325' 'Toyota Sienna' 'Honda Fit'
 'Honda CR-V' 'Hyundai Tucson' 'Jaguar XJ8' 'BMW X6' 'Mercedes-Benz C 300'
 'Mitsubishi Galant' 'Mercedes-Benz GL 450' 'Lexus RX 300'
 'Toyota Highlander' 'Mitsubishi CANTER PICK UP' 'Nissan Titan'
 'Lexus IS 250' 'Mercedes-Benz 200' 'Toyota Sequoia' 'Ford Explorer'
 'Hyundai ix35' 'Lexus CT 200h' 'Lexus LX 570' 'Toyota Avensis'
 'Toyota 4-Runner' 'Mercedes-Benz GLE 350' 'Mercedes-Benz E 300'
 'Toyota Avalon' 'Chevrolet Camaro' 'Land Rover Range Rover' 'Mazda CX-9'
 'Lexus RX 330' 'Lincoln Mark' 'Kia Optima' 'Lexus GS 300' 'Jaguar X-Type'
 'Nissan Altima' 'Acura MDX' 'DAF 95XF TRACTOR HEAD' 'Man TGA 18.360'
 'Nissan Pathfinder' 'Mercedes-Benz E 350' 'Honda Crosstour' 'Honda Pilot'
 'Lexus LS 460' 'Nissan Cabstar' 'Kia Sorento' 'Mercedes-Benz CLA 250'
 'Mitsubishi Pajero' 'Mercedes-Benz C 350' 'Lexus GS 350'
 'Mercedes-Benz E 320' 'Toyota Yaris' 'Toyota Matrix' 'Isuzu NQR'
 'IVM LT35' 'Hyundai Elantra' 'Porsche Cayenne' 'Toyota Prado'
 'Hyundai Sonata' 'MINI Cooper' 'Toyota Hiace' 'Mercedes-Benz 350'
 'Honda Odyssey' 'Mercedes-Benz E 550' 'GMC Terrain'
 'Mercedes-Benz GLK 350' 'Mercedes-Benz C 250' 'Mercedes-Benz ML 430'
 'Mercedes-Benz GLC 300' 'Kia Cerato' 'Chevrolet Evanda' 'Iveco TRUCK'
 'Acura ZDX' 'Mercedes-Benz 450' 'Mercedes-Benz GLA 250'
 'Mercedes-Benz CLS 500' 'Scania P94 FLATBED' 'Nissan Versa' 'Ford F 150'
 'Mercedes-Benz GLE 43 AMG' 'Volkswagen Golf' 'Mercedes-Benz 320'
 'Honda Ridgeline' 'Mercedes-Benz S 450' 'Mercedes-Benz 300' 'Kia Rio'
 'BMW 740' 'Ford Edge' 'Toyota Dyna' 'Volvo FL6' 'Toyota Coaster'
 'GAC Gonow Other' 'IVECO EUROTECH 7.50E-16' 'Mack CH613'
 'Scania TRACTOR HEAD' 'Nissan Xterra' 'Mercedes-Benz ML 320' 'Ford Focus'
 'Mercedes-Benz 220' 'Man Truck 18.44' 'BMW 730' 'Peugeot 607' 'BMW 528'
 'Volvo XC60' 'Mercedes-Benz E 200' 'Volkswagen Passat'
 'Volkswagen Sharan' 'Lexus GX 470' 'Ford Transit' 'Nissan Quest'
 'Nissan Maxima' 'Hyundai Santa Fe' 'Lexus ES 300' 'Mazda Tribute'
 'Ford Fusion' 'Acura RDX' 'Peugeot 206' 'Mercedes-Benz G 63 AMG'
 'Toyota Hilux' 'Kia Stinger' 'Volkswagen Tiguan' 'Acura TL'
 'Porsche Panamera' 'Rolls-Royce Ghost' 'BMW 745' 'BMW 335'
 'Volkswagen Jetta' 'Toyota Solara' 'Mercedes-Benz C 450 AMG'
 'Nissan Murano' 'Chevrolet Traverse' 'Volkswagen T4 Caravelle'
 'MAN-VOLKSWAGEN FLATBED' 'Nissan Frontier' 'Mercedes-Benz C 180'
 'Infiniti M35' 'Nissan Sentra' 'Jeep Cherokee' 'Toyota DYNA 200'
 'Nissan Rogue' 'Land Rover Range Rover Velar' 'ALPINA B3' 'Mazda 323'
 'Volkswagen T6 other' 'Bentley Arnage' 'Mazda 6' 'Infiniti FX'
 'Ford Expedition' 'Kia Picanto' 'Toyota Tundra' 'JMC Vigus'
 'Infiniti QX80' 'Volvo FH12' 'Volkswagen Touareg' 'Porsche Macan'
 'Peugeot 308' 'Nissan INFINITI M90.150/2' 'MINI Cooper Countryman'
 'Lexus ES 330' 'Honda Insight' 'Toyota Vitz' 'Isuzu CABSTER'
 'Mercedes-Benz C 63 AMG' 'Mercedes-Benz SL 400' 'Volkswagen 17.22'
 'DAF CF']
#fts : 185 

location :
['Lagos' 'Abuja' 'Ogun' 'FCT' 'Accra' 'other' 'Abia' 'Adamawa ']
#fts : 8 

isimported :
['Foreign Used' 'New ' 'Locally used']
#fts : 3 

transmission :
['automatic' 'manual']
#fts : 2 

fuel :
['petrol' 'diesel']
#fts : 2 

paint :
['red' 'black' 'gray' 'white' 'blue' 'black/red' 'dark blue' 'dark gray'
 'brown' 'green' 'purple' 'gold' 'cream' 'beige' 'dark green' 'yellow'
 'wine' 'light blue' 'light gray']
#fts : 19 

</pre>

<pre><code class="language-python">for i in range(len(cat_fts2)):
  print(cat_fts2[i], &quot;:&quot;)
  print(df_test[cat_fts2[i]].unique())
  print(&quot;#fts :&quot;, len(df_test[cat_fts2[i]].unique()), &#39;\n&#39;)</code></pre>
<pre>
title :
['Mercedes-Benz C 300' 'Honda Accord' 'Mercedes-Benz S 550'
 'Toyota Sienna' 'Toyota Hiace' 'Toyota Corolla' 'Iveco EUROCARGO 120e18'
 'Mercedes-Benz GLE 350' 'Toyota Highlander' 'Toyota Hilux' 'Toyota Camry'
 'Mercedes-Benz C 180' 'Lexus ES 350' 'Honda Fit' 'Toyota Matrix'
 'Toyota Venza' 'Lexus IS 250' 'Nissan Primera' 'Volkswagen Sharan'
 'Jeep Wrangler' 'Volkswagen Golf' 'Mercedes-Benz 814' 'Nissan Sentra'
 'Volkswagen Passat' 'Mercedes-Benz GLK 350' 'Lexus RX 350' 'Ford Mondeo'
 'BMW X3' 'Mack CXN613 CAB BEHIND ENGINE' 'Toyota RAV 4'
 'Land Rover Discovery' 'Toyota Avalon' 'Lexus GX 460' 'Hyundai Santa Fe'
 'Peugeot 206' 'Volvo FL7' 'Mercedes-Benz C 320' 'Hyundai Sonata'
 'Infiniti FX' 'Honda Civic' 'Mercedes-Benz CLS 500'
 'Mercedes-Benz GLK 300' 'Acura RDX' 'Mercedes-Benz G 550' 'BMW 535'
 'Acura TL' 'Nissan Xterra' 'Land Rover Range Rover' 'Nissan A'
 'Toyota 4-Runner' 'Honda Pilot' 'Man LE 8. 180 PLATFORM TRUCK'
 'Toyota Yaris' 'Hyundai Elantra' 'Volvo S80' 'Mercedes-Benz GLA 180'
 'Acura TSX' 'Lexus LX 570' 'Mercedes-Benz Maybach' 'Mercedes-Benz 300'
 'Acura MDX' 'Nissan INFINITI M90.150/2' 'Land Rover Range Rover Sport'
 'Nissan Altima' 'Peugeot 307' 'Fiat Ducato' 'Mercedes-Benz C 350'
 'Lexus RX 330' 'Ford Edge' 'Honda CR-V' 'Volvo FL12' 'Ford Explorer'
 'Man 26-403' 'MINI Cooper Coupé' 'Iveco TRUCK' 'Nissan Cabstar'
 'MINI Cooper' 'Lexus RX 400' 'Ford TRANSIT PICKUP' 'Toyota Prius'
 'Toyota Tundra' 'Honda Element' 'Toyota Tacoma' 'Lexus ES 300'
 'DAF XF TRACTOR HEAD' 'Honda Odyssey' 'Nissan Pathfinder' 'Mazda 323'
 'Mercedes-Benz E 300' 'Lexus GS 350' 'Mercedes-Benz ML 350'
 'Mercedes-Benz E 350' 'Porsche Cayenne' 'BMW 525' 'Toyota Land Cruiser'
 'Mack R-686ST' 'Toyota C-HR' 'Mitsubishi Eclipse' 'Chevrolet Camaro'
 'Mercedes-Benz CABIN PLUS CHASSIS ONLY' 'Mercedes-Benz GLE 450'
 'Toyota Avensis' 'Ford Mustang' 'Volvo FL6' 'Kia Optima'
 'Mitsubishi Pajero' 'Honda Crosstour' 'Lexus RX 300' 'Honda Ridgeline'
 'Mercedes-Benz 220' 'Mitsubishi Montero' 'Pontiac Vibe' 'Ford F 150'
 'Rolls-Royce Ghost' 'Ford Fusion' 'Lexus GS 300' 'Ford Transit'
 'Hyundai Azera' 'Mitsubishi L200' 'Mercedes-Benz DUMP TRUCK'
 'Mercedes-Benz WATER TANKER' 'Kia Rio' 'Man BOCKMANN' 'Lexus GX 470']
#fts : 124 

location :
['Abuja' 'Lagos' 'Ogun' 'Mushin' 'other' 'Arepo ogun state ' 'Abia']
#fts : 7 

isimported :
['New ' 'Foreign Used' 'Locally used']
#fts : 3 

transmission :
['automatic' 'manual']
#fts : 2 

fuel :
['petrol' 'diesel']
#fts : 2 

paint :
['white' 'black' 'dark gray' 'red' 'gray' 'blue' 'gold' 'green' 'cream'
 'brown' 'yellow' 'dark green' 'white and green' 'light gray' 'wine'
 'blac' 'dark blue' 'golf' 'indigo ink pearl' 'gray and black'
 'classic gray met(1f7)' 'beige' 'mint green']
#fts : 23 

</pre>

<p>📝 One-hot encoding을 진행해줍니다</p>
<pre><code class="language-python">train_data = train_df.copy()
test_data = df_test.copy()</code></pre>
<pre><code class="language-python">for i in range(len(cat_fts2)):
  onehot_encoder = OneHotEncoder(handle_unknown=&quot;ignore&quot;, sparse = False)

  transformed = onehot_encoder.fit_transform(train_data[cat_fts2[i]].to_numpy().reshape(-1, 1))
  onehot_df = pd.DataFrame(transformed, columns=onehot_encoder.get_feature_names())
  train_data = pd.concat([train_data, onehot_df], axis=1).drop(cat_fts2[i], axis=1)

  test_transformed = onehot_encoder.transform(test_data[cat_fts2[i]].to_numpy().reshape(-1, 1))
  test_onehot_df = pd.DataFrame(test_transformed, columns=onehot_encoder.get_feature_names())
  test_data = pd.concat([test_data, test_onehot_df], axis=1).drop(cat_fts2[i], axis=1)</code></pre>
<pre><code class="language-python">print(train_data.columns)
print(test_data.columns)</code></pre>
<pre>
Index(['id', 'odometer', 'engine', 'year', 'target', 'company', 'x0_ALPINA B3',
       'x0_Acura MDX', 'x0_Acura RDX', 'x0_Acura TL',
       ...
       'x0_gold', 'x0_gray', 'x0_green', 'x0_light blue', 'x0_light gray',
       'x0_purple', 'x0_red', 'x0_white', 'x0_wine', 'x0_yellow'],
      dtype='object', length=225)
Index(['id', 'odometer', 'engine', 'year', 'company', 'x0_ALPINA B3',
       'x0_Acura MDX', 'x0_Acura RDX', 'x0_Acura TL', 'x0_Acura ZDX',
       ...
       'x0_gold', 'x0_gray', 'x0_green', 'x0_light blue', 'x0_light gray',
       'x0_purple', 'x0_red', 'x0_white', 'x0_wine', 'x0_yellow'],
      dtype='object', length=224)
</pre>

<p>📝 train 데이터의 target 컬럼을 제외하고는 train과 test의 열길이가 같도록 one-hot encoding이 잘 진행된것을 확인할 수 있습니다.</p>
<pre><code class="language-python">train_x = train_data.drop(&#39;id&#39;, axis = 1)
test_x = test_data.drop(&#39;id&#39;, axis = 1)

print(train_x.shape)
print(test_x.shape)</code></pre>
<pre>
(975, 224)
(436, 223)
</pre>

<h2 id="4-modeling"><strong>4. Modeling</strong></h2>
<p>📝 pycaret을 활용했습니다.</p>
<pre><code class="language-python">py_reg = setup(train_x, target = &#39;target&#39;, session_id = seed_num, silent = True)</code></pre>
<pre>
                               Description             Value
0                               session_id                42
1                                   Target            target
2                            Original Data        (975, 224)
3                           Missing Values             False
4                         Numeric Features                35
5                     Categorical Features               188
6                         Ordinal Features             False
7                High Cardinality Features             False
8                  High Cardinality Method              None
9                    Transformed Train Set        (682, 226)
10                    Transformed Test Set        (293, 226)
11                      Shuffle Train-Test              True
12                     Stratify Train-Test             False
13                          Fold Generator             KFold
14                             Fold Number                10
15                                CPU Jobs                -1
16                                 Use GPU             False
17                          Log Experiment             False
18                         Experiment Name  reg-default-name
19                                     USI              ee21
20                         Imputation Type            simple
21          Iterative Imputation Iteration              None
22                         Numeric Imputer              mean
23      Iterative Imputation Numeric Model              None
24                     Categorical Imputer          constant
25  Iterative Imputation Categorical Model              None
26           Unknown Categoricals Handling    least_frequent
27                               Normalize             False
28                        Normalize Method              None
29                          Transformation             False
30                   Transformation Method              None
31                                     PCA             False
32                              PCA Method              None
33                          PCA Components              None
34                     Ignore Low Variance             False
35                     Combine Rare Levels             False
36                    Rare Level Threshold              None
37                         Numeric Binning             False
38                         Remove Outliers             False
39                      Outliers Threshold              None
40                Remove Multicollinearity             False
41             Multicollinearity Threshold              None
42             Remove Perfect Collinearity              True
43                              Clustering             False
44                    Clustering Iteration              None
45                     Polynomial Features             False
46                       Polynomial Degree              None
47                    Trignometry Features             False
48                    Polynomial Threshold              None
49                          Group Features             False
50                       Feature Selection             False
51                Feature Selection Method           classic
52            Features Selection Threshold              None
53                     Feature Interaction             False
54                           Feature Ratio             False
55                   Interaction Threshold              None
56                        Transform Target             False
57                 Transform Target Method           box-cox
</pre>

<pre><code class="language-python">compare_models()</code></pre>
<pre>
                                    Model           MAE           MSE  \
catboost               CatBoost Regressor  2.052122e+06  3.032507e+13   
gbr           Gradient Boosting Regressor  2.215648e+06  3.169851e+13   
rf                Random Forest Regressor  2.132068e+06  3.173878e+13   
et                  Extra Trees Regressor  2.235193e+06  3.563028e+13   
ridge                    Ridge Regression  3.439487e+06  4.245590e+13   
dt                Decision Tree Regressor  2.503733e+06  3.621137e+13   
omp           Orthogonal Matching Pursuit  3.249912e+06  4.415962e+13   
lr                      Linear Regression  3.577824e+06  4.495084e+13   
llar         Lasso Least Angle Regression  3.479438e+06  4.552524e+13   
lasso                    Lasso Regression  3.562897e+06  4.500952e+13   
lightgbm  Light Gradient Boosting Machine  3.335596e+06  4.506558e+13   
en                            Elastic Net  4.823784e+06  7.191481e+13   
ada                    AdaBoost Regressor  5.726544e+06  6.274745e+13   
knn                 K Neighbors Regressor  5.217788e+06  8.947216e+13   
br                         Bayesian Ridge  5.853927e+06  9.663452e+13   
huber                     Huber Regressor  5.072447e+06  1.106676e+14   
dummy                     Dummy Regressor  6.606546e+06  1.206503e+14   
par          Passive Aggressive Regressor  6.787941e+06  1.124849e+14   
lar                Least Angle Regression  7.229185e+28  7.044768e+59   

                  RMSE            R2    RMSLE          MAPE  TT (Sec)  
catboost  4.874472e+06  7.763000e-01   0.3940  2.705000e-01     5.786  
gbr       4.992229e+06  7.507000e-01   0.3991  3.435000e-01     0.201  
rf        4.964929e+06  7.477000e-01   0.3567  2.637000e-01     0.797  
et        5.401867e+06  7.073000e-01   0.3660  2.627000e-01     0.872  
ridge     5.963903e+06  6.569000e-01   0.8327  9.147000e-01     0.036  
dt        5.587945e+06  6.550000e-01   0.4376  2.988000e-01     0.027  
omp       6.123065e+06  6.337000e-01   0.8006  7.729000e-01     0.020  
lr        6.188975e+06  6.291000e-01   0.7927  9.623000e-01     0.349  
llar      6.215977e+06  6.243000e-01   0.8050  9.201000e-01     0.084  
lasso     6.206766e+06  6.219000e-01   0.7581  9.474000e-01     0.072  
lightgbm  6.324969e+06  5.892000e-01   0.5592  4.798000e-01     0.091  
en        7.970752e+06  3.732000e-01   0.8749  1.184900e+00     0.103  
ada       7.697862e+06  3.293000e-01   0.9726  1.669600e+00     0.149  
knn       8.988560e+06  1.959000e-01   0.8459  1.057700e+00     0.071  
br        9.334507e+06  1.286000e-01   1.0131  1.458200e+00     0.048  
huber     9.854927e+06  9.680000e-02   0.8858  8.251000e-01     0.079  
dummy     1.044993e+07 -6.330000e-02   1.1069  1.864000e+00     0.013  
par       1.019090e+07 -1.097000e-01   1.1548  1.859500e+00     0.026  
lar       2.657465e+29 -6.320727e+45  28.3450  1.132970e+22     0.113  
</pre>
<pre>
<catboost.core.CatBoostRegressor at 0x7f9eb6f53c50>
</pre>

<pre><code class="language-python">catboost = create_model(&#39;catboost&#39;, verbose = False)
rf = create_model(&#39;rf&#39;, verbose = False)
gbr = create_model(&#39;gbr&#39;, verbose = False)</code></pre>
<p>📝 상위 3개의 모델을 혼합한 모델을 생성합니다.</p>
<pre><code class="language-python">blended_model = blend_models(estimator_list = [catboost, rf, gbr])</code></pre>
<pre>
               MAE           MSE          RMSE      R2   RMSLE    MAPE
Fold                                                                  
0     3.368465e+06  1.239934e+14  1.113523e+07  0.6025  0.3779  0.3052
1     1.523530e+06  7.672571e+12  2.769941e+06  0.8638  0.3379  0.2818
2     1.430990e+06  6.330266e+12  2.516002e+06  0.8527  0.3042  0.2395
3     1.205003e+06  6.456912e+12  2.541045e+06  0.7147  0.3131  0.2569
4     2.395485e+06  2.857651e+13  5.345700e+06  0.6260  0.3721  0.3061
5     3.142842e+06  6.432011e+13  8.019982e+06  0.5683  0.3675  0.2571
6     1.753312e+06  1.835539e+13  4.284319e+06  0.8353  0.3038  0.2304
7     1.810014e+06  2.096415e+13  4.578662e+06  0.8783  0.3255  0.2637
8     1.982680e+06  1.493599e+13  3.864710e+06  0.8744  0.3505  0.2618
9     1.554736e+06  7.822396e+12  2.796855e+06  0.9317  0.3449  0.3052
Mean  2.016706e+06  2.994277e+13  4.785245e+06  0.7748  0.3398  0.2708
Std   6.935942e+05  3.544528e+13  2.654091e+06  0.1269  0.0263  0.0262
</pre>

<p>📝 전체 데이터로 마지막 학습을 진행하고 test 예측을 생성합니다.</p>
<pre><code class="language-python">final_model = finalize_model(blended_model)
prediction = predict_model(final_model, data = test_x)</code></pre>
<pre><code class="language-python">pred = prediction[&#39;Label&#39;]</code></pre>
<hr>
<p>읽어주셔서 감사합니다 :)<br>도움이 됐길 바랍니다👍👍</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[Dacon] 소비자 데이터 기반 소비 예측 경진대회]]></title>
            <link>https://velog.io/@standing-o/forecasting-spending</link>
            <guid>https://velog.io/@standing-o/forecasting-spending</guid>
            <pubDate>Tue, 23 Aug 2022 12:59:51 GMT</pubDate>
            <description><![CDATA[<h1 id="소비자-데이터-기반-소비-예측"><strong>소비자 데이터 기반 소비 예측</strong></h1>
<ul>
<li>본 포스팅은 간단한 데이터 전처리 및 EDA와 Ensemble(Elasticnet, LightGBM, XGBoost) 등의 내용을 포함하고 있습니다.  </li>
<li>코드실행은 Google Colab의 CPU, Standard RAM 환경에서 진행했습니다.  </li>
<li>Keyword : 소비예측, regression, lightgbm, xgboost, elasticnet, ensemble
➔ <a href="https://dacon.io/codeshare/4881">데이콘에서 읽기</a>  </li>
<li>👉<a href="https://standing-o.github.io/posts/forecasting-spending/"><code>Click</code></a></li>
</ul>
<h2 id="0-import-packages"><strong>0. Import Packages</strong></h2>
<pre><code class="language-python">from google.colab import drive
drive.mount(&#39;/content/drive&#39;)</code></pre>
<pre>
Mounted at /content/drive
</pre>

<pre><code class="language-python">!pip install folium==0.2.1
!pip install markupsafe==2.0.1
!pip install -U pandas-profiling</code></pre>
<pre><code class="language-python">import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import sklearn
import pandas_profiling
import seaborn as sns
import random as rn
import os
import scipy.stats as stats
import datetime
import calendar

from sklearn.preprocessing import PowerTransformer
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split

from sklearn.model_selection import GridSearchCV, cross_val_score, RepeatedKFold
from sklearn import metrics

from sklearn.linear_model import ElasticNet
from xgboost import XGBRegressor
from lightgbm import LGBMRegressor

from collections import Counter
import warnings

%matplotlib inline
warnings.filterwarnings(action=&#39;ignore&#39;)</code></pre>
<pre><code class="language-python">print(&quot;numpy version: {}&quot;. format(np.__version__))
print(&quot;pandas version: {}&quot;. format(pd.__version__))
print(&quot;matplotlib version: {}&quot;. format(matplotlib.__version__))
print(&quot;scikit-learn version: {}&quot;. format(sklearn.__version__))
print(&quot;xgboost version: {}&quot;. format(xgb.__version__))
print(&quot;lightgbm version: {}&quot;. format(lgb.__version__))</code></pre>
<pre>
numpy version: 1.21.6
pandas version: 1.3.5
matplotlib version: 3.2.2
scikit-learn version: 1.0.2
xgboost version: 0.90
lightgbm version: 2.2.3
</pre>

<pre><code class="language-python"># reproducibility
seed_num = 42   ####
np.random.seed(seed_num)
rn.seed(seed_num)
os.environ[&#39;PYTHONHASHSEED&#39;]=str(seed_num)</code></pre>
<h2 id="1-load-and-check-dataset"><strong>1. Load and Check Dataset</strong></h2>
<pre><code class="language-python">train = pd.read_csv(&#39;/content/drive/MyDrive/Consumer_spending_forecast/dataset/train.csv&#39;)
test = pd.read_csv(&#39;/content/drive/MyDrive/Consumer_spending_forecast/dataset/test.csv&#39;)

print(train.shape)
train.head()</code></pre>
<pre>
(1108, 22)
</pre>
<pre>
   id  Year_Birth   Education Marital_Status   Income  Kidhome  Teenhome  \
0   0        1974      Master       Together  46014.0        1         1   
1   1        1962  Graduation         Single  76624.0        0         1   
2   2        1951  Graduation        Married  75903.0        0         1   
3   3        1974       Basic        Married  18393.0        1         0   
4   4        1946         PhD       Together  64014.0        2         1   

  Dt_Customer  Recency  NumDealsPurchases  ...  NumStorePurchases  \
0  21-01-2013       21                 10  ...                  8   
1  24-05-2014       68                  1  ...                  7   
2  08-04-2013       50                  2  ...                  9   
3  29-03-2014        2                  2  ...                  3   
4  10-06-2014       56                  7  ...                  5   

   NumWebVisitsMonth  AcceptedCmp3  AcceptedCmp4  AcceptedCmp5  AcceptedCmp1  \
0                  7             0             0             0             0   
1                  1             1             0             0             0   
2                  3             0             0             0             0   
3                  8             0             0             0             0   
4                  7             0             0             0             1   

   AcceptedCmp2  Complain  Response  target  
0             0         0         0     541  
1             0         0         0     899  
2             0         0         0     901  
3             0         0         0      50  
4             0         0         0     444  

[5 rows x 22 columns]
</pre>

<pre><code class="language-python">pr = train.profile_report()
pr.to_file(&#39;/content/drive/MyDrive/Consumer_spending_forecast/pr_report.html&#39;)
pr</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/7ee7fb00-9e15-446c-a7e2-11f69137672d/image.png" alt=""></p>
<h3 id="summary-of-pandas-profiling--alert"><strong>Summary of Pandas profiling : Alert</strong></h3>
<h4 id="high-correlation"><strong>High Correlation</strong></h4>
<ul>
<li><p><code>Income</code>-<code>Kidhome</code>-<code>NumWebPurchases</code>-<code>NumStorePurchases</code>-<code>NumStorePurchases</code>-<code>NumWebVisitsMonth</code>-<code>AcceptedCmp1</code>-<code>AcceptedCmp5</code>-<code>target</code></p>
</li>
<li><p><code>NumDealsPurchases</code>-<code>Teenhome</code></p>
</li>
</ul>
<h4 id="high-cardinality"><strong>High Cardinality</strong></h4>
<p><code>Dt_customer</code></p>
<ul>
<li>↪ 고객이 회사에 등록한 날짜를 의미하기 때문에 중복도가 낮은 데이터입니다.</li>
</ul>
<hr>
<p>📝 Cardinality가 높다 &lt;-&gt; 중복되는 값이 적다</p>
<h2 id="2-eda--exploratory-data-analysis">2. EDA | Exploratory Data Analysis</h2>
<ul>
<li><p><code>id</code> : 샘플 아이디, <code>Year_Birth</code> : 고객 생년월일, <code>Education</code> : 고객 학력</p>
</li>
<li><p><code>Marital_status</code> : 고객 결혼 상태, <code>Income</code> : 고객 연간 가구 소득</p>
</li>
<li><p><code>Kidhome</code> : 고객 가구의 자녀 수, <code>Teenhome</code> : 고객 가구의 청소년 수, <code>Dt_Customer</code> : 고객이 회사에 등록한 날짜</p>
</li>
<li><p><code>Recency</code> : 고객의 마지막 구매 이후 일수, <code>NumDealsPurchases</code> : 할인된 구매 횟수, <code>NumWebPurchases</code> : 회사 웹사이트를 통한 구매 건수</p>
</li>
<li><p><code>NumCatalogPurchases</code> : 카탈로그를 사용한 구매 수, <code>NumStorePuchases</code> : 매장에서 직접 구매한 횟수</p>
</li>
<li><p><code>NumWebVisitsMonth</code> : 지난 달 회사 웹사이트 방문 횟수</p>
</li>
<li><p><code>AcceptedCmp(1-5)</code> : 고객이 (1-5) 번째 캠페인에서 제안을 수락한 경우 1, 그렇지 않은 경우 0</p>
</li>
<li><p><code>Complain</code> : 고객이 지난 2년 동안 불만을 제기한 경우 1, 그렇지 않은 경우 0</p>
</li>
<li><p><code>Response</code> : 고객이 마지막 캠페인에서 제안을 수락한 경우 1, 그렇지 않은 경우 0</p>
</li>
<li><p><code>target</code> : 고객의 제품 총 소비량</p>
</li>
</ul>
<hr>
<h3 id="data-type"><strong>Data Type</strong></h3>
<ul>
<li><p>Numeric (10) : <code>id</code>, <code>Year_Birth</code>, <code>Income</code>, <code>Recency</code>, <code>NumDealsPurchases</code>, <code>NumWebPurchases</code>, <code>NumCatalogPurchases</code>, <code>NumStorePurchases</code>, <code>NumWebVisitsMonth</code>, <code>target</code></p>
</li>
<li><p>Categorical (12) : <code>Education</code>, <code>Marital_Status</code>, <code>Kidhome</code>, <code>Teenhome</code>, <code>Dt_Customer</code>, <code>AcceptedCmp(1~5)</code>, <code>Complain</code>, <code>Response</code></p>
</li>
</ul>
<hr>
<p>📝 결측치가 없습니다.</p>
<pre><code class="language-python">train.isnull().sum()</code></pre>
<pre>
id                     0
Year_Birth             0
Education              0
Marital_Status         0
Income                 0
Kidhome                0
Teenhome               0
Dt_Customer            0
Recency                0
NumDealsPurchases      0
NumWebPurchases        0
NumCatalogPurchases    0
NumStorePurchases      0
NumWebVisitsMonth      0
AcceptedCmp3           0
AcceptedCmp4           0
AcceptedCmp5           0
AcceptedCmp1           0
AcceptedCmp2           0
Complain               0
Response               0
target                 0
dtype: int64
</pre>

<pre><code class="language-python">test.isnull().sum()</code></pre>
<pre>
id                     0
Year_Birth             0
Education              0
Marital_Status         0
Income                 0
Kidhome                0
Teenhome               0
Dt_Customer            0
Recency                0
NumDealsPurchases      0
NumWebPurchases        0
NumCatalogPurchases    0
NumStorePurchases      0
NumWebVisitsMonth      0
AcceptedCmp3           0
AcceptedCmp4           0
AcceptedCmp5           0
AcceptedCmp1           0
AcceptedCmp2           0
Complain               0
Response               0
dtype: int64
</pre>

<pre><code class="language-python">df_train = train.copy()
df_test = test.copy()</code></pre>
<h4 id="2-1-outliers"><strong>2-(1). Outliers</strong></h4>
<ul>
<li>📝 <code>id</code>와 <code>target</code>을 제외한 numerical 데이터의 outlier 들을 IQR method를 활용하여 찾아줍니다.</li>
</ul>
<pre><code class="language-python">numeric_fts = [&#39;Year_Birth&#39;, &#39;Income&#39;, &#39;Recency&#39;, &#39;NumDealsPurchases&#39;, &#39;NumWebPurchases&#39;, &#39;NumCatalogPurchases&#39;, &#39;NumStorePurchases&#39;, &#39;NumWebVisitsMonth&#39;]

train_outlier_ind = []
for i in numeric_fts:
  Q1 = np.percentile(df_train[i],25)
  Q3 = np.percentile(df_train[i],75)
  IQR = Q3-Q1
  train_outlier_list = df_train[(df_train[i] &lt; Q1 - IQR * 1.5) | (df_train[i] &gt; Q3 + IQR * 1.5)].index
  train_outlier_ind.extend(train_outlier_list)

train_outlier_ind = Counter(train_outlier_ind)
train_multi_outliers = list(k for k,j in train_outlier_ind.items() if j &gt; 2)  

print(&quot;The number of train outliers :&quot;, len(train_multi_outliers))</code></pre>
<pre>
The number of train outliers : 0
</pre>

<p>📝 Train 데이터에는 IQR method로 탐지되는 이상치가 없는것을 확인할 수 있습니다.</p>
<h4 id="2-2-transformation"><strong>2-(2). Transformation</strong></h4>
<p>📝 왜곡된 분포는 모델 학습에 안좋은 영향을 줄 수 있습니다. 높은 skewness를 가지고 있는 <code>NumDealsPurchases</code> 변수에 대하여 몇가지 transformation을 진행하려합니다.</p>
<pre><code class="language-python">print(df_train[numeric_fts].skew())</code></pre>
<pre>
Year_Birth            -0.439100
Income                 0.291634
Recency               -0.061310
NumDealsPurchases      2.264245
NumWebPurchases        1.289607
NumCatalogPurchases    1.099499
NumStorePurchases      0.653689
NumWebVisitsMonth      0.299000
dtype: float64
</pre>

<pre><code class="language-python">fig = plt.figure(figsize = (16,6))
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

sns.distplot(df_train[&#39;NumDealsPurchases&#39;], ax = ax1, label=&#39;Skewness : {:.2f}&#39;.format(df_train[&#39;NumDealsPurchases&#39;].skew()))
ax1.legend(loc=&#39;best&#39;, fontsize = 15)

stats.probplot(df_train[&#39;NumDealsPurchases&#39;], plot = ax2)
plt.title(&quot;Q-Q Plot&quot;, fontsize = 15)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/97866172-b33c-4851-aed4-06376653ec66/image.png" alt=""></p>
<h4 id="log-transformation"><strong>Log transformation</strong></h4>
<pre><code class="language-python">log_trans = df_train[&#39;NumDealsPurchases&#39;].map(lambda i: np.log(i) if i &gt; 0 else 0)

fig = plt.figure(figsize = (16,6))
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

sns.distplot(log_trans, ax = ax1, color=&#39;crimson&#39;, label=&#39;Skewness : {:.2f}&#39;.format(log_trans.skew()))
ax1.legend(loc=&#39;best&#39;, fontsize = 15)
ax1.set_title(&#39;Log transformation&#39;, fontsize = 15)

stats.probplot(log_trans, plot = ax2)
ax2.set_title(&quot;Q-Q Plot&quot;, fontsize = 15)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/ad673a66-fa77-45cf-b29b-05776f5f9dab/image.png" alt=""></p>
<h4 id="yeo-johnson-transformation"><strong>Yeo-Johnson transformation</strong></h4>
<pre><code class="language-python">jy = PowerTransformer(method = &#39;yeo-johnson&#39;)
jy.fit(df_train[&#39;NumDealsPurchases&#39;].values.reshape(-1, 1))
x_yj = jy.transform(df_train[&#39;NumDealsPurchases&#39;].values.reshape(-1, 1))

fig = plt.figure(figsize = (16,6))
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)

sns.distplot(x_yj, ax = ax1, color=&#39;crimson&#39;, label=&#39;Skewness : {:.5f}&#39;.format(np.float(stats.skew(x_yj))))
ax1.legend(loc=&#39;best&#39;, fontsize = 15)
ax1.set_title(&#39;Yeo-Johnson transformation&#39;, fontsize = 15)

stats.probplot(x_yj.reshape(x_yj.shape[0]), plot = ax2)
ax2.legend([&#39;Lambda : {:.2f}&#39;.format(np.float(jy.lambdas_))], loc=&#39;best&#39;, fontsize = 15)
ax2.set_title(&quot;Q-Q Plot&quot;, fontsize = 15)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/e511a5bd-14c2-4e54-bd34-2457480a4ab6/image.png" alt=""></p>
<p>📝 두 변환을 진행한 결과 모두 조금 더 정규분포 직선과 비슷해진것을 Q-Q Plot을 통해 확인할 수 있습니다.</p>
<p>📝 데이터 전처리에는 Yeo-Johnson transformation을 사용했습니다.</p>
<pre><code class="language-python">df_train[&#39;NumDealsPurchases&#39;] = x_yj
df_train[&#39;NumDealsPurchases&#39;].head()</code></pre>
<pre>
0    2.258975
1   -0.801066
2    0.146388
3    0.146388
4    1.846930
Name: NumDealsPurchases, dtype: float64
</pre>

<pre><code class="language-python">test_jy = PowerTransformer(method = &#39;yeo-johnson&#39;)
test_jy.fit(df_test[&#39;NumDealsPurchases&#39;].values.reshape(-1, 1))
test_x_yj = test_jy.transform(df_test[&#39;NumDealsPurchases&#39;].values.reshape(-1, 1))
df_test[&#39;NumDealsPurchases&#39;] = test_x_yj</code></pre>
<h3 id="2-3-correlation"><strong>2-(3). Correlation</strong></h3>
<p>📝 앞서 수행한 pandas profiling report의 alert를 참고하여 상관계수를 계산했습니다.</p>
<pre><code class="language-python">corr_fts1 = [&#39;Income&#39;, &#39;Kidhome&#39;, &#39;NumWebPurchases&#39;, &#39;NumCatalogPurchases&#39;, &#39;NumStorePurchases&#39;, &#39;NumWebVisitsMonth&#39;, &#39;AcceptedCmp1&#39;, &#39;AcceptedCmp5&#39;, &#39;target&#39;]
corr_fts2 = [&#39;NumDealsPurchases&#39;, &#39;Teenhome&#39;]</code></pre>
<pre><code class="language-python">plt.figure(figsize = (10,8))
sns.heatmap(df_train[corr_fts1].corr(), annot=True)

plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/d8b90380-32ed-43a3-a185-3f3866d44d98/image.png" alt=""></p>
<pre><code class="language-python">plt.figure(figsize = (8,6))
sns.heatmap(df_train[corr_fts2].corr(), annot=True)

plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/65ddf97e-a83c-45f4-a3ea-eb573be2e30d/image.png" alt=""></p>
<p>📝 독립변수 간의 높은 상관관계는 다중공선성을 유발하기 때문에 좋지 않습니다.</p>
<p>📝 위 문제는 변수 선택, 차원 축소, 규제 등의 방법으로 해결할 수 있고, 저는 모델에 규제를 적용하거나 다중공선성의 영향을 적게 받는다고 생각되는 Decision Tree 베이스의 모델을 사용 할 예정입니다.</p>
<pre><code class="language-python">train_dataset = df_train.copy()
test_dataset = df_test.copy()</code></pre>
<h2 id="3-feature-engineering"><strong>3. Feature Engineering</strong></h2>
<h4 id="3-1-dt_customer-변수--날짜-데이터-다루기"><strong>3-(1) <code>Dt_Customer</code> 변수 : 날짜 데이터 다루기</strong></h4>
<p>📝 <code>Dt_Customer</code> 변수는 회사 등록일을 뜻합니다. 회사에 등록한 시점에 대한 정보를 유지하면서 모델링에 사용할 수 있는 새 수치형 변수를 만들려고합니다.  </p>
<p>↪ 가장 과거 시점의 회사 등록일로부터 며칠이 지났는지를 뜻하는 <code>Pass_Customer</code>변수를 새롭게 생성합니다. </p>
<pre><code class="language-python">train_dataset[&quot;Dt_Customer&quot;]</code></pre>
<pre>
0       21-01-2013
1       24-05-2014
2       08-04-2013
3       29-03-2014
4       10-06-2014
           ...    
1103    31-03-2013
1104    21-10-2013
1105    16-12-2013
1106    30-05-2013
1107    29-10-2012
Name: Dt_Customer, Length: 1108, dtype: object
</pre>

<pre><code class="language-python">train_dataset[&quot;Dt_Customer&quot;] = pd.to_datetime(train_dataset[&quot;Dt_Customer&quot;], format=&#39;%d-%m-%Y&#39;)
test_dataset[&quot;Dt_Customer&quot;] = pd.to_datetime(test_dataset[&quot;Dt_Customer&quot;], format=&#39;%d-%m-%Y&#39;)

train_dataset[&quot;Dt_Customer&quot;]</code></pre>
<pre>
0      2013-01-21
1      2014-05-24
2      2013-04-08
3      2014-03-29
4      2014-06-10
          ...    
1103   2013-03-31
1104   2013-10-21
1105   2013-12-16
1106   2013-05-30
1107   2012-10-29
Name: Dt_Customer, Length: 1108, dtype: datetime64[ns]
</pre>

<pre><code class="language-python">print(f&#39;Minimum date: {train_dataset[&quot;Dt_Customer&quot;].min()}&#39;)
print(f&#39;Maximum date: {train_dataset[&quot;Dt_Customer&quot;].max()}&#39;)</code></pre>
<pre>
Minimum date: 2012-07-31 00:00:00
Maximum date: 2014-06-29 00:00:00
</pre>

<pre><code class="language-python">train_diff_date = train_dataset[&quot;Dt_Customer&quot;] - train_dataset[&quot;Dt_Customer&quot;].min()
test_diff_date = test_dataset[&quot;Dt_Customer&quot;] - test_dataset[&quot;Dt_Customer&quot;].min()

train_dataset[&quot;Pass_Customer&quot;] = [i.days for i in train_diff_date]
test_dataset[&quot;Pass_Customer&quot;] = [i.days for i in test_diff_date]</code></pre>
<pre><code class="language-python">train_dataset[&quot;Pass_Customer&quot;].head()</code></pre>
<pre>
0    174
1    662
2    251
3    606
4    679
Name: Pass_Customer, dtype: int64
</pre>

<h4 id="3-2-year_birth-to-age"><strong>3-(2) <code>Year_Birth</code> to <code>Age</code></strong></h4>
<ul>
<li><p>📝 <code>Year_Birth</code> 변수를 활용하여 고객의 나이를 뜻하는 <code>Age</code> 변수를 새롭게 생성했습니다.</p>
</li>
<li><p>📝 한국나이로 계산했습니다.</p>
</li>
</ul>
<pre><code class="language-python">print(&quot;Minimum birth :&quot;, train_dataset[&quot;Year_Birth&quot;].min(), &quot;\nMaximum birth :&quot;, train_dataset[&quot;Year_Birth&quot;].max(), &quot;\n&quot;)
train_dataset[&quot;Year_Birth&quot;].head()</code></pre>
<pre>
Minimum birth : 1893 
Maximum birth : 1996 

</pre>
<pre>
0    1974
1    1962
2    1951
3    1974
4    1946
Name: Year_Birth, dtype: int64
</pre>

<pre><code class="language-python">train_dataset[&quot;Age&quot;] = 2022 - train_dataset[&quot;Year_Birth&quot;] + 1
test_dataset[&quot;Age&quot;] = 2022 - test_dataset[&quot;Year_Birth&quot;] + 1

train_dataset[&quot;Age&quot;].head()</code></pre>
<pre>
0    49
1    61
2    72
3    49
4    77
Name: Age, dtype: int64
</pre>

<h4 id="3-3-acceptedcmp15-와-response-변수로-새-feature-생성"><strong>3-(3) <code>AcceptedCmp(1~5)</code> 와 <code>Response</code> 변수로 새 Feature 생성</strong></h4>
<p>📝 위 여섯개의 변수는 고객이 1~5 번째와 마지막 캠페인에서 제안을 수락한 경우 1, 아닌경우 0 값을 가집니다.<br>📝 이 변수들을 활용하여 캠페인에서 제안을 수락한 횟수를 나타내는 <code>AcceptCount</code> 변수를 새롭게 생성하겠습니다.  </p>
<pre><code class="language-python">train_dataset[&quot;AcceptCount&quot;] = train_dataset[&quot;AcceptedCmp1&quot;] + train_dataset[&quot;AcceptedCmp2&quot;] + train_dataset[&quot;AcceptedCmp3&quot;] + train_dataset[&quot;AcceptedCmp4&quot;] + train_dataset[&quot;AcceptedCmp5&quot;] + train_dataset[&quot;Response&quot;]
test_dataset[&quot;AcceptCount&quot;] = test_dataset[&quot;AcceptedCmp1&quot;] + test_dataset[&quot;AcceptedCmp2&quot;] + test_dataset[&quot;AcceptedCmp3&quot;] + test_dataset[&quot;AcceptedCmp4&quot;] + test_dataset[&quot;AcceptedCmp5&quot;] + test_dataset[&quot;Response&quot;]

train_dataset[&quot;AcceptCount&quot;].head()</code></pre>
<pre>
0    0
1    1
2    0
3    0
4    1
Name: AcceptCount, dtype: int64
</pre>

<pre><code class="language-python">print(&quot;Minimum count :&quot;, train_dataset[&quot;AcceptCount&quot;].min(), &quot;\nMaximum count :&quot;, train_dataset[&quot;AcceptCount&quot;].max(), &quot;\n&quot;)</code></pre>
<pre>
Minimum count : 0 
Maximum count : 5 

</pre>

<p>📝 train 데이터에서 캠페인 제안을 여섯번 모두 수락한 경우는 없는것을 확인할 수 있습니다.<br>📝 원래의 변수와 <code>target</code>과의 상관관계를 확인하겠습니다.  </p>
<pre><code class="language-python">train_dataset[[&#39;Year_Birth&#39;, &#39;AcceptedCmp1&#39;,&#39;AcceptedCmp2&#39;,&#39;AcceptedCmp3&#39;,&#39;AcceptedCmp4&#39;,&#39;AcceptedCmp5&#39;, &#39;Response&#39;,&#39;target&#39;]].corr()</code></pre>
<pre>
              Year_Birth  AcceptedCmp1  AcceptedCmp2  AcceptedCmp3  \
Year_Birth      1.000000     -0.050053     -0.034204      0.066802   
AcceptedCmp1   -0.050053      1.000000      0.198530      0.052213   
AcceptedCmp2   -0.034204      0.198530      1.000000      0.052513   
AcceptedCmp3    0.066802      0.052213      0.052513      1.000000   
AcceptedCmp4   -0.111485      0.184717      0.328941     -0.083690   
AcceptedCmp5   -0.010873      0.379563      0.192139      0.060890   
Response       -0.012304      0.268577      0.201945      0.194275   
target         -0.136035      0.361102      0.129995      0.040736   

              AcceptedCmp4  AcceptedCmp5  Response    target  
Year_Birth       -0.111485     -0.010873 -0.012304 -0.136035  
AcceptedCmp1      0.184717      0.379563  0.268577  0.361102  
AcceptedCmp2      0.328941      0.192139  0.201945  0.129995  
AcceptedCmp3     -0.083690      0.060890  0.194275  0.040736  
AcceptedCmp4      1.000000      0.313120  0.189849  0.256784  
AcceptedCmp5      0.313120      1.000000  0.336610  0.458208  
Response          0.189849      0.336610  1.000000  0.242760  
target            0.256784      0.458208  0.242760  1.000000  
</pre>

<pre><code class="language-python"># Dt_customer
year = pd.to_datetime(train_dataset[&quot;Dt_Customer&quot;]).dt.year
month = pd.to_datetime(train_dataset[&quot;Dt_Customer&quot;]).dt.month
day = pd.to_datetime(train_dataset[&quot;Dt_Customer&quot;]).dt.day

print(np.corrcoef(year, train_dataset[&#39;target&#39;]), &#39;\n&#39;)
print(np.corrcoef(month, train_dataset[&#39;target&#39;]), &#39;\n&#39;)
print(np.corrcoef(day, train_dataset[&#39;target&#39;]))</code></pre>
<pre>
[[ 1.         -0.15940385]
 [-0.15940385  1.        ]] 

[[1.         0.03764911]
 [0.03764911 1.        ]] 

[[1.         0.01891694]
 [0.01891694 1.        ]]
</pre>
<p>📝 새로 생성한 변수와 <code>target</code>과의 상관관계를 확인하겠습니다.  </p>
<pre><code class="language-python">train_dataset[[&#39;Pass_Customer&#39;, &#39;Age&#39;, &#39;AcceptCount&#39;, &#39;target&#39;]].corr()</code></pre>
<pre>
               Pass_Customer       Age  AcceptCount    target
Pass_Customer       1.000000  0.012309    -0.080152 -0.174969
Age                 0.012309  1.000000     0.043180  0.136035
AcceptCount        -0.080152  0.043180     1.000000  0.444114
target             -0.174969  0.136035     0.444114  1.000000
</pre>
<p>📝 정리하자면, <code>Pass_Customer</code>-<code>target</code>의 상관계수 절댓값이 <code>Dt_Customer</code>(<code>year</code>, <code>month</code>, <code>day</code>)-<code>target</code> 보다 조금 더 크다는 것을 확인할 수 있습니다.  </p>
<p>📝 또한, 당연하게도 <code>Year_Birth</code>를 <code>Age</code> 변수로 바꾼것은 상관관계에 아무런 영향도 주지 못했습니다.  </p>
<p>📝 <code>AcceptCount</code>는 <code>target</code>과 어느정도 상관관계가 있습니다.  </p>
<pre><code class="language-python">train_data = train_dataset.copy()
test_data = test_dataset.copy()</code></pre>
<h2 id="4-one-hot-encoding"><strong>4. One-Hot Encoding</strong></h2>
<p>📝 <code>Education</code>, <code>Marital_Status</code> 변수의 one-hot encoding을 진행했습니다.  </p>
<pre><code class="language-python">drop_col = [&#39;id&#39;, &#39;Dt_Customer&#39;, &#39;Year_Birth&#39;, &#39;AcceptedCmp1&#39;, &#39;AcceptedCmp2&#39;, &#39;AcceptedCmp3&#39;, &#39;AcceptedCmp4&#39;, &#39;AcceptedCmp5&#39;, &#39;Response&#39;]

train_data = train_data.drop(drop_col, axis = 1)
test_data = test_data.drop(drop_col, axis = 1)</code></pre>
<pre><code class="language-python">print(train_data[&#39;Education&#39;].unique())
print(train_data[&#39;Marital_Status&#39;].unique())</code></pre>
<pre>
['Master' 'Graduation' 'Basic' 'PhD' '2n Cycle']
['Together' 'Single' 'Married' 'Widow' 'Divorced' 'Alone' 'YOLO' 'Absurd']
</pre>

<pre><code class="language-python"># One-hot encoding
train_data = pd.get_dummies(train_data)
test_data = pd.get_dummies(test_data)

print(train_data.columns)
print(test_data.columns)</code></pre>
<pre>
Index(['Income', 'Kidhome', 'Teenhome', 'Recency', 'NumDealsPurchases',
       'NumWebPurchases', 'NumCatalogPurchases', 'NumStorePurchases',
       'NumWebVisitsMonth', 'Complain', 'target', 'Pass_Customer', 'Age',
       'AcceptCount', 'Education_2n Cycle', 'Education_Basic',
       'Education_Graduation', 'Education_Master', 'Education_PhD',
       'Marital_Status_Absurd', 'Marital_Status_Alone',
       'Marital_Status_Divorced', 'Marital_Status_Married',
       'Marital_Status_Single', 'Marital_Status_Together',
       'Marital_Status_Widow', 'Marital_Status_YOLO'],
      dtype='object')
Index(['Income', 'Kidhome', 'Teenhome', 'Recency', 'NumDealsPurchases',
       'NumWebPurchases', 'NumCatalogPurchases', 'NumStorePurchases',
       'NumWebVisitsMonth', 'Complain', 'Pass_Customer', 'Age', 'AcceptCount',
       'Education_2n Cycle', 'Education_Basic', 'Education_Graduation',
       'Education_Master', 'Education_PhD', 'Marital_Status_Absurd',
       'Marital_Status_Alone', 'Marital_Status_Divorced',
       'Marital_Status_Married', 'Marital_Status_Single',
       'Marital_Status_Together', 'Marital_Status_Widow',
       'Marital_Status_YOLO'],
      dtype='object')
</pre>

<pre><code class="language-python">print(&quot;Length of train column :&quot;, len(train_data.columns))
print(&quot;Length of test column :&quot;, len(test_data.columns))</code></pre>
<pre>
Length of train column : 27
Length of test column : 26
</pre>

<p>📝 train 데이터의 <code>target</code> 컬럼을 제외하고는 train과 test의 열길이가 같도록 one-hot encoding이 잘 진행된것을 확인할 수 있습니다.  </p>
<pre><code class="language-python">train_data.head()</code></pre>
<pre>
    Income  Kidhome  Teenhome  Recency  NumDealsPurchases  NumWebPurchases  \
0  46014.0        1         1       21           2.258975                7   
1  76624.0        0         1       68          -0.801066                5   
2  75903.0        0         1       50           0.146388                6   
3  18393.0        1         0        2           0.146388                3   
4  64014.0        2         1       56           1.846930                8   

   NumCatalogPurchases  NumStorePurchases  NumWebVisitsMonth  Complain  ...  \
0                    1                  8                  7         0  ...   
1                   10                  7                  1         0  ...   
2                    6                  9                  3         0  ...   
3                    0                  3                  8         0  ...   
4                    2                  5                  7         0  ...   

   Education_Master  Education_PhD  Marital_Status_Absurd  \
0                 1              0                      0   
1                 0              0                      0   
2                 0              0                      0   
3                 0              0                      0   
4                 0              1                      0   

   Marital_Status_Alone  Marital_Status_Divorced  Marital_Status_Married  \
0                     0                        0                       0   
1                     0                        0                       0   
2                     0                        0                       1   
3                     0                        0                       1   
4                     0                        0                       0   

   Marital_Status_Single  Marital_Status_Together  Marital_Status_Widow  \
0                      0                        1                     0   
1                      1                        0                     0   
2                      0                        0                     0   
3                      0                        0                     0   
4                      0                        1                     0   

   Marital_Status_YOLO  
0                    0  
1                    0  
2                    0  
3                    0  
4                    0  

[5 rows x 27 columns]
</pre>
<h2 id="5-modeling"><strong>5. Modeling</strong></h2>
<p>📝 train x 데이터와 target 데이터를 나눠줍니다.  </p>
<pre><code class="language-python">train_x = train_data.drop(&#39;target&#39;, axis = 1)
train_y = pd.DataFrame(train_data[&#39;target&#39;])</code></pre>
<pre><code class="language-python">def nmae(true, pred):
    mae = np.mean(np.abs(true-pred))
    score = mae / np.mean(np.abs(true))
    return score</code></pre>
<p>📝 Lasso, Ridge regression은 Linear regression에 규제를 적용하는 방법입니다. 저는 이 두 모델의 규제를 모두 적용할 수 있는 <strong>Elastic-Net</strong>을 사용했습니다.  </p>
<p>📝 또한 <strong>LightGBM, XGBoost</strong> 모델을 사용했고, 최종적으로 세 모델을 활용하여 <strong>Ensemble</strong>을 진행했습니다.  </p>
<h3 id="elastic-net"><strong>Elastic-Net</strong></h3>
<pre><code class="language-python">ela_param_grid = {&#39;alpha&#39;: np.arange(1e-4,1e-3,1e-4),
              &#39;l1_ratio&#39;: np.arange(0.1,1.0,0.1),
              &#39;max_iter&#39;:[100000]}

elasticnet = ElasticNet(random_state = seed_num)

ela_rkfold = RepeatedKFold(n_splits = 5, n_repeats = 5, random_state = seed_num)
ela_gsearch = GridSearchCV(elasticnet, ela_param_grid, cv = ela_rkfold, scoring=&#39;neg_mean_absolute_error&#39;,
                               verbose=1, return_train_score=True)</code></pre>
<pre><code class="language-python">ela_gsearch.fit(train_x, train_y)</code></pre>
<pre>
Fitting 25 folds for each of 81 candidates, totalling 2025 fits
</pre>
<pre>
GridSearchCV(cv=RepeatedKFold(n_repeats=5, n_splits=5, random_state=42),
             estimator=ElasticNet(random_state=42),
             param_grid={'alpha': array([0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008,
       0.0009]),
                         'l1_ratio': array([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]),
                         'max_iter': [100000]},
             return_train_score=True, scoring='neg_mean_absolute_error',
             verbose=1)
</pre>

<pre><code class="language-python">elasticnet = ela_gsearch.best_estimator_        
ela_grid_results = pd.DataFrame(ela_gsearch.cv_results_)  
ela_pred = elasticnet.predict(train_x)</code></pre>
<pre><code class="language-python">print(&quot;train nmae of elasticnet :&quot;, nmae(train_y.values, ela_pred))</code></pre>
<pre>
train nmae of elasticnet : 1.0457572302381468
</pre>
<h3 id="xgboost"><strong>XGBoost</strong></h3>
<pre><code class="language-python">xgb = XGBRegressor(objective=&#39;reg:squarederror&#39;, random_state = seed_num)

xgb_param_grid = {&#39;n_estimators&#39;:np.arange(100,500,100),
              &#39;max_depth&#39;:[1,2,3],
             }

xgb_rkfold = RepeatedKFold(n_splits = 5, n_repeats = 1, random_state = seed_num)
xgb_gsearch = GridSearchCV(xgb, xgb_param_grid, cv = xgb_rkfold, scoring=&#39;neg_mean_absolute_error&#39;,
                               verbose=1, return_train_score=True)</code></pre>
<pre><code class="language-python">xgb_gsearch.fit(train_x, train_y)</code></pre>
<pre>
Fitting 5 folds for each of 12 candidates, totalling 60 fits
</pre>
<pre>
GridSearchCV(cv=RepeatedKFold(n_repeats=1, n_splits=5, random_state=42),
             estimator=XGBRegressor(objective='reg:squarederror',
                                    random_state=42),
             param_grid={'max_depth': [1, 2, 3],
                         'n_estimators': array([100, 200, 300, 400])},
             return_train_score=True, scoring='neg_mean_absolute_error',
             verbose=1)
</pre>

<pre><code class="language-python">xgb = xgb_gsearch.best_estimator_        
xgb_grid_results = pd.DataFrame(xgb_gsearch.cv_results_)  
xgb_pred = xgb.predict(train_x)

print(&quot;train nmae of xgb :&quot;, nmae(train_y.values, xgb_pred))</code></pre>
<pre>
train nmae of xgb : 1.0603134393578721
</pre>
<h3 id="lightgbm"><strong>LightGBM</strong></h3>
<pre><code class="language-python">lgbm = LGBMRegressor(objective=&#39;regression&#39;, random_state = seed_num)

lgbm_param_grid = {&#39;n_estimators&#39;: [8,16,24], &#39;num_leaves&#39;: [6,8,12,16], &#39;reg_alpha&#39; : [1,1.2], &#39;reg_lambda&#39; : [1,1.2,1.4]}


lgbm_rkfold = RepeatedKFold(n_splits = 5, n_repeats = 1, random_state = seed_num)
lgbm_gsearch = GridSearchCV(lgbm, lgbm_param_grid, cv = lgbm_rkfold, scoring=&#39;neg_mean_absolute_error&#39;,
                               verbose=1, return_train_score=True)</code></pre>
<pre><code class="language-python">lgbm_gsearch.fit(train_x, train_y)</code></pre>
<pre>
Fitting 5 folds for each of 72 candidates, totalling 360 fits
</pre>
<pre>
GridSearchCV(cv=RepeatedKFold(n_repeats=1, n_splits=5, random_state=42),
             estimator=LGBMRegressor(objective='regression', random_state=42),
             param_grid={'n_estimators': [8, 16, 24],
                         'num_leaves': [6, 8, 12, 16], 'reg_alpha': [1, 1.2],
                         'reg_lambda': [1, 1.2, 1.4]},
             return_train_score=True, scoring='neg_mean_absolute_error',
             verbose=1)
</pre>

<pre><code class="language-python">lgbm = lgbm_gsearch.best_estimator_        
lgbm_grid_results = pd.DataFrame(lgbm_gsearch.cv_results_)  
lgbm_pred = lgbm.predict(train_x)

print(&quot;train nmae of lgbm :&quot;, nmae(train_y.values, lgbm_pred))</code></pre>
<pre>
train nmae of lgbm : 1.0075199760582296
</pre>
<h3 id="blending-models---ensemble"><strong>Blending Models - Ensemble</strong></h3>
<pre><code class="language-python">def blended_models(X):
    return ((elasticnet.predict(X)) + (xgb.predict(X)) + (lgbm.predict(X)))/3</code></pre>
<pre><code class="language-python">blended_score = nmae(train_y.values, blended_models(train_x))
print(&#39;train nmae of blended model:&#39;, blended_score)</code></pre>
<pre>
train nmae of blended model: 1.0298941681188711
</pre>


<hr>
<p>감사합니다 :)<br>도움이 됐길 바랍니다👍👍</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[Dacon] 인구 데이터 기반 소득 예측]]></title>
            <link>https://velog.io/@standing-o/forecasting-income</link>
            <guid>https://velog.io/@standing-o/forecasting-income</guid>
            <pubDate>Tue, 23 Aug 2022 12:49:34 GMT</pubDate>
            <description><![CDATA[<h1 id="인구-데이터-기반-소득-예측"><strong>인구 데이터 기반 소득 예측</strong></h1>
<ul>
<li>본 포스팅은 데이콘의 <code>인구 데이터 기반 소득 예측 경진대회</code>에 참여하여 작성한 코드이며, 간단한 데이터 전처리 및 <strong>EDA</strong>, <strong>LightGBM</strong>과 <strong>XGBoost</strong>로 <strong>Ensemble</strong> 모델링 등의 내용을 포함하고 있습니다.</li>
<li>코드실행은 Google Colab의 GPU, Standard RAM 환경에서 진행했습니다. </li>
<li>Keyword : 소득예측, classification, lightgbm, xgboost, ensemble, voting
➔ <a href="https://dacon.io/competitions/official/235892/codeshare/4865?page=1&amp;dtype=recent">데이콘에서 읽기</a></li>
<li>👉<a href="https://standing-o.github.io/posts/forecasting-income/"><code>Click</code></a></li>
</ul>
<h2 id="0-import-packages"><strong>0. Import Packages</strong></h2>
<pre><code class="language-python">from google.colab import drive
drive.mount(&#39;/content/drive&#39;)</code></pre>
<pre>
Mounted at /content/drive
</pre>

<pre><code class="language-python">!pip install -U pandas-profiling</code></pre>
<pre><code class="language-python">import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
import sklearn
import pandas_profiling
import seaborn as sns
import random as rn
import os
import scipy.stats as stats

from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import OneHotEncoder
from sklearn.model_selection import train_test_split
from sklearn.ensemble import VotingClassifier
from sklearn import metrics

import xgboost as xgb
import lightgbm as lgb

from collections import Counter
import warnings

%matplotlib inline
warnings.filterwarnings(action=&#39;ignore&#39;)</code></pre>
<pre><code class="language-python">print(&quot;numpy version: {}&quot;. format(np.__version__))
print(&quot;pandas version: {}&quot;. format(pd.__version__))
print(&quot;matplotlib version: {}&quot;. format(matplotlib.__version__))
print(&quot;scikit-learn version: {}&quot;. format(sklearn.__version__))
print(&quot;xgboost version: {}&quot;. format(xgb.__version__))
print(&quot;lightgbm version: {}&quot;. format(lgb.__version__))</code></pre>
<pre>
numpy version: 1.21.6
pandas version: 1.3.5
matplotlib version: 3.2.2
scikit-learn version: 1.0.2
xgboost version: 0.90
lightgbm version: 2.2.3
</pre>

<pre><code class="language-python"># reproducibility
seed_num = 42   ####
np.random.seed(seed_num)
rn.seed(seed_num)
os.environ[&#39;PYTHONHASHSEED&#39;]=str(seed_num)</code></pre>
<h2 id="1-load-and-check-dataset"><strong>1. Load and Check Dataset</strong></h2>
<h3 id="variable-description"><strong>Variable Description</strong></h3>
<p>|age|workclass|fnlwgt|education|education.num|marital.status|occupation|
|나이|일 유형|CPS(Current Population Survey) 가중치|교육수준|교육수준 번호|결혼 상태|직업|</p>
<p>|relationship|race|sex|capital.gain|capital.loss|hours.per.week|native.country|
|가족관계|인종|성별|자본 이익|자본 손실|주당 근무시간|본 국적|</p>
<pre><code class="language-python">train = pd.read_csv(&#39;/content/drive/MyDrive/Forecasting_income/dataset/train.csv&#39;)
test = pd.read_csv(&#39;/content/drive/MyDrive/Forecasting_income/dataset/test.csv&#39;)

train.columns = train.columns.str.replace(&#39;.&#39;,&#39;_&#39;)
test.columns = test.columns.str.replace(&#39;.&#39;,&#39;_&#39;)

train.head()</code></pre>
<pre>
   id  age workclass  fnlwgt     education  education_num      marital_status  \
0   0   32   Private  309513    Assoc-acdm             12  Married-civ-spouse   
1   1   33   Private  205469  Some-college             10  Married-civ-spouse   
2   2   46   Private  149949  Some-college             10  Married-civ-spouse   
3   3   23   Private  193090     Bachelors             13       Never-married   
4   4   55   Private   60193       HS-grad              9            Divorced   

        occupation   relationship   race     sex  capital_gain  capital_loss  \
0     Craft-repair        Husband  White    Male             0             0   
1  Exec-managerial        Husband  White    Male             0             0   
2     Craft-repair        Husband  White    Male             0             0   
3     Adm-clerical      Own-child  White  Female             0             0   
4     Adm-clerical  Not-in-family  White  Female             0             0   

   hours_per_week native_country  target  
0              40  United-States       0  
1              40  United-States       1  
2              40  United-States       0  
3              30  United-States       0  
4              40  United-States       0  
</pre>

<pre><code class="language-python">pr=train.profile_report()
pr.to_file(&#39;/content/drive/MyDrive/Forecasting_income/pr_report.html&#39;)</code></pre>
<p>📝 Pandas profiling을 활용하면 아래와 같이 데이터 프레임을 쉽고 효율적으로 탐색할 수 있습니다. </p>
<p><img src="https://velog.velcdn.com/images/standing-o/post/79f6d779-da05-4306-a9c6-c86f2e21db57/image.png" alt="">
<img src="https://velog.velcdn.com/images/standing-o/post/bd080362-b669-44e8-8772-06e68c34bc9a/image.png" alt=""></p>
<h3 id="pandas-profiling-report의-alert-활용하기"><strong>Pandas profiling report의 Alert 활용하기</strong></h3>
<h4 id="high-correlation"><strong>High Correlation</strong></h4>
<ol>
<li><p><code>relationship</code> - <code>sex</code> </p>
</li>
<li><p><code>age</code> - <code>marital.status</code></p>
</li>
<li><p><code>workclass</code> - <code>occupation</code></p>
</li>
<li><p><code>education</code> - <code>education.num</code></p>
</li>
<li><p><code>relationship</code> -  <code>marital.status</code></p>
</li>
<li><p><code>race</code> - <code>native.country</code></p>
</li>
<li><p><code>sex</code> - <code>occupation</code></p>
</li>
<li><p><code>target</code> - <code>relationship</code></p>
</li>
</ol>
<h4 id="data-type"><strong>Data Type</strong></h4>
<ul>
<li><p>Numeric (7) : <code>id</code>, <code>age</code>, <code>fnlwgt</code>, <code>education.num</code>, <code>capital.gain</code>, <code>capital.loss</code>, <code>hours.per.week</code></p>
</li>
<li><p>Categorical (9) : <code>workclass</code>, <code>education</code>, <code>marital.status</code>, <code>occupation</code>, <code>relationship</code>, <code>race</code>, <code>sex</code>, <code>native.country</code>, <code>target</code></p>
</li>
</ul>
<h4 id="note"><strong>Note</strong></h4>
<p>📝 <code>workclass</code>와 <code>occupation</code>이 같은 비율 (10.5%)의 missing value를 가지므로 확인해 볼 필요가 있습니다.  </p>
<p>📝 또한, <code>native.country</code>는 583(3.3%) missing value를 가지므로 행을 삭제해주겠습니다.</p>
<p>📝 <code>capital.gain</code>, <code>capital.loss</code>가 high skewness를 가집니다. outlier를 확인하고 필요시 transformation을 진행하겠습니다.</p>
<pre><code class="language-python">train.info()</code></pre>
<pre>
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 17480 entries, 0 to 17479
Data columns (total 16 columns):
 #   Column          Non-Null Count  Dtype 
---  ------          --------------  ----- 
 0   id              17480 non-null  int64 
 1   age             17480 non-null  int64 
 2   workclass       15644 non-null  object
 3   fnlwgt          17480 non-null  int64 
 4   education       17480 non-null  object
 5   education_num   17480 non-null  int64 
 6   marital_status  17480 non-null  object
 7   occupation      15637 non-null  object
 8   relationship    17480 non-null  object
 9   race            17480 non-null  object
 10  sex             17480 non-null  object
 11  capital_gain    17480 non-null  int64 
 12  capital_loss    17480 non-null  int64 
 13  hours_per_week  17480 non-null  int64 
 14  native_country  16897 non-null  object
 15  target          17480 non-null  int64 
dtypes: int64(8), object(8)
memory usage: 2.1+ MB
</pre>


<h2 id="2-data-preprocessing"><strong>2. Data Preprocessing</strong></h2>
<h3 id="1-missing-value"><strong>(1) Missing Value</strong></h3>
<pre><code class="language-python">train.columns[train.isnull().any()]</code></pre>
<pre>
Index(['workclass', 'occupation', 'native_country'], dtype='object')
</pre>

<pre><code class="language-python">train[train[&quot;workclass&quot;].isnull()]</code></pre>
<pre>
          id  age workclass  fnlwgt     education  education_num  \
15081  15081   90       NaN   77053       HS-grad              9   
15082  15082   66       NaN  186061  Some-college             10   
15084  15084   51       NaN  172175     Doctorate             16   
15086  15086   61       NaN  135285       HS-grad              9   
15087  15087   71       NaN  100820       HS-grad              9   
...      ...  ...       ...     ...           ...            ...   
17475  17475   35       NaN  320084     Bachelors             13   
17476  17476   30       NaN   33811     Bachelors             13   
17477  17477   71       NaN  287372     Doctorate             16   
17478  17478   41       NaN  202822       HS-grad              9   
17479  17479   72       NaN  129912       HS-grad              9   

           marital_status occupation   relationship                race  \
15081             Widowed        NaN  Not-in-family               White   
15082             Widowed        NaN      Unmarried               Black   
15084       Never-married        NaN  Not-in-family               White   
15086  Married-civ-spouse        NaN        Husband               White   
15087  Married-civ-spouse        NaN        Husband               White   
...                   ...        ...            ...                 ...   
17475  Married-civ-spouse        NaN           Wife               White   
17476       Never-married        NaN  Not-in-family  Asian-Pac-Islander   
17477  Married-civ-spouse        NaN        Husband               White   
17478           Separated        NaN  Not-in-family               Black   
17479  Married-civ-spouse        NaN        Husband               White   

          sex  capital_gain  capital_loss  hours_per_week native_country  \
15081  Female             0          4356              40  United-States   
15082  Female             0          4356              40  United-States   
15084    Male             0          2824              40  United-States   
15086    Male             0          2603              32  United-States   
15087    Male             0          2489              15  United-States   
...       ...           ...           ...             ...            ...   
17475  Female             0             0              55  United-States   
17476  Female             0             0              99  United-States   
17477    Male             0             0              10  United-States   
17478  Female             0             0              32  United-States   
17479    Male             0             0              25  United-States   

       target  
15081       0  
15082       0  
15084       1  
15086       0  
15087       0  
...       ...  
17475       1  
17476       0  
17477       1  
17478       0  
17479       0  

[1836 rows x 16 columns]
</pre>

<pre><code class="language-python">train[&#39;workclass&#39;].unique()</code></pre>
<pre>
array(['Private', 'State-gov', 'Local-gov', 'Self-emp-not-inc',
       'Self-emp-inc', 'Federal-gov', 'Without-pay', nan, 'Never-worked'],
      dtype=object)
</pre>
<p>📝 <code>workclass</code>, <code>occupation</code> 컬럼의 결측치를 포함한 행은 삭제합니다.</p>
<p>📝 두 컬럼이 동시에 결측치를 갖는 경우가 대부분이었기에 <code>workclass</code>의 결측치만 &#39;Never-worked&#39;와 같은 이미 존재하는 특성으로 채우는것은 의미가 없습니다.</p>
<p>📝 <code>workclass</code>와 <code>occupation</code>에 새 feature을 생성하여 넣는 방법도 시도했지만, one-hot encoding을 해서 생기는 test 데이터와의 컬럼 차이 때문에 다른 방법을 고려해볼 필요가 있다고 생각합니다. 😔😔</p>
<pre><code class="language-python">print(sum(train[&#39;workclass&#39;].isna()))
print(sum(train[&#39;occupation&#39;].isna()))

fill_na = train[&#39;workclass&#39;].isna()</code></pre>
<pre>
1836
1843
</pre>

<pre><code class="language-python">df_train = train.dropna()  

print(sum(df_train[&#39;workclass&#39;].isna()))
print(sum(df_train[&#39;occupation&#39;].isna()))
print(sum(df_train[&#39;native_country&#39;].isna()))</code></pre>
<pre>
0
0
0
</pre>

<pre><code class="language-python">df_train</code></pre>
<pre>
          id  age     workclass  fnlwgt     education  education_num  \
0          0   32       Private  309513    Assoc-acdm             12   
1          1   33       Private  205469  Some-college             10   
2          2   46       Private  149949  Some-college             10   
3          3   23       Private  193090     Bachelors             13   
4          4   55       Private   60193       HS-grad              9   
...      ...  ...           ...     ...           ...            ...   
15076  15076   35       Private  337286       Masters             14   
15077  15077   36       Private  182074  Some-college             10   
15078  15078   50  Self-emp-inc  175070   Prof-school             15   
15079  15079   39       Private  202937  Some-college             10   
15080  15080   33       Private   96245    Assoc-acdm             12   

           marital_status       occupation   relationship                race  \
0      Married-civ-spouse     Craft-repair        Husband               White   
1      Married-civ-spouse  Exec-managerial        Husband               White   
2      Married-civ-spouse     Craft-repair        Husband               White   
3           Never-married     Adm-clerical      Own-child               White   
4                Divorced     Adm-clerical  Not-in-family               White   
...                   ...              ...            ...                 ...   
15076       Never-married  Exec-managerial  Not-in-family  Asian-Pac-Islander   
15077            Divorced     Adm-clerical  Not-in-family               White   
15078  Married-civ-spouse   Prof-specialty        Husband               White   
15079            Divorced     Tech-support  Not-in-family               White   
15080  Married-civ-spouse   Prof-specialty        Husband               White   

          sex  capital_gain  capital_loss  hours_per_week native_country  \
0        Male             0             0              40  United-States   
1        Male             0             0              40  United-States   
2        Male             0             0              40  United-States   
3      Female             0             0              30  United-States   
4      Female             0             0              40  United-States   
...       ...           ...           ...             ...            ...   
15076    Male             0             0              40  United-States   
15077    Male             0             0              45  United-States   
15078    Male             0             0              45  United-States   
15079  Female             0             0              40         Poland   
15080    Male             0             0              50  United-States   

       target  
0           0  
1           1  
2           0  
3           0  
4           0  
...       ...  
15076       0  
15077       0  
15078       1  
15079       0  
15080       0  

[15081 rows x 16 columns]
</pre>

<h3 id="2-outlier"><strong>(2) Outlier</strong></h3>
<pre><code class="language-python">fig, ax = plt.subplots(1, 2, figsize=(12,3))
g = sns.distplot(df_train[&#39;capital_gain&#39;], color=&#39;b&#39;, label=&#39;Skewness : {:.2f}&#39;.format(df_train[&#39;capital_gain&#39;].skew()), ax=ax[0])
g = g.legend(loc=&#39;best&#39;)

g = sns.distplot(df_train[&#39;capital_loss&#39;], color=&#39;b&#39;, label=&#39;Skewness : {:.2f}&#39;.format(df_train[&#39;capital_loss&#39;].skew()), ax=ax[1])
g = g.legend(loc=&#39;best&#39;)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/0a711c68-7a69-44f1-a415-f664193adb95/image.png" alt=""></p>
<pre><code class="language-python">numeric_fts = [&#39;age&#39;, &#39;fnlwgt&#39;, &#39;education_num&#39;, &#39;capital_gain&#39;, &#39;capital_loss&#39;, &#39;hours_per_week&#39;]

outlier_ind = []
for i in numeric_fts:
  Q1 = np.percentile(df_train[i],25)
  Q3 = np.percentile(df_train[i],75)
  IQR = Q3-Q1
  outlier_list = df_train[(df_train[i] &lt; Q1 - IQR * 1.5) | (df_train[i] &gt; Q3 + IQR * 1.5)].index
  outlier_ind.extend(outlier_list)</code></pre>
<pre><code class="language-python">outlier_ind = Counter(outlier_ind)
multi_outliers = list(k for k,j in outlier_ind.items() if j &gt; 2)</code></pre>
<pre><code class="language-python"># Drop outliers
train_df = df_train.drop(multi_outliers, axis = 0).reset_index(drop = True)
train_df</code></pre>
<pre>
          id  age     workclass  fnlwgt     education  education_num  \
0          0   32       Private  309513    Assoc-acdm             12   
1          1   33       Private  205469  Some-college             10   
2          2   46       Private  149949  Some-college             10   
3          3   23       Private  193090     Bachelors             13   
4          4   55       Private   60193       HS-grad              9   
...      ...  ...           ...     ...           ...            ...   
15043  15076   35       Private  337286       Masters             14   
15044  15077   36       Private  182074  Some-college             10   
15045  15078   50  Self-emp-inc  175070   Prof-school             15   
15046  15079   39       Private  202937  Some-college             10   
15047  15080   33       Private   96245    Assoc-acdm             12   

           marital_status       occupation   relationship                race  \
0      Married-civ-spouse     Craft-repair        Husband               White   
1      Married-civ-spouse  Exec-managerial        Husband               White   
2      Married-civ-spouse     Craft-repair        Husband               White   
3           Never-married     Adm-clerical      Own-child               White   
4                Divorced     Adm-clerical  Not-in-family               White   
...                   ...              ...            ...                 ...   
15043       Never-married  Exec-managerial  Not-in-family  Asian-Pac-Islander   
15044            Divorced     Adm-clerical  Not-in-family               White   
15045  Married-civ-spouse   Prof-specialty        Husband               White   
15046            Divorced     Tech-support  Not-in-family               White   
15047  Married-civ-spouse   Prof-specialty        Husband               White   

          sex  capital_gain  capital_loss  hours_per_week native_country  \
0        Male             0             0              40  United-States   
1        Male             0             0              40  United-States   
2        Male             0             0              40  United-States   
3      Female             0             0              30  United-States   
4      Female             0             0              40  United-States   
...       ...           ...           ...             ...            ...   
15043    Male             0             0              40  United-States   
15044    Male             0             0              45  United-States   
15045    Male             0             0              45  United-States   
15046  Female             0             0              40         Poland   
15047    Male             0             0              50  United-States   

       target  
0           0  
1           1  
2           0  
3           0  
4           0  
...       ...  
15043       0  
15044       0  
15045       1  
15046       0  
15047       0  

[15048 rows x 16 columns]
</pre>

<pre><code class="language-python">print(train_df[&#39;capital_gain&#39;].skew(), train_df[&#39;capital_loss&#39;].skew())</code></pre>
<pre>
12.004940559585881 4.607122286739042
</pre>
<p>📝 Outlier들을 제거했음에도 두 변수는 여전히 high skewness를 가지고 있으므로 log transformation을 진행해보고자 합니다.</p>
<pre><code class="language-python"># log transformation
train_df[&#39;capital_gain&#39;] = train_df[&#39;capital_gain&#39;].map(lambda i: np.log(i) if i &gt; 0 else 0)
test[&#39;capital_gain&#39;] = test[&#39;capital_gain&#39;].map(lambda i: np.log(i) if i &gt; 0 else 0)

train_df[&#39;capital_loss&#39;] = train_df[&#39;capital_loss&#39;].map(lambda i: np.log(i) if i &gt; 0 else 0)
test[&#39;capital_loss&#39;] = test[&#39;capital_loss&#39;].map(lambda i: np.log(i) if i &gt; 0 else 0)</code></pre>
<pre><code class="language-python">print(train_df[&#39;capital_gain&#39;].skew(), train_df[&#39;capital_loss&#39;].skew())</code></pre>
<pre>
3.0945787119106676 4.390015583095806
</pre>

<h2 id="3-feature-engineering"><strong>3. Feature Engineering</strong></h2>
<h4 id="1-correlation"><strong>(1) Correlation</strong></h4>
<p>📝 Categorical 데이터를 라벨인코더를 통해 수치형으로 변환한 후 상관관계를 확인합니다.</p>
<p>📝 Categorical : <code>workclass</code>, <code>education</code>, <code>marital.status</code>, <code>occupation</code>, <code>relationship</code>, <code>race</code>, <code>sex</code>, <code>native.country</code></p>
<pre><code class="language-python">la_train = train_df.copy()

cat_fts = [&#39;workclass&#39;, &#39;education&#39;, &#39;marital_status&#39;, &#39;occupation&#39;, &#39;relationship&#39;, &#39;race&#39;, &#39;sex&#39;, &#39;native_country&#39;]
for i in range(len(cat_fts)):
  encoder = LabelEncoder()
  la_train[cat_fts[i]] = encoder.fit_transform(la_train[cat_fts[i]])</code></pre>
<pre><code class="language-python">la_train.head()</code></pre>
<pre>
   id  age  workclass  fnlwgt  education  education_num  marital_status  \
0   0   32          2  309513          7             12               2   
1   1   33          2  205469         15             10               2   
2   2   46          2  149949         15             10               2   
3   3   23          2  193090          9             13               4   
4   4   55          2   60193         11              9               0   

   occupation  relationship  race  sex  capital_gain  capital_loss  \
0           2             0     4    1           0.0           0.0   
1           3             0     4    1           0.0           0.0   
2           2             0     4    1           0.0           0.0   
3           0             3     4    0           0.0           0.0   
4           0             1     4    0           0.0           0.0   

   hours_per_week  native_country  target  
0              40              38       0  
1              40              38       1  
2              40              38       0  
3              30              38       0  
4              40              38       0  
</pre>

<p>📝 앞서 수행한 pandas profiling report의 alert를 참고하여 상관계수를 계산했습니다.</p>
<p>📝 꽤 유의미한 상관관계를 가지고 있다고 생각되는것은 <code>relationship</code>-<code>sex</code>, <code>occupation</code>-<code>workclass</code>, <code>education</code>-<code>education.num</code> 입니다.</p>
<pre><code class="language-python"># Pearson
la_train[[&#39;age&#39;,&#39;marital_status&#39;, &#39;relationship&#39;, &#39;sex&#39;, &#39;occupation&#39;, &#39;workclass&#39;]].corr()</code></pre>
<pre>
                     age  marital_status  relationship       sex  occupation  \
age             1.000000       -0.271955     -0.240331  0.087515   -0.007994   
marital_status -0.271955        1.000000      0.180281 -0.124481    0.023856   
relationship   -0.240331        0.180281      1.000000 -0.590077   -0.052109   
sex             0.087515       -0.124481     -0.590077  1.000000    0.061443   
occupation     -0.007994        0.023856     -0.052109  0.061443    1.000000   
workclass       0.081100       -0.044000     -0.070512  0.078764    0.010194   

                workclass  
age              0.081100  
marital_status  -0.044000  
relationship    -0.070512  
sex              0.078764  
occupation       0.010194  
workclass        1.000000  
</pre>

<pre><code class="language-python">la_train[[&#39;education&#39;, &#39;education_num&#39;, &#39;race&#39;, &#39;native_country&#39;]].corr()</code></pre>
<pre>
                education  education_num      race  native_country
education        1.000000       0.348614  0.011236        0.079063
education_num    0.348614       1.000000  0.034686        0.097485
race             0.011236       0.034686  1.000000        0.126654
native_country   0.079063       0.097485  0.126654        1.000000
</pre>
<p>📝 Categorical 인 두 변수의 경우는 Cramer&#39;s V 공식을 활용하여 상관관계를 확인했습니다.</p>
<pre><code class="language-python">stat = stats.chi2_contingency(la_train[[&#39;race&#39;, &#39;native_country&#39;]].values, correction=False)[0]
obs = np.sum(la_train[[&#39;race&#39;, &#39;native_country&#39;]].values) 
mini = min(la_train[[&#39;race&#39;, &#39;native_country&#39;]].values.shape)-1 

# Cramer&#39;s V 
V = np.sqrt((stat/(obs*mini)))
print(V)</code></pre>
<pre>
0.11306993147326666
</pre>
<h4 id="2-string-to-numerical"><strong>(2) String to numerical</strong></h4>
<p>📝 Categorical 데이터를 모델에 넣기 위해서는 수치화 시킬 필요가 있습니다. LabelEncoder는 불필요한 상관관계를 만들 가능성이 있기에 OnehotEncoder를 사용했습니다.</p>
<p>📝 Categorical : <code>workclass</code>, <code>education</code>, <code>marital.status</code>, <code>occupation</code>, <code>relationship</code>, <code>race</code>, <code>sex</code>, <code>native.country</code></p>
<pre><code class="language-python">train_dataset = train_df.copy()
test_dataset = test.copy()</code></pre>
<p>📝 get_dummies를 사용하여 one-hot encoding을 진행했습니다.</p>
<pre><code class="language-python">train_dataset = pd.get_dummies(train_dataset)
test_dataset = pd.get_dummies(test_dataset)

print(train_dataset.columns)
print(test_dataset.columns)</code></pre>
<pre>
Index(['id', 'age', 'fnlwgt', 'education_num', 'capital_gain', 'capital_loss',
       'hours_per_week', 'target', 'workclass_Federal-gov',
       'workclass_Local-gov',
       ...
       'native_country_Portugal', 'native_country_Puerto-Rico',
       'native_country_Scotland', 'native_country_South',
       'native_country_Taiwan', 'native_country_Thailand',
       'native_country_Trinadad&Tobago', 'native_country_United-States',
       'native_country_Vietnam', 'native_country_Yugoslavia'],
      dtype='object', length=106)
Index(['id', 'age', 'fnlwgt', 'education_num', 'capital_gain', 'capital_loss',
       'hours_per_week', 'workclass_Federal-gov', 'workclass_Local-gov',
       'workclass_Private',
       ...
       'native_country_Portugal', 'native_country_Puerto-Rico',
       'native_country_Scotland', 'native_country_South',
       'native_country_Taiwan', 'native_country_Thailand',
       'native_country_Trinadad&Tobago', 'native_country_United-States',
       'native_country_Vietnam', 'native_country_Yugoslavia'],
      dtype='object', length=104)
</pre>
<p>📝 train과 test의 열길이를 맞춰주는 작업을 합니다.</p>
<pre><code class="language-python">test_col = []
add_test = []

for i in test_dataset.columns:
    test_col.append(i)
for j in train_dataset.columns:
    if j not in test_col:
        add_test.append(j)
add_test.remove(&#39;target&#39;)</code></pre>
<p>📝 test 데이터의 <code>native.country</code> 컬럼에는 &#39;Holand-Netherlands&#39; 특성이 없는걸까요?</p>
<pre><code class="language-python">print(add_test)</code></pre>
<pre>
['native_country_Holand-Netherlands']
</pre>

<pre><code class="language-python">for d in add_test:
    test_dataset[d] = 0</code></pre>
<pre><code class="language-python">print(train_dataset.columns)
print(test_dataset.columns)</code></pre>
<pre>
Index(['id', 'age', 'fnlwgt', 'education_num', 'capital_gain', 'capital_loss',
       'hours_per_week', 'target', 'workclass_Federal-gov',
       'workclass_Local-gov',
       ...
       'native_country_Portugal', 'native_country_Puerto-Rico',
       'native_country_Scotland', 'native_country_South',
       'native_country_Taiwan', 'native_country_Thailand',
       'native_country_Trinadad&Tobago', 'native_country_United-States',
       'native_country_Vietnam', 'native_country_Yugoslavia'],
      dtype='object', length=106)
Index(['id', 'age', 'fnlwgt', 'education_num', 'capital_gain', 'capital_loss',
       'hours_per_week', 'workclass_Federal-gov', 'workclass_Local-gov',
       'workclass_Private',
       ...
       'native_country_Puerto-Rico', 'native_country_Scotland',
       'native_country_South', 'native_country_Taiwan',
       'native_country_Thailand', 'native_country_Trinadad&Tobago',
       'native_country_United-States', 'native_country_Vietnam',
       'native_country_Yugoslavia', 'native_country_Holand-Netherlands'],
      dtype='object', length=105)
</pre>

<p>📝 Train의 target column을 제외하고 보면 열길이가 잘 맞춰진것을 확인할 수 있습니다.</p>
<h2 id="4-modeling"><strong>4. Modeling</strong></h2>
<p>📝 먼저, train과 validation 데이터를 train_test_split 함수를 사용하여 나눠줍니다.</p>
<pre><code class="language-python">test_size =0.15

train_data, val_data = train_test_split(train_dataset, test_size = test_size, random_state = seed_num)

drop_col = [&#39;target&#39;, &#39;id&#39;]

train_x = train_data.drop(drop_col, axis = 1)
train_y = pd.DataFrame(train_data[&#39;target&#39;])

val_x = val_data.drop(drop_col, axis = 1)
val_y = pd.DataFrame(val_data[&#39;target&#39;])</code></pre>
<pre><code class="language-python">print(train_x.shape, train_y.shape)
print(val_x.shape, val_y.shape)</code></pre>
<pre>
(12790, 104) (12790, 1)
(2258, 104) (2258, 1)
</pre>

<p>📝 LGBM과 XGboost를 Soft Voting하여 간단한 ensemble 모델을 제작했습니다.</p>
<p>📝 Soft Voting은 LGBM, XGboost 모델의 예측 확률을 평균하여 최종 class를 결정합니다.</p>
<pre><code class="language-python">LGBClassifier = lgb.LGBMClassifier(random_state = seed_num)</code></pre>
<pre><code class="language-python">lgbm = LGBClassifier.fit(train_x.values,
                       train_y.values.ravel(),
                       eval_set = [(train_x.values, train_y), (val_x.values, val_y)], 
                       eval_metric =&#39;auc&#39;, early_stopping_rounds = 1000,
                       verbose = True)</code></pre>
<pre><code class="language-python">XGBClassifier = xgb.XGBClassifier(max_depth = 6, learning_rate = 0.01, n_estimators = 10000, random_state = seed_num)</code></pre>
<pre><code class="language-python">xgb = XGBClassifier.fit(train_x.values,
                       train_y.values.ravel(),
                       eval_set = [(train_x.values, train_y), (val_x.values, val_y)], 
                       eval_metric = &#39;auc&#39;, early_stopping_rounds = 1000,
                       verbose = True)</code></pre>
<pre><code class="language-python">voting = VotingClassifier(estimators=[(&#39;xgb&#39;, xgb),(&#39;lgbm&#39;, lgbm)], voting=&#39;soft&#39;)
vot = voting.fit(train_x.values, train_y.values)</code></pre>
<h2 id="5-evaluation--submission"><strong>5. Evaluation &amp; Submission</strong></h2>
<pre><code class="language-python">l_val_y_pred = lgbm.predict(val_x.values)
x_val_y_pred = xgb.predict(val_x.values)
v_val_y_pred = vot.predict(val_x.values)</code></pre>
<pre><code class="language-python">print(metrics.accuracy_score(l_val_y_pred, val_y))
print(metrics.accuracy_score(x_val_y_pred, val_y))
print(metrics.accuracy_score(v_val_y_pred, val_y))</code></pre>
<pre>
0.8702391496899912
0.8680248007085917
0.8596102745792737
</pre>

<pre><code class="language-python">print(metrics.classification_report(v_val_y_pred, val_y))</code></pre>
<pre>
              precision    recall  f1-score   support

           0       0.93      0.89      0.91      1800
           1       0.63      0.72      0.68       458

    accuracy                           0.86      2258
   macro avg       0.78      0.81      0.79      2258
weighted avg       0.87      0.86      0.86      2258

</pre>

<pre><code class="language-python">val_xgb = pd.Series(l_val_y_pred, name=&quot;XGB&quot;)
val_lgbm = pd.Series(x_val_y_pred, name=&quot;LGBM&quot;)</code></pre>
<pre><code class="language-python">ensemble_results = pd.concat([val_xgb,val_lgbm],axis=1)
sns.heatmap(ensemble_results.corr(), annot=True)
plt.show()</code></pre>
<p><img src="https://velog.velcdn.com/images/standing-o/post/a842ff5c-d213-4f6c-9b48-6ba56a1e13cc/image.png" alt=""></p>
<p>📝 Soft Voting을 진행했음에도 성능이 향상되지 않았습니다. </p>
<p>📝 두 모델의 예측은 높은 상관관계를 가지고 있었기에 앙상블 이전보다 성능이 향상되지 않았다고 해석할 수 있습니다.  </p>
<p>(조금 더 공부가 필요할것 같습니다 😂😂)</p>
<hr>
<p>감사합니다 :)
도움이 됐길 바랍니다👍👍</p>
]]></description>
        </item>
    </channel>
</rss>