Home [SeSAC]파이썬 라이브러리를 활용한 데이터분석-2
Post
Cancel

[SeSAC]파이썬 라이브러리를 활용한 데이터분석-2

Intro:

Chapter 4. 넘파이 기본: 배열과 벡터 연산

Chapter 5. Pandas

기초 개념

1
2
3
4
5
6
7
8
import numpy as np
import pandas as pd
logx = np.logspace(0, 1, 100)
linx = np.linspace(0, 10, 100)

df = pd.DataFrame()
df['logspace'] = logx  #컬럼명 logspace의 Series 데이터
df['linspace'] = linx  # 컬럼명 linespace의 Series 데이터

Column명 변경

DataFrame의 컬럼명 Rename 3가지 방법

  • pd.DataFrame.columns = [ '칼럼1', '칼럼2', … , ]
  • pd.DataFrame.rename( { 'OLD칼럼명' : 'NEW칼럼명', …, }, axis=1)
  • pd.DataFrame.rename(columns={ 'OLD칼럼명' : 'NEW칼럼명', …, })

###

몬티홀 딜레마

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
# 그림 그리는 환경
import random

def init():
    doors = ['A', 'B', 'C']
    choice = random.choice(doors)
    prize = random.choice(doors)
    return choice, prize

def MC(mychoice, prize):
    candidates = ["A", "B", "C"]
    
    if mychoice == prize:
        candidates.remove(prize)
    else:
        candidates = [x for x in candidates if x != mychoice and x != prize]
    return random.choice(candidates)

def no_change():
    choice, prize = init()
    opened_door = MC(choice, prize)
    return 1 if choice == prize else 0

def change():
    choice, prize = init()
    opened_door = MC(choice, prize)
    return 0 if choice == prize else 1


from tqdm.auto import tqdm, trange
tries = 1000
result_change = []
result_nochange = []
for _ in tqdm(range(tries), position=0):
    win_nochange = 0
    win_change = 0
    for _ in range(tries):
        win_nochange += no_change()
        win_change += change()
    result_nochange.append(win_nochange)
    result_change.append(win_change)
plt.figure(figsize=(20, 20))
plt.plot(result_change, 'o:', label='Change')
plt.plot(result_nochange, 'o:', label='No Change')
plt.legend()
plt.title('Monty Hall Dilemma')
plt.xlabel('Trial')
plt.ylabel('? / 100')

This post is licensed under CC BY 4.0 by the author.

[SeSAC]데이터분석 관련 영상 시청

[SeSAC]파이썬 라이브러리를 활용한 데이터분석-3