๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

์ „์ฒด ๊ธ€

(80)
apply, sapply, lapply ์‚ฌ์šฉ๋ฒ• apply, sapply, lapply ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ๋ฒ•์„ ์ดํ•ดํ•˜๊ณ  ์ ์šฉํ•˜์—ฌ ๋ณด์ž. In: library(dplyr) df_iris = iris df_iris_num = iris %>% select(-Species) str(df_iris_num) Out: 'data.frame':150 obs. of 4 variables: $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ... $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ... $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ... $ Petal.Width : num 0.2 0.2 0.2 0..
Selenium๊ณผ BeautifulSoup๋ฅผ ํ™œ์šฉํ•œ ํฌ๋กค๋ง BeautifulSoup์™€ Selenium์„ ์ด์šฉํ•˜์—ฌ ํ”ผํŒŒ ์˜จ๋ผ์ธ์˜ ๋ฐ์ดํ„ฐ ์„ผํ„ฐ์—์„œ 5์›”๋ถ€ํ„ฐ 10์›”๊นŒ์ง€์˜ ํฌ์ง€์…˜๋ณ„ ์„ ์ˆ˜์˜ ์ด์šฉ์ž ์ˆ˜ ๋ฐ์ดํ„ฐ๋ฅผ ํฌ๋กค๋ง ํ•œ ํ›„, ํ”ผํŒŒ ์˜จ๋ผ์ธ์˜ ํฌ์ง€์…˜๋ณ„ ์„ ์ˆ˜์˜ ์„ ํ˜ธ๋„๋ฅผ ํ™•์ธํ•  ๊ฒƒ์ด๋‹ค. ์ •์  ํฌ๋กค๋ง๋งŒ์œผ๋กœ ํ•ด๋‹น ์‚ฌ์ดํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ •์  ํฌ๋กค๋ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” BeautifulSoup์™€ ๋™์  ํฌ๋กค๋ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” Selenium์„ ํ•จ๊ป˜ ํ™œ์šฉํ•˜์—ฌ ํฌ๋กค๋ง์„ ์ˆ˜ํ–‰ํ•˜์˜€๋‹ค. ์ˆ˜ํ–‰ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1. ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๊ณผ์ • 2. ํฌ๋กค๋ง 3. ์‹œ๊ฐํ™” 1. ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ ๊ณผ์ • ํฌ์ง€์…˜๋ณ„ ์„ ์ˆ˜์˜ ์ด์šฉ์ž ์ˆ˜๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ”ผํŒŒ ์˜จ๋ผ์ธ์˜ ๋ฐ์ดํ„ฐ ์„ผํ„ฐ(fifaonline4.nexon.com/datacenter/dailysquad)์— ์ ‘์†ํ•œ ํ›„, ๋‹ค์Œ ๊ณผ์ •์„ ํ†ตํ•ด ์ˆ˜ํ–‰ํ•˜์—ฌ์•ผ ํ•œ๋‹ค. (1)..
BeautifulSoup๋ฅผ ํ™œ์šฉํ•œ ํฌ๋กค๋ง โ–ก ํฌ๋กค๋ง์˜ ์ˆœ์„œ (1) ์›ํ•˜๋Š” ํŽ˜์ด์ง€์˜ HTML ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ธฐ (2) HTML์„ ๋ถ„์„(ํŒŒ์‹ฑ) ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ๋กœ ๊ฐ€๊ณต (3) ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœ In: import urllib.request # ์›นํŽ˜์ด์ง€ ์ ‘์† req = urllib.request.Request('https://naver.com') # ์›น์„œ๋ฒ„๋ฅผ Requestํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ res = urllib.request.urlopen(req) # Reqeustํ•œ ๊ฐ์ฒด์— ๋Œ€ํ•œ urlopen ํ•จ์ˆ˜์˜ ์ˆ˜ํ–‰๊ฒฐ๊ณผ๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅ html_byte = res.read() # read ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฐ”์ดํŠธ ํ˜•ํƒœ๋กœ ์ €์žฅ html_str = html_byte.decode('UTF8') # ๋ฌธ์ž์—ด๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด UTF-8๋กœ ๋””์ฝ”๋”ฉ ์ˆ˜ํ–‰ html_splt = html..
[Level 2] ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜• ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜• ์ฐพ๊ธฐ [[0,1,1,1],[1,1,1,1],[1,1,1,1],[0,0,1,0]] 9 programmers.co.kr def solution(b): len_x = len(b) len_y = len(b[0]) for x in range(1, len_x): for y in range(1, len_y): if b[x][y] == 1: b[x][y] = min(b[x][y-1], b[x-1][y], b[x-1][y-1])+1 return(max([i for x in b for i in x])**2) โ–ท ์ด ๋ฌธ์ œ์˜ ๊ฐ€์žฅ ํ•ต์‹ฌ์€ DP(Dyanamic Programming)์„ ์ด์šฉํ•˜์—ฌ ํ‘ธ๋Š” ๊ฒƒ์ด๋‹ค. DP๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ, ์‹œ๊ฐ„์ด ์ดˆ๊ณผ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ •์‚ฌ๊ฐํ˜•์˜ ๋ชจ..
LSTM(Long Short-Term Memories model) ๊ตฌํ˜„ ํŒŒ์ดํ† ์น˜๋ฅผ ์ด์šฉํ•˜์—ฌ LSTM(Long Short-Term Memories model)์„ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๋‹ค. ์‚ฌ์šฉ๋  ๋ฐ์ดํ„ฐ๋Š” ์•„๋งˆ์กด์˜ ์ฃผ๊ฐ€๋กœ ์ข…๊ฐ€์™€ ๊ฑฐ๋ž˜๋Ÿ‰์„ ์ด์šฉํ•˜์—ฌ, ์ผ์ฃผ์ผ ๋’ค์˜ ์ข…๊ฐ€๋ฅผ ์˜ˆ์ธกํ•˜๋Š” ๊ฒƒ์ด ๋ชฉ์ ์ด๋‹ค. ๋ฐ์ดํ„ฐ๋Š” ์—ฌ๊ธฐ(www.kaggle.com/camnugent/sandp500)์—์„œ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค. ๋ฐ์ดํ„ฐ๊ตฌํ˜„ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1. ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ 2. ๋ชจ๋ธ ์„ค์ • 3. ๋ชจ๋ธ ํ•™์Šต 4. ํ•™์Šต ๊ฒฐ๊ณผ 1. ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ In: import pandas as pd from numpy import array from numpy import hstack import matplotlib.pyplot as plt import torch def min_max_scaler(arr): min_arr = min(arr)..
๋งค๊ฐœํšจ๊ณผ(Mediating effect) โ–ก ๋งค๊ฐœํšจ๊ณผ(Mediating effect) - ๋…๋ฆฝ๋ณ€์ˆ˜๊ฐ€ ์ข…์†๋ณ€์ˆ˜์— ์œ ์˜ํ•œ ์˜ํ–ฅ์„ ๋ฏธ์น  ๋•Œ, ๊ทธ ์‚ฌ์ด์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๊ทธ ์˜ํ–ฅ์ด ์ „๋‹ฌ๋˜๋Š” ๊ฒฝ์šฐ - X(๋…๋ฆฝ๋ณ€์ˆ˜) → M(๋งค๊ฐœ๋ณ€์ˆ˜) → Y(์ข…์†๋ณ€์ˆ˜) โ–ก ๋งค๊ฐœํšจ๊ณผ์˜ ์ข…๋ฅ˜ (1) ๋ถ€๋ถ„๋งค๊ฐœ ํšจ๊ณผ (2) ์™„์ „๋งค๊ฐœ ํšจ๊ณผ โ–ก ๋งค๊ฐœํšจ๊ณผ ๊ฒ€์ฆ๋ฐฉ๋ฒ• - Baron & Kenny (1986) ๋ฐฉ๋ฒ• [1๋‹จ๊ณ„] X → M [2๋‹จ๊ณ„] X → Y [3๋‹จ๊ณ„] X + M → Y (1) ๋ถ€๋ถ„๋งค๊ฐœ ํšจ๊ณผ ๊ฒ€์ฆ [1๋‹จ๊ณ„] M = a + b1*X → b1 ์œ ์˜ [2๋‹จ๊ณ„] Y = a + phi*X → phi ์œ ์˜ [3๋‹จ๊ณ„] Y = a + b2*X + b3*M → b3, b2 ์œ ์˜ โ–ท ์œ„์˜ 3๋‹จ๊ณ„์˜ ๊ฐ ๊ณ„์ˆ˜์˜ ์œ ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๊ฒฝ์šฐ, ๋ถ€๋ถ„๋งค๊ฐœํšจ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. (2) ์™„์ „๋งค๊ฐœ ํšจ๊ณผ..
[Level 2] ๋ฌธ์ž์—ด ์••์ถ• ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฌธ์ž์—ด ์••์ถ• ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ์ „๋ฌธ๊ฐ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์–ดํ”ผ์น˜๋Š” ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ๊ทผ์— ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•œ ๊ฐ„๋‹จํ•œ ๋น„์†์‹ค ์••์ถ• ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ๋ฌธ์ž programmers.co.kr def solution(s): len_s = len(s) cand = [] if len_s < 3: return len_s for u in range(1, int(len_s/2)+1): splt_s = [s[u*i:u*(i+1)] for i in range(int(len_s/u)+1)] comp_s = [] prev = splt_s.pop(0) cnt = 1 while splt_s: cur = splt_s.pop(0) if cur == prev: cnt +..
[Level 2] ๊ตฌ๋ช…๋ณดํŠธ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๊ตฌ๋ช…๋ณดํŠธ ๋ฌด์ธ๋„์— ๊ฐ‡ํžŒ ์‚ฌ๋žŒ๋“ค์„ ๊ตฌ๋ช…๋ณดํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌ์ถœํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๋ช…๋ณดํŠธ๋Š” ์ž‘์•„์„œ ํ•œ ๋ฒˆ์— ์ตœ๋Œ€ 2๋ช…์”ฉ ๋ฐ–์— ํƒˆ ์ˆ˜ ์—†๊ณ , ๋ฌด๊ฒŒ ์ œํ•œ๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์‚ฌ๋žŒ๋“ค์˜ ๋ชธ๋ฌด๊ฒŒ๊ฐ€ [70kg, 50kg, 80kg, 5 programmers.co.kr def solution(ppl, lim): ppl.sort() cnt = 0 while ppl: if len(ppl) == 1: cnt += 1 break cur_cnt = cnt for i in reversed(range(1, len(ppl))): if ppl[0]+ppl[i]
[Level 2] ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก ์ „ํ™”๋ฒˆํ˜ธ๋ถ€์— ์ ํžŒ ์ „ํ™”๋ฒˆํ˜ธ ์ค‘, ํ•œ ๋ฒˆํ˜ธ๊ฐ€ ๋‹ค๋ฅธ ๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ „ํ™”๋ฒˆํ˜ธ๊ฐ€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒฝ์šฐ, ๊ตฌ์กฐ๋Œ€ ์ „ํ™”๋ฒˆํ˜ธ๋Š” ์˜์„์ด์˜ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ ‘๋‘์‚ฌ์ž…๋‹ˆ๋‹ค. ๊ตฌ์กฐ๏ฟฝ๏ฟฝ programmers.co.kr def solution(p_b): for i in range(len(p_b)): for j in range(i+1, len(p_b)): if p_b[i] == p_b[j][:len(p_b[i])] or p_b[j] == p_b[i][:len(p_b[j])]: return False return True โ–ท ์ „ํ™”๋ฒˆํ˜ธ ๋ชฉ๋ก์— ์žˆ๋Š” ์ „ํ™”๋ฒˆํ˜ธ ๋‘ ๊ฐœ๋ฅผ ์„ ํƒํ•˜์—ฌ ๋น„๊ตํ•˜๋„๋ก for ๋ฌธ์„ ์ด์šฉํ•œ ์ด์ค‘๋ฃจํ”„๋ฅผ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. โ–ท ์ฃผ์˜ํ•  ์ ์€ ์ „ํ™”๋ฒˆํ˜ธ์˜ ์ ‘๋‘์–ด๋ฅผ ๋น„๊ตํ•  ๋•Œ, ..
[Level 2] H-Index (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - H-Index H-Index๋Š” ๊ณผํ•™์ž์˜ ์ƒ์‚ฐ์„ฑ๊ณผ ์˜ํ–ฅ๋ ฅ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ง€ํ‘œ์ž…๋‹ˆ๋‹ค. ์–ด๋Š ๊ณผํ•™์ž์˜ H-Index๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ’์ธ h๋ฅผ ๊ตฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์œ„ํ‚ค๋ฐฑ๊ณผ1์— ๋”ฐ๋ฅด๋ฉด, H-Index๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ตฌํ•ฉ๋‹ˆ๋‹ค. ์–ด๋–ค ๊ณผํ•™์ž๊ฐ€ ๋ฐœํ‘œ programmers.co.kr def solution(c): candi_h = [] if not max(c): return 0 for h in range(1, max(c)+1): l_h = 0 u_h = 0 for i in range(len(c)): if c[i] >= h: u_h += 1 if c[i] = h) and (l_h
์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network) ๊ตฌํ˜„ ํŒŒ์ดํ† ์น˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆœํ™˜ ์‹ ๊ฒฝ๋ง(Recurrent Neural Network)๋ฅผ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๋‹ค. ๊ตฌํ˜„ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 1. ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ 2. ๋ชจ๋ธ ์„ค์ • 3. ๋ชจ๋ธ ํ•™์Šต 4. ํ•™์Šต ๊ฒฐ๊ณผ 1. ๋ฐ์ดํ„ฐ ์ „์ฒ˜๋ฆฌ In: import torch import torch.nn as nn import torch.optim as optim import numpy as np string = "To climb steep hills requires a slow pace at first." chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ?!.,:;'01" char_list = [i for i in chars] n_letter = len(chars) def s..
[Level 2] ์œ„์žฅ (Python) ์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์œ„์žฅ programmers.co.kr def solution(clothes): clothes_num = {} for i in clothes: if i[1] in clothes_num: clothes_num[i[1]] += 1 else: clothes_num[i[1]] = 1 cnt = 1 for i in clothes_num.values(): cnt *= i+1 cnt -= 1 return cnt โ–ท ์ด ๋ฌธ์ œ๋Š” ํ‚ค์™€ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€ ์ˆ˜ ์žˆ๋‹ค. ์˜๋ฅ˜์˜ ์ข…๋ฅ˜๋ฅผ ํ‚ค๋กœ, ํ•ด๋‹น ์ข…๋ฅ˜์— ํฌํ•จ๋œ ์˜๋ฅ˜๋ฅผ ๊ฐ’์œผ๋กœ ๋‘”๋‹ค. ์ฒซ ๋ฒˆ์งธ for ๋ฌธ์€ ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ด๋‹ค. โ–ท ์ „์ฒด ๊ฒฝ์šฐ์˜ ์ˆ˜๋Š” ๊ฐ ํ•ด๋‹น ์˜๋ฅ˜์˜ ์ˆ˜์— 1์„ ๋”ํ•œ ํ›„, ๋ชจ๋‘ ๊ณฑํ•œ ๋’ค, 1์„ ๋นผ์ค€ ๊ฒƒ์„ ํ†ตํ•ด ๊ตฌํ•  ์ˆ˜ ์žˆ..