0. 아래의 코드를 실행하여 numpy와 matplotlib.pyploy을 임포트하라.

import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt 

1. 코드구현 I (문제에 조건이 있는 경우 조건을 준수할것)

(1) 리스트의 *연산자를 이용하여 길이가 10이고 모든원소가 1인 리스트를 선언하라.

[1]*10
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

(2) 튜플언패킹을 이용하여 아래를 한줄로 처리하는 코드를 작성하라.

a=1
b=2.33
c='guebin'
a,b,c = 1,2.33,'guebin'

(3) 길이가 1인 튜플을 만들어 자신의 학번을 저장하라.

(43052,)
(43052,)

(4) 아래와 같은 문자열의 마지막원소를 출력하는 코드를 작성하라.

test_str = 'guebin'
test_str = 'guebin'
test_str[-1]
'n'

(5) 본인 학번을 이용하여 random seed 를 설정하라. 평균이 0, 분산이 1인 정규분포에 10000개의 값을 추출한뒤 값이 1.96보다 큰 경우가 몇개 있는지 세어보라.

hint: 본인학번이 202143052인 경우 아래와 같이 설정

np.random.seed(202143052)
np.random.seed(202143052)
sum(np.random.randn(10000) > 1.96)
242

(6) 아래와 같은 array의 shape을 (4,3)으로 수정하는 코드를 작성하라.

test_arr = np.arange(12)
test_arr = np.arange(12)
test_arr.reshape(4,3)
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])

2. 코드구현 II

(1) ${\bf x}=(x_1,\dots,x_{100})$를 표준정규분포에서 서로독립인 100개의 난수를 생성하여 만든 길이가 100인 vector라고 하자.

(a) $\sum_{i=1}^{100} x_i^2$ 를 계산하라.

(b) $y_k = \sum_{i=1}^{k}x_i$를 만족하는 ${\bf y}=(y_1,y_2,\dots,y_{100})$를 생성하라.

x=np.random.randn(100)
sum(x**2)
103.57060890918511
x.cumsum()
array([ 0.47468281,  0.34386559,  1.15108986,  2.21490225,  3.68862517,
        6.6560929 ,  4.93164856,  4.64464292,  4.42000953,  4.59835234,
        4.89968072,  5.96011555,  6.05848061,  6.30156284,  6.45723823,
        5.83750354,  5.37523078,  5.15841694,  5.9573261 ,  6.05283436,
        5.22666108,  4.59445292,  4.28220497,  5.02407374,  4.04275772,
        3.760779  ,  4.64585317,  3.35034863,  4.70650237,  6.00188554,
        5.655467  ,  4.71014034,  4.1729924 ,  2.6572072 ,  2.45471889,
        2.4957559 ,  2.33281836,  1.93651955,  1.19124851,  0.72053316,
       -0.85868479, -0.51403318, -0.39293256, -0.11037825, -0.90405075,
        0.36017316, -0.64589555, -0.61405341, -0.6071379 , -2.25775258,
       -2.58632882, -4.15597547, -4.35785515, -3.06555043, -2.53143389,
       -3.75231655, -2.53480854, -3.17666336, -3.24829447, -4.82257719,
       -5.12059882, -3.92232594, -3.92249184, -5.26608319, -5.50649365,
       -6.94874974, -6.31473021, -5.30493964, -6.66675701, -6.05865575,
       -5.9302322 , -7.51211747, -5.28113967, -4.7794617 , -5.80837388,
       -6.83179461, -5.72807049, -5.43528895, -4.82945081, -6.24232477,
       -5.84089936, -4.82742572, -4.05496918, -5.22220941, -4.43970357,
       -6.44483947, -6.82657167, -6.58432556, -5.34876223, -4.73907863,
       -3.30982415, -3.40413528, -3.31029211, -1.97453616, -1.27172043,
       -1.88394219,  0.7616805 ,  1.32059937,  3.97185397,  3.08178286])

(2) $i=1,2,\dots,1000$에 대하여 $(\cos(t_i) ,\sin(t_i))$를 시각화하는 코드를 작성하라. 단 $t_i=\frac{2\pi i }{1000}$.

t=np.arange(1,1001)*2*np.pi/1000
plt.plot(np.cos(t),np.sin(t))
[<matplotlib.lines.Line2D at 0x7f02d16635b0>]

(3) 아래의 문자열에서 W가 몇개 들어 있는지 세는 코드를 작성하라.

test_str = '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'
test_str = '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'
test_str.count("W")
4
list(test_str).count("W")
4

(4) 블록대각행렬의 구현: 입력과 출력이 아래의 예시와 같은 함수를 구현하라.

### 예시1

입력: (2,3) 

출력: 
1 1 0 0 0 
1 1 0 0 0 
0 0 1 1 1 
0 0 1 1 1 
0 0 1 1 1
### 예시2

입력: (2,2,3) 

출력: 
1 1 0 0 0 0 0
1 1 0 0 0 0 0
0 0 1 1 0 0 0 
0 0 1 1 0 0 0
0 0 0 0 1 1 1 
0 0 0 0 1 1 1 
0 0 0 0 1 1 1
def f(arr): 
    arr2 = np.cumsum(arr)
    bmat = np.zeros([sum(arr) ,sum(arr) ])
    a,b=0,0 
    for i in arr2: 
        b=i 
        bmat[a:b,a:b] = np.ones(b-a) 
        a=i
    return bmat 
