R입문 중간고사
작성 완료
-
파이썬 연습겸 R입문 중간고사 문제 파이썬으로 풀어봄
2**-5 + 2**3
33**0.5
sum_ = 0
for k in range(1, 101):
sum_ += 1 / ((k+1) ** 2)
print(sum_)
-
리스트 컴프리핸션
sum([1/((k+1)**2) for k in range(1, 101)])
import numpy as np
x = np.arange(-10, 10.5, 0.5)
print(x)
def f(x):
if abs(x) > 5:
return x
elif abs(x) < 2:
return 0
else:
return 5
y = list(map(f, x))
print(y)
from math import exp
def f(x):
return 2*x + 3
def g(x):
return exp(x) / (1 + exp(x))
def h(x):
return max(x, 0)
x1 = list(map(f, x))
x2 = list(map(g, x1))
x3 = list(map(h, x2))
print(x3)
import matplotlib.pyplot as plt
n = 10
l = [0]*n
for i in range(1, n+1):
l[i-1] = 1/(2**i)
l_ = np.cumsum(l)
plt.plot(list(range(n)), l_)
sum = 0
n = 10
x = 5 ## 예시
for i in range(n):
sum += x**i
print(exp(x),"\n", sum)
-
모든 x에 대해 성립해야 되는데 아니다
x = a = 10 #예시
salary = a
for i in range(1, 19):
a *= 1.08
salary += a
for j in range(20, 29):
a *= 0.75
salary += a
print(salary / x)
-
(a) : 참
-
(b) : 거짓
-
(c) : 거짓
-
(d) : 거짓
n = 100
doors = [False] * (n+1)
for i in range(1, n+1):
for j in range(i, n+1):
if j % i == 0:
if doors[j] == True:
doors[j] = False
else:
doors[j] = True
sum_ = 0
for k in range(1, n+1):
if doors[k] == True:
sum_ += 1
print(sum_)
dist = 35
dp = [0] * 456
dp2 = [0] * 456
x = np.array(range(1, 457)) / 100
for i in np.arange(10, 0, -0.5):
for j in range(456):
if dp2[j] == 0:
dp[j] = round(dp[j] + round(x[j], 3), 3)
if x[j] > i and dp2[j] == 0: ## 0은 False이다
if dp[j] - round((x[j] - i), 3) < dist:
dp2[j] = -1
elif dp[j] - round((x[j] - i), 3) >= dist:
dp2[j] = 1
elif x[j] <= i and dp[j] >= dist and dp2[j] == 0:
dp2[j] = 1
x += 1
dp2[0] # 사망
dp2[66] # 사망
dp2[217] # 생존
dp2[455] # 사망
sum_ = 0
for k in range(456):
if dp2[k] == 1:
sum_ += 1
print(sum_)
-
전체 생존자는 85명
-
고찰
-
이것때문에 정신나감
-
그리고 처음 dp 설정할 때 전부 0으로 해야됐는데 -1로 해서 또 정신나감
a = np.array([0, 0])
b = [0] * 2
a[0] += 0.01
print(a[0])
-
왜 0인지 이해가 안됨
-
왜 0.01이 아니냐?
b[0] += 0.01
print(b[0])
-
ㅋㅋㅋㅋㅋㅋㅋㅋㅋ
-
넘파이 어레이는 0인데 그냥 리스트는 0.01이다
-
이유는 모른다
import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/miruetoto/yechan/master/_notebooks/round2.csv")
mat = np.matrix(df)
mat.shape
plt.plot(mat[:,0], mat[:,1], '.k')
mat[0,:]
np.matrix([[0,-1], [-1,0]]) @ np.matrix([12,313]).T
mat2 = mat
for i in range(5513):
mat2[i,:] = (np.matrix([[0,-1], [-1,0]]) @ mat[i,:].T).T
plt.plot(mat2[:,0], mat2[:,1], '.r')
-
위에서 mat2 = mat이라고 했는데 그러면 안됨
id(mat2), id(mat)
-
메모리 주소가 동일해서 mat2를 바꾸면 mat도 바뀌어서 아래와 같이 된다
plt.plot(mat[:,0], mat[:,1], '.r')
-
그럼 어떻게? ---> 깊은복사!
-
mat 다시 초기화
mat = np.matrix(df)
plt.plot(mat[:,0], mat[:,1], '.k')
mat3 = mat.copy()
for i in range(5513):
mat3[i,:] = (np.matrix([[0,-1], [-1,0]]) @ mat[i,:].T).T
plt.plot(mat3[:,0], mat2[:,1], '.r')
id(mat), id(mat3)
-
메모리 주소가 다르다
plt.plot(mat[:,0], mat[:,1], '.k')
-
mat3를 바꿨지만 깊은복사를 하였기에 mat에는 영향을 끼치지 않음