f([3,2,2])
array([[1., 1., 1., 0., 0., 0., 0.],
       [1., 1., 1., 0., 0., 0., 0.],
       [1., 1., 1., 0., 0., 0., 0.],
       [0., 0., 0., 1., 1., 0., 0.],
       [0., 0., 0., 1., 1., 0., 0.],
       [0., 0., 0., 0., 0., 1., 1.],
       [0., 0., 0., 0., 0., 1., 1.]])

3. 다음을 잘 읽고 물음에 답하라.

(1) 아래는 python을 설치하는 방법을 소개한 url 이다. 직접 url에 들어가서 설치하는 방법을 읽어보고 곤이, 철용, 아귀, 짝귀 중 옳은말을 한 사람을 모두 골라라.

(곤이) 해당 방법은 아나콘다를 이용하지 않고 파이썬을 설치하는 방법이다.

(철용) 그래서 이 방법으로는 가상환경을 만들 수 없겠군.

(아귀) 위 url에 제시된 방법으로 설치하면 항상 *.py을 만들어야만 파이썬코드를 실행할 수 있다는 단점이 있다.

(짝귀) 위 url은 IDE의 선택에 대하여 서술하고 있다.

답: 곤이, 철용

(2) 아래를 보고 적절한 설명을 한 사람을 골라라.

import pandas as pd 
pd?
Type:        module
String form: <module 'pandas' from '/home/cgb2/anaconda3/envs/py39/lib/python3.9/site-packages/pandas/__init__.py'>
File:        ~/anaconda3/envs/py39/lib/python3.9/site-packages/pandas/__init__.py
Docstring:  
pandas - a powerful data analysis and manipulation library for Python
=====================================================================

**pandas** is a Python package providing fast, flexible, and expressive data
structures designed to make working with "relational" or "labeled" data both
easy and intuitive. It aims to be the fundamental high-level building block for
doing practical, **real world** data analysis in Python. Additionally, it has
the broader goal of becoming **the most powerful and flexible open source data
analysis / manipulation tool available in any language**. It is already well on
its way toward this goal.

Main Features
-------------
Here are just a few of the things that pandas does well:

  - Easy handling of missing data in floating point as well as non-floating
    point data.
  - Size mutability: columns can be inserted and deleted from DataFrame and
    higher dimensional objects
  - Automatic and explicit data alignment: objects can be explicitly aligned
    to a set of labels, or the user can simply ignore the labels and let
    `Series`, `DataFrame`, etc. automatically align the data for you in
    computations.
  - Powerful, flexible group by functionality to perform split-apply-combine
    operations on data sets, for both aggregating and transforming data.
  - Make it easy to convert ragged, differently-indexed data in other Python
    and NumPy data structures into DataFrame objects.
  - Intelligent label-based slicing, fancy indexing, and subsetting of large
    data sets.
  - Intuitive merging and joining data sets.
  - Flexible reshaping and pivoting of data sets.
  - Hierarchical labeling of axes (possible to have multiple labels per tick).
  - Robust IO tools for loading data from flat files (CSV and delimited),
    Excel files, databases, and saving/loading data from the ultrafast HDF5
    format.
  - Time series-specific functionality: date range generation and frequency
    conversion, moving window statistics, date shifting and lagging.

(로이) pd?이 실행된 결과를 살펴보니 사용자가 pandas라는 이름의 패키지를 설치했거나 본인이 pandas라는 이름의 폴더를 만들어 ~/anaconda3/envs/py39/lib/python3.9/site-packages에 넣었다고 볼 수 있겠군.

(이서) 네, 따라서 앞으로 pandas 패지키에 포함된 모든 함수를 이용하기 위해서는 pandas. 를 앞에 붙이고 사용하시면 됩니다. 예를들어 pandas안의 concat()함수를 사용하고 싶다면 pandas.concat()과 같은 형식으로요.

(일권) 이서말도 맞지만 아래와 같이 선언한다면 pandas.concat() 대신에 그냥 concat() 만으로도 사용할 수 있어.

import pandas as pd 
from pd import concat

(현이) pd?의 실행결과 Docstring:이 있는것으로 보아 __init__.py상단에 아래와 같은 내용이 있음을 유추할 수 있어.

pandas - a powerful data analysis and manipulation library for Python
=====================================================================
... 
(중략) 
...
  - Time series-specific functionality: date range generation and frequency
    conversion, moving window statistics, date shifting and lagging.

답: 로이, 현이

some notes

- 모든 문항은 부분점수 없이 채점합니다.

- 코드구현 I은 문제의 조건을 준수하여 구현해야 하는 유형입니다. 조건을 무시하고 구현할시 0점처리합니다.

  • 예를들어 문제 1-(1) 의 경우 [1,1,1,1,1,1,1,1,1,1] 와 같이 리스트를 선언하면 0점 처리됩니다.

- 코드구현 II는 자유롭게 코드를 구현해도 괜찮은 유형입니다.

- 문제 3-(1)의 경우 영어로 된 url이 나올 수 있습니다.

- 실제시험은 예상유형의 문항수보다 많습니다.

- 예상유형과 똑같은 문제는 시험에 출제되지 않습니다.