{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 06wk-1: Numpy (1)\n",
"\n",
"최규빈 \n",
"2023-04-10\n",
"\n",
"
\n",
"\n",
"# 중간고사 관련\n",
"\n",
"`1`. 일정\n",
"\n",
"- 9wk-1 : 가장 유력\n",
"- 8wk-2, 9wk-2: 가능성 있음\n",
"\n",
"`2`. 방식\n",
"\n",
"- 방식1: 오픈북 O / 구글검색 O / ChatGPT O \\<– 가장 유력\n",
"- 방식2: 오픈북 O / 구글검색 O / ChatGPT X \\<– 2022년 중간고사 방식\n",
"- 방식3: 오픈북 X / 구글검색 X / ChatGPT X \\<– 2021년 중간고사 방식\n",
"\n",
"`3`. 난이도\n",
"\n",
"- 시험방식에 따라 다름 (방식1로 칠 경우 난이도가 가장 높을 것으로\n",
" 예상)\n",
"\n",
"`4`. 범위\n",
"\n",
"- 넘파이, 판다스, matplolib \\<– 유력\n",
"\n",
"# 강의영상\n",
"\n",
"> youtube:\n",
"> \n",
"\n",
"# 예비학습\n",
"\n",
"`-` 혹시 행렬을 처음 보시는 학생이 있다면 아래주소의 영상에서 1-4를\n",
"학습하시면 됩니다. (1-5까지 있는데 1-4까지만 보시면됩니다) 혹은 스스로\n",
"학습하셔도 무방합니다.\n",
"\n",
"- https://guebin.github.io/IR2021/2021/10/29/(A2)-%EB%A7%A4%ED%8A%B8%EB%A6%AD%EC%8A%A4.html\n",
"\n",
"`-` 행렬을 알고 있다면 위의 주소의 영상은 skip 하셔도 무방합니다.\n",
"\n",
"# import"
],
"id": "070a4b52-e480-4ec6-bc6e-104630a345d9"
},
{
"cell_type": "code",
"execution_count": 310,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
],
"id": "8adbbe65-da8c-4f20-ab65-6301e6e1bfc1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 넘파이 공부 1단계\n",
"\n",
"## 선언"
],
"id": "4bb4d22c-027b-4226-a29f-8bb238265423"
},
{
"cell_type": "code",
"execution_count": 313,
"metadata": {},
"outputs": [],
"source": [
"l = [1,2,3]\n",
"a = np.array(l)"
],
"id": "53af6c64-6440-408e-9c31-03ce91fb8a41"
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"a=np.array([1,2,3]) # list를 만들고 ndarray화 시킴 \n",
"l=[1,2,3]"
],
"id": "2fab2a6e-e024-41b7-96e2-4ab023fa312a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 기본연산 브로드캐스팅\n",
"\n",
"`-` 사칙연산가능"
],
"id": "f0932bfb-e2ef-4da6-a293-bb396ee8bfb8"
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"a+1 ## [1,2,3] + 1 = [2,3,4]"
],
"id": "8c94ee51-e9b5-4838-9ee8-df1eaa8bff56"
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"l+1"
],
"id": "4185b316-27a9-4ab7-9399-1dc8f87f3d6b"
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"a*2"
],
"id": "26aa79d2-8959-4c95-bbd6-5106b14b8b27"
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"l*2"
],
"id": "d760dcfa-c674-44fc-b4ea-3774ebcf34c7"
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"a/2"
],
"id": "b2cc457b-b667-4d7f-8bd1-e5959f6396c6"
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"l/2"
],
"id": "f3a14c42-5c51-435d-b48d-36c07bcf62b6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 제곱이 가능"
],
"id": "4b5b5680-c0c1-487e-9e4a-257c1ab1860f"
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"a**2"
],
"id": "76ca10c8-e5d4-4f3d-b997-eee258b00a65"
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"l**2"
],
"id": "01eb01cc-fa6e-43cd-ba1f-a8fe806ebe9f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 나머지 연산가능"
],
"id": "f88b516c-8842-415f-ba0a-fd870819bc31"
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"a%2 # %2 = 2로 나눈 나머지를 리턴 a=[1,2,3] "
],
"id": "d6e2a52b-92e0-4ee8-95b2-afb4b24bf4cf"
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"l%2"
],
"id": "a6f885f4-10f7-412b-8b4a-29781a0f0ddf"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 기타수학연산지원"
],
"id": "513f0b13-a4f2-4ab0-b1ff-20aba7285fb9"
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"np.sqrt(a), np.sqrt(l)"
],
"id": "5cfd3ab5-d6fa-4fd0-9489-2d40d4e08a1f"
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"np.log(a), np.log(l)"
],
"id": "a89b7818-4db4-487d-9af9-6035295ab9ac"
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"np.exp(a), np.exp(l)"
],
"id": "cd1a21de-eac3-4b34-98a6-5c7541788a13"
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"np.sin(a), np.sin(l)"
],
"id": "d01340e5-c6c7-409a-a379-1d5f51915379"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 차원 ($\\star$)\n",
"\n",
"`-` 넘파이배열의 차원은 .shape 으로 확인가능\n",
"\n",
"`-` 아래는 모두 미묘하게 다르다."
],
"id": "ce80bf1b-71ac-4d35-9c06-c27bf87b2434"
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"a=np.array(3.14) # 스칼라, 0d array \n",
"a, a.shape"
],
"id": "2d8272fa-a98d-4702-9c2b-b2c08130d120"
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"a=np.array([3.14]) # 벡터, 1d array \n",
"a, a.shape"
],
"id": "a6074b71-043f-45b8-a7fb-5742fc6172a4"
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"a=np.array([[3.14]]) # 매트릭스, 2d array \n",
"a, a.shape"
],
"id": "9510658e-921e-4f0d-ae60-4c5ea3fcbf30"
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"a=np.array([[[3.14]]]) # 텐서, 3d array \n",
"a, a.shape"
],
"id": "be8a4dd9-7616-4fe1-ba12-bb003a2de222"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 넘파이 공부 2단계\n",
"\n",
"## 인덱싱 1차원\n",
"\n",
"`-` 선언"
],
"id": "6c437330-ff6a-4b6f-af3f-f1c815c17a3a"
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"l=[11,22,33,44,55,66] \n",
"a=np.array(l) "
],
"id": "c861c6e5-d139-4e0e-a455-039979ac9321"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 인덱스로 접근"
],
"id": "bfea0e86-1d51-4d8a-bb02-21e105c289c4"
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"l[0],l[1],l[2],l[3],l[-2],l[-1]"
],
"id": "5938ed7f-c289-41b9-a1df-b2e824f3d654"
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"source": [
"a[0],a[1],a[2],a[3],a[-2],a[-1]"
],
"id": "1b4c447d-005e-49a1-8a8f-3badc2077bc6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` `:` 이용 (슬라이싱)"
],
"id": "86fca6a7-5147-4fc7-bc3c-57334133eebc"
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"l[2:4] # index 2에서 시작, index 4는 포함하지 않음 "
],
"id": "cdc3ee19-9257-479e-bf45-f01d5e7ffc8f"
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"a[2:4] "
],
"id": "0288a120-cad8-4ce9-8100-417c5811e3d1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 정수배열에 의한 인덱싱"
],
"id": "69aacd1f-b940-4b73-9133-756c23c19095"
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "11e47e31-2fab-46b2-a6cb-122a9cb56a00"
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"a[[0,2,4]] # index=0, index=2, index=4 에 해당하는 원소를 뽑고 싶다 "
],
"id": "7c91dd7d-2bc8-4561-a67e-a8f9f42636bd"
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"l[[0,2,4]] # 리스트는 불가능 "
],
"id": "b8afb3ad-8076-4f05-93a7-7c52aad55a3c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 참고: 정수배열에 의한 인덱싱에서 아래의 코드는 돌아가지 않음."
],
"id": "e7290517-8b6a-432c-8c14-45fed6371a1b"
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"a[(0,2,4)] # a[[0,2,4]] 는 돌아감 "
],
"id": "4db7bce3-64e8-49fb-bef7-2cc2fd712e52"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 즉 a\\[`index`\\]에서 index의 자리에 리스트를 넣어야만 동작.\n",
"- index자리에 튜플을 넣으면 동작하지 않음.\n",
"- 이러한 사소한 지식까지 알아야 하는걸 추천하는건 아님.\n",
"\n",
"`-` 부울값에 의한 인덱싱"
],
"id": "0a2b4106-a8c2-41a0-bf9a-5247620017f8"
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [],
"source": [
"a"
],
"id": "402ca51a-ab66-420f-832d-2816d67b54f0"
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"a[[True, False, True, False, True, False]]"
],
"id": "976f4223-ded7-4860-99c2-3be6696fb01f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"응용하면?"
],
"id": "65798bec-ca17-46be-9a64-95300bfca9d3"
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"a < 33 "
],
"id": "b6aaf4ce-c5a1-4cdf-bc80-51696ffd5d79"
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"a[a<33]"
],
"id": "17289e20-3b6d-4350-bf3c-43c6ffdee09e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"리스트는 불가능"
],
"id": "9a0ddcbe-539f-4bfd-8561-b42a7c131a47"
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"l<33 # 여기에서부터 불가능 "
],
"id": "2c596997-7960-4595-8495-52ff8baff998"
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"l[[True,False,True,False,True,False]] # 이것도 불가능 "
],
"id": "a3afa138-ebf2-4155-a4c8-e39b7dcd9ad0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 인덱싱 2차원\n",
"\n",
"`-` 중첩리스트와 2차원 np.array 선언"
],
"id": "adfb23a1-f4f5-4a0a-82f4-e2e80ba98336"
},
{
"cell_type": "code",
"execution_count": 403,
"metadata": {},
"outputs": [],
"source": [
"A = [[1,2,3,4],[-1,-2,-3,-4],[5,6,7,8],[-5,-6,-7,-8]]\n",
"A2 = np.array(A)"
],
"id": "7331f59b-c1af-4c99-9e77-dbad1859db53"
},
{
"cell_type": "code",
"execution_count": 404,
"metadata": {},
"outputs": [],
"source": [
"A"
],
"id": "366e82df-8d76-48e8-8d38-02e36dc3c4fb"
},
{
"cell_type": "code",
"execution_count": 405,
"metadata": {},
"outputs": [],
"source": [
"A2"
],
"id": "39b30cc4-59b6-4c6b-b23d-23ddeeb8daa3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` A의 원소 인덱싱"
],
"id": "b74f459d-0483-4d7a-9e2d-4104cfee4ce7"
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"np.array(A)"
],
"id": "d20bae49-24c3-400b-940b-fbcd726bd701"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` A(이중list)에서의 원소 추출"
],
"id": "4e7109d7-983b-4418-99b6-b8ab703e43fd"
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"A[0][0] # (1,1)의 원소 "
],
"id": "f0ead1ae-fed3-47d3-b272-127bf2fa24ac"
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"A[1][2] # (2,3)의 원소 "
],
"id": "d3aeb65c-1899-4f26-b53b-06f16fc211d9"
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"A[-1][0] # (4,1)의 원소 "
],
"id": "77f60a6c-1e66-4f9b-ad0f-1306242f1920"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` A2(넘파이어레이)에서의 원소 인덱싱"
],
"id": "6f502bc3-3fa8-42a9-9d05-cba53723bcce"
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"A2[0][0] # (1,1)의 원소 "
],
"id": "336c2274-24cf-4876-a74c-831ab2dfd772"
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"A2[1][2] # (2,3)의 원소 "
],
"id": "369bcfd3-57e0-47b1-873a-0e40f5d0c2f1"
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"A2[-1][0] # (4,1)의 원소 "
],
"id": "5fd4f7c6-cfcd-42fb-8376-e3f1fe1de9c1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` A2에서만 되는 기술 (넘파이에서 제시하는 신기술, R에서는 기본적으로\n",
"쓰던것, 이중list는 불가능)"
],
"id": "7300d21a-cf14-46e3-9ea9-193adff8ee25"
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"A2[0,0] # (1,1)의 원소 "
],
"id": "e02143a9-2d30-44ec-920c-a511c5bc3b1f"
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"A2[1,2] # (2,3)의 원소 "
],
"id": "45f7e56b-cc06-4d92-b0c3-988d43acdbd5"
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"A2[-1,0] # (4,1)의 원소 "
],
"id": "909078ec-c2c0-46aa-a229-d986284b2e70"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` A2에서의 인덱싱예시\n",
"\n",
"**(예시1)** 1행,1-2열 추출"
],
"id": "ab61dc15-06bf-451d-b4af-d49a6492f721"
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"A2"
],
"id": "59a2221a-4a22-453d-a00a-8df58dad8f7f"
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"A2[0,0:2] # 1행1열, 1행2열 "
],
"id": "c029d84f-8d2d-4da0-935e-f56bc73c0ed2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(예시2)** 1행 추출"
],
"id": "1b88621c-91a1-4e7f-8aa2-5f47708de457"
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [],
"source": [
"A2"
],
"id": "bdde7594-8fab-4545-91c6-db611dbbda32"
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"A2[0,:] # 방법1"
],
"id": "d60a1858-d5cd-4562-b8f7-06067d59801d"
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [],
"source": [
"A2[0] # 방법2"
],
"id": "abd02229-fee6-4851-bb44-2794d8052dd3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(예시3)** 1행,3행 추출"
],
"id": "29e0819d-944e-4f6a-a590-1166bfe8f366"
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"A2"
],
"id": "03bba20f-7159-490d-b784-a7117787c8f1"
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [],
"source": [
"A2[[0,2],:] # 방법1"
],
"id": "ac91a63f-3a34-4cc3-819c-dd63159011d4"
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"A2[[0,2]] # 방법2"
],
"id": "1c266c0a-ed43-4bd7-8cfe-0a489795b4e6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(예시4)** 1열 추출"
],
"id": "16bdc20a-cf66-47f1-b6a0-ea3e27d5a1e7"
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"A2"
],
"id": "a3626d3d-4cf1-4965-97c6-5ee214c2b76a"
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [],
"source": [
"A2[:,0] # 방법1, 출력이 이상하게 보이는데?"
],
"id": "1bf0ad5d-83af-4cad-9cdf-60a1b72232db"
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [],
"source": [
"A2[:,[0]] # 방법2"
],
"id": "69c14f70-49e5-4d13-8f70-345510023961"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(예시5)** 1열,3열 추출"
],
"id": "1f7346df-1dd2-4564-a78d-3f4ec572815e"
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [],
"source": [
"A2"
],
"id": "df8965f7-b9c6-4a78-9ee8-aeff476c089a"
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [],
"source": [
"A2[:,[0,2]] # 1열, 3열"
],
"id": "52dcf8c6-1a96-4b99-803f-24ed0eaed1e2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**(예시6)** 1행2행, 1열3열 추출 – 삭제하려다가 다시 설명하는 내용"
],
"id": "066a3310-146a-4916-a650-520a928b81da"
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"A2"
],
"id": "065d7d76-320a-43ba-a53c-a9250ea36f3c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**헷갈려:** 문장이 좀 모호한데 경우1,2중 수행 하라는 것인지 모호하다.\n",
"\n",
"(경우1) 이러한 숫자들을 뽑으라는 의미인지, (즉 1행 1,3열, 2행 1,3열을\n",
"뽑으란 의미인지)\n",
"\n",
"$$\\begin{bmatrix}\n",
"1 & | & 3 & | \\\\\n",
"-1 & | & -3 & | \\\\\n",
"| & | & | & | \\\\\n",
"| & | & | & |\n",
"\\end{bmatrix}$$\n",
"\n",
"(경우2) 이러한 숫자들을 뽑으란 의미인지, (즉 1행1열, 2행3열을 뽑으란\n",
"의미인지)\n",
"\n",
"$$\\begin{bmatrix}\n",
"1 & | & | & | \\\\\n",
"| & | & -3 & | \\\\\n",
"| & | & | & | \\\\\n",
"| & | & | & |\n",
"\\end{bmatrix}$$"
],
"id": "c7227c0f-20b9-45a7-9fbe-56be4747da95"
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [],
"source": [
"A2[0:2, [0,2]] # 경우1이 구현"
],
"id": "3ac62958-589c-4397-90d5-babdb25c849c"
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [],
"source": [
"A2[[0,1],[0,2]] # 경우2가 구현"
],
"id": "8e573c93-9cd5-49e2-9ae0-3aab7f734250"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 인덱싱에 대한 미묘한 차이 $(\\star)$\n",
"\n",
"**관찰**: 2차원 array의 인덱싱 결과가 2차원이 나올수도있고, 1차원이\n",
"나올수도 있다."
],
"id": "76d3b648-5422-4ff4-870c-8c08abc7549e"
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"A2[0], A2[0].shape"
],
"id": "49a85e9e-d4ce-4208-a955-0af77a8162de"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- shape이 (4,)인 ndarray = 길이가 4인 1차원 벡터"
],
"id": "a8d08da8-24c2-494c-8907-e79c6c1b88b0"
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"A2[0:2], A2[0:2].shape"
],
"id": "955e0c7e-8eb9-4e5b-8a0e-642c5b7cbbf2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- shape이 (2,4)인 ndarray = dim이 `2*4`인 행렬"
],
"id": "02d41a23-d68d-43c0-8640-78721886bcb0"
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [],
"source": [
"A2[0:1], A2[0:1].shape"
],
"id": "d82551fe-3679-4d02-9934-43173cc03f99"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- shape이 (1,4)인 ndarray = dim이 `1*4`인 행렬\n",
"- `A2[0:1]`은 `A2[0]`와 같은 코드가 아니었다!!\n",
"\n",
"**인덱싱:** 결과가 어떻게 나올지 생각해보고 인덱싱 할 것\n",
"\n",
"(예시1) 한개의 값을 뽑을때 -\\> 출력을 0차원,1차원,2차원으로 만들 수\n",
"있다."
],
"id": "540ecd37-ea75-4807-b03a-56b57762db3e"
},
{
"cell_type": "code",
"execution_count": 406,
"metadata": {},
"outputs": [],
"source": [
"A2"
],
"id": "0990e854-38a6-4b35-b232-852b848a6b50"
},
{
"cell_type": "code",
"execution_count": 407,
"metadata": {},
"outputs": [],
"source": [
"A2[0,0] # 출력이 0차원 "
],
"id": "a64a4baf-1338-474e-8645-ee7867b587b1"
},
{
"cell_type": "code",
"execution_count": 408,
"metadata": {},
"outputs": [],
"source": [
"A2[[0],0] # 출력이 1차원 "
],
"id": "e96213a9-9470-4dfc-b8d5-0f2cff154cb1"
},
{
"cell_type": "code",
"execution_count": 409,
"metadata": {},
"outputs": [],
"source": [
"A2[0,[0]] # 출력이 1차원 "
],
"id": "c4bf71ed-b0cf-4ca0-9252-c73ac2ac02ab"
},
{
"cell_type": "code",
"execution_count": 416,
"metadata": {},
"outputs": [],
"source": [
"A2[0:1,0] # 출력이 1차원 "
],
"id": "0557ef41-8b3d-4538-a472-ac6301ac9e7e"
},
{
"cell_type": "code",
"execution_count": 415,
"metadata": {},
"outputs": [],
"source": [
"A2[0,0:1] # 출력이 1차원"
],
"id": "a5412c7f-49ce-49f3-ada1-bb3f380965bb"
},
{
"cell_type": "code",
"execution_count": 414,
"metadata": {},
"outputs": [],
"source": [
"A2[0:1,0:1] # 출력이 2차원 "
],
"id": "525e734d-370b-4397-afbd-7d71ed89b796"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시2) 하나의 벡터를 뽑을때 -\\> 출력을 1차원, 2차원으로 만들 수 있다."
],
"id": "258e8491-374f-42f7-8695-03eaa6a39528"
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [],
"source": [
"A2"
],
"id": "173e050f-06ea-4d63-a04e-fa9b60b2ab1a"
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
"A2[:,1] # 출력이 1차원: 출력은 길이가 4인 벡터처럼 해석가능 "
],
"id": "8d9bf8bf-676b-42b6-b8af-29810ceba2d6"
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [],
"source": [
"A2[:,[1]] # 출력이 2차원: 출력을 4*1 매트릭스처럼 해석가능"
],
"id": "1a8c4e85-26f7-4bd8-96b0-8b19a6a008c5"
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [],
"source": [
"A2[-1,:] # 출력이 1차원: 출력을 길이가 4인 벡터처럼 해석가능 "
],
"id": "fa8f47de-1630-4cf2-a6db-8b0e7c7bb426"
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [],
"source": [
"A2[[-1],:] # 출력이 2차원: 출력을 1*4 매트릭스처럼 해석가능 "
],
"id": "81aaca1f-3e94-49ee-81a4-ee16756d0d10"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1차원 배열의 선언\n",
"\n",
"`-` 리스트나 튜플을 선언하고 형변환"
],
"id": "8d768924-d435-4e0b-9bc0-4ba69fcffaa7"
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"np.array((1,2,3)) # 튜플->넘파이어레이 "
],
"id": "3de506da-1bac-4022-8666-2867fe205aef"
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [],
"source": [
"np.array([1,2,3]) # 리스트 ->넘파이어레이 "
],
"id": "669f3058-244c-4fe4-934f-25342d735c78"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` range()를 이용해서 선언하고 형변환"
],
"id": "e2ac3b21-3b3b-47e9-8615-87276d6a90c2"
},
{
"cell_type": "code",
"execution_count": 133,
"metadata": {},
"outputs": [],
"source": [
"np.array(range(10)) # range(10) -> 넘파이어레이 "
],
"id": "616b8bfb-b9d4-4ae5-936c-cfaa6f577ab1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` np.zeros, np.ones"
],
"id": "0f53c383-427d-4494-999e-b5853529c8a3"
},
{
"cell_type": "code",
"execution_count": 135,
"metadata": {},
"outputs": [],
"source": [
"np.zeros(3) "
],
"id": "ac3774ab-6f17-4b94-a68b-ae712290f53d"
},
{
"cell_type": "code",
"execution_count": 137,
"metadata": {},
"outputs": [],
"source": [
"np.ones(4)"
],
"id": "fe38e90b-920d-4e29-b43e-0d889e3266c1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` np.linspace"
],
"id": "2b617e46-21aa-4e33-a83c-871e82455ea1"
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {},
"outputs": [],
"source": [
"np.linspace(0,1,12) # 0에서 시작하고 1에서 끝남 (양끝점 모두 포함)"
],
"id": "16dfe855-350d-4534-a6d7-ce0179cf6a6f"
},
{
"cell_type": "code",
"execution_count": 142,
"metadata": {},
"outputs": [],
"source": [
"len(np.linspace(0,1,12)) # 길이는 12"
],
"id": "718ae69c-69e1-4d76-b48f-2a3d85f3b853"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` np.arange"
],
"id": "d3e05fc5-df74-4923-b60a-26013b80c469"
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [],
"source": [
"np.arange(5) # np.array(range(5))"
],
"id": "6d2de2ee-ecc8-409a-b958-a2bda810537a"
},
{
"cell_type": "code",
"execution_count": 190,
"metadata": {},
"outputs": [],
"source": [
"np.arange(1,6) # np.array(range(1,6))"
],
"id": "96a13589-3084-4502-8a15-6ec9c6ecca36"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## reshape ($\\star$)\n",
"\n",
"`-` reshape: ndarray의 특수한 기능"
],
"id": "8f7cb868-9da0-4e73-aa3b-4e707643bd6e"
},
{
"cell_type": "code",
"execution_count": 197,
"metadata": {},
"outputs": [],
"source": [
"a=np.array([11,22,33,44,55,66])\n",
"a ## 길이가 6인 벡터 "
],
"id": "4731e325-ab66-4463-a754-e46af4b78345"
},
{
"cell_type": "code",
"execution_count": 198,
"metadata": {},
"outputs": [],
"source": [
"a.reshape(2,3) ## (2,3) matrix 라고 생각해도 무방 "
],
"id": "dc0791b2-2774-45cc-bee8-e66b08a843b6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"note: reshape은 a자체를 변화시키는것은 아님"
],
"id": "1572fa88-c9ba-4f54-8e3f-246e2936702e"
},
{
"cell_type": "code",
"execution_count": 207,
"metadata": {},
"outputs": [],
"source": [
"a # a는 그대로 있음 "
],
"id": "3d1b3d98-bed1-49ca-aba0-aa028b9a0a1a"
},
{
"cell_type": "code",
"execution_count": 208,
"metadata": {},
"outputs": [],
"source": [
"b= a.reshape(2,3) # a를 reshape한 결과를 b에 저장 \n",
"b"
],
"id": "21e92247-3bad-4dbd-b90b-575a4882b77e"
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [],
"source": [
"a # a는 여전히 그대로 있음"
],
"id": "9d7a19da-c4f9-4780-9c45-8ed12ae468e2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 다시 b를 a처럼 바꾸고 싶다"
],
"id": "b08aacf3-8275-4d2c-b2ef-3f5d0c4a3d56"
},
{
"cell_type": "code",
"execution_count": 171,
"metadata": {},
"outputs": [],
"source": [
"b"
],
"id": "bcacae02-f02c-4d7d-aeda-bc1b19c8e88b"
},
{
"cell_type": "code",
"execution_count": 174,
"metadata": {},
"outputs": [],
"source": [
"b.reshape(6) # b는 (2,3) matrix , 그런데 이것을 길이가 6인 벡터로 만들고 싶다. "
],
"id": "5af9b148-56be-471a-939a-c968307f9a61"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` reshape with -1"
],
"id": "e63aa529-98b1-48c6-a9aa-83a531e0138b"
},
{
"cell_type": "code",
"execution_count": 180,
"metadata": {},
"outputs": [],
"source": [
"a=np.arange(24) # np.array(range(24))\n",
"a"
],
"id": "9403d743-e9ae-4a21-917e-1fcc2ab8fe43"
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [],
"source": [
"a.reshape(2,-1)"
],
"id": "96eaef8b-4785-42b9-b501-3a7c76a0eb6c"
},
{
"cell_type": "code",
"execution_count": 186,
"metadata": {},
"outputs": [],
"source": [
"a.reshape(3,-1)"
],
"id": "0b2a2e78-87cb-4d72-bfda-d45452fa706c"
},
{
"cell_type": "code",
"execution_count": 187,
"metadata": {},
"outputs": [],
"source": [
"a.reshape(4,-1)"
],
"id": "0c0e19df-d09e-4134-ae05-fcf4682d7c2c"
},
{
"cell_type": "code",
"execution_count": 188,
"metadata": {},
"outputs": [],
"source": [
"a.reshape(5,-1)"
],
"id": "69b2d2d5-74c3-401c-bf52-5ab1a8e55c8d"
},
{
"cell_type": "code",
"execution_count": 189,
"metadata": {},
"outputs": [],
"source": [
"a.reshape(6,-1)"
],
"id": "b207ba94-c3b1-4737-97d2-ef0a94f4177b"
},
{
"cell_type": "code",
"execution_count": 190,
"metadata": {},
"outputs": [],
"source": [
"a.reshape(7,-1)"
],
"id": "1ae36f11-6d1c-4db0-8dd5-09de802896ef"
},
{
"cell_type": "code",
"execution_count": 191,
"metadata": {},
"outputs": [],
"source": [
"a.reshape(8,-1)"
],
"id": "46efedd9-26f4-4e39-b4d7-ef83e86f9ea6"
},
{
"cell_type": "code",
"execution_count": 192,
"metadata": {},
"outputs": [],
"source": [
"a.reshape(12,-1)"
],
"id": "90c353ed-911d-4be1-a63e-38f3f30d72dc"
},
{
"cell_type": "code",
"execution_count": 193,
"metadata": {},
"outputs": [],
"source": [
"b= a.reshape(12,-1)\n",
"b"
],
"id": "d4ea5808-dbc6-4f35-89fd-05863c87d1f3"
},
{
"cell_type": "code",
"execution_count": 197,
"metadata": {},
"outputs": [],
"source": [
"b.reshape(-1) # b를 다시 길이가 24인 벡터로!"
],
"id": "004a3421-e410-4341-b41d-86362ff76959"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2차원 배열의 선언"
],
"id": "a5e96de3-8de4-4377-b34f-0dcfb889ffe0"
},
{
"cell_type": "code",
"execution_count": 198,
"metadata": {},
"outputs": [],
"source": [
"np.zeros((3,3))"
],
"id": "3da3c67b-30b7-4cf1-872e-d310ea4055e1"
},
{
"cell_type": "code",
"execution_count": 199,
"metadata": {},
"outputs": [],
"source": [
"np.ones((3,3))"
],
"id": "73c8e7f2-61f7-4325-b002-d4cbcef6bdf7"
},
{
"cell_type": "code",
"execution_count": 200,
"metadata": {},
"outputs": [],
"source": [
"np.eye(3)"
],
"id": "299f35b3-aabc-40e5-b15f-82fd4f2b4575"
},
{
"cell_type": "code",
"execution_count": 202,
"metadata": {},
"outputs": [],
"source": [
"np.diag([1,2,3,-1])"
],
"id": "cfef5c30-b3ef-4587-9354-35820bef735f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 랜덤으로 배열 생성"
],
"id": "7e1c7c5a-d6aa-4bdc-a87c-0a436401fc50"
},
{
"cell_type": "code",
"execution_count": 204,
"metadata": {},
"outputs": [],
"source": [
"np.random.randn(10) # 표쥰정규분포에서 10개를 뽑음 "
],
"id": "9b77680f-93dd-4dbf-81ca-61e0c6a9b389"
},
{
"cell_type": "code",
"execution_count": 205,
"metadata": {},
"outputs": [],
"source": [
"np.random.rand(10) # 0~1사이에서 10개를 뽑음"
],
"id": "95492076-4b69-474c-b22b-73a3376af346"
},
{
"cell_type": "code",
"execution_count": 247,
"metadata": {},
"outputs": [],
"source": [
"np.random.randn(4).reshape(2,2) # 표준정규분포에서 4개를 뽑고 (2,2) ndarray로 형태변환 "
],
"id": "f633f72b-1b63-44d2-aef9-91e36fbba440"
},
{
"cell_type": "code",
"execution_count": 208,
"metadata": {},
"outputs": [],
"source": [
"np.random.rand(4).reshape(2,2) # 0~1 4개를 뽑고 (2,2) ndarray로 형태변환 "
],
"id": "ffe8f40d-18bc-4b08-817b-f03413e48cbc"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 행렬관련기능"
],
"id": "feb76cf1-a293-40e7-b3c6-45abec2f1c6d"
},
{
"cell_type": "code",
"execution_count": 249,
"metadata": {},
"outputs": [],
"source": [
"A=np.arange(4).reshape(2,2) \n",
"A"
],
"id": "642c8453-3cae-410c-a5db-b87bb5f5a4b7"
},
{
"cell_type": "code",
"execution_count": 250,
"metadata": {},
"outputs": [],
"source": [
"A.T # .T는 전치행렬을 구해줌 "
],
"id": "6f6f4580-bf90-42be-9638-163bb8a761df"
},
{
"cell_type": "code",
"execution_count": 221,
"metadata": {},
"outputs": [],
"source": [
"np.linalg.inv(A) # np.linalg.inv는 역행렬을 구해주는 함수 "
],
"id": "f1ad1530-4df0-49ee-af18-460fac01f585"
},
{
"cell_type": "code",
"execution_count": 254,
"metadata": {},
"outputs": [],
"source": [
"A @ np.linalg.inv(A) # @는 행렬곱을 수행 "
],
"id": "4e90a130-c711-49c5-9d30-2399bacac8e4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 넘파이 공부 3단계: 차원\n",
"\n",
"## 2차원 배열과 연립 1차 방정식\n",
"\n",
"`-` 아래의 연립방정식 고려\n",
"\n",
"$\\begin{cases} y+z+w = 3 \\\\ x+z+w = 3 \\\\ x+y+w = 3 \\\\ x+y+z = 3 \\end{cases}$\n",
"\n",
"`-` 행렬표현?\n",
"\n",
"$\\begin{bmatrix} 0 & 1 & 1 & 1 \\\\ 1 & 0 & 1 & 1 \\\\ 1 & 1 & 0 & 1 \\\\ 1 & 1 & 1 & 0 \\end{bmatrix} \\begin{bmatrix} x \\\\ y \\\\ z \\\\ w \\end{bmatrix} = \\begin{bmatrix} 3 \\\\ 3 \\\\ 3 \\\\ 3 \\end{bmatrix}$\n",
"\n",
"`-` 풀이"
],
"id": "05dc946d-7cdd-4dce-b3e9-a00aa4a263a8"
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[0,1,1,1],[1,0,1,1],[1,1,0,1],[1,1,1,0]])\n",
"A"
],
"id": "fcb12768-c763-4243-be10-5405ba738336"
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"b= np.array([3,3,3,3]).reshape(4,1)\n",
"b"
],
"id": "2c54484b-90a0-42cc-9c79-175feb552791"
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"np.linalg.inv(A) @ b "
],
"id": "c5e4dd47-25d5-49b7-a554-68b062458ead"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 다른풀이\n",
"\n",
"b를 아래와 같이 만들어도 된다."
],
"id": "159f3abb-0685-459e-ba43-a2b782535c3c"
},
{
"cell_type": "code",
"execution_count": 267,
"metadata": {},
"outputs": [],
"source": [
"b=np.array([3,3,3,3])\n",
"b"
],
"id": "b9bb8c56-d5f7-46a8-8be6-e5939333b607"
},
{
"cell_type": "code",
"execution_count": 268,
"metadata": {},
"outputs": [],
"source": [
"b.shape # b.shape은 길이가 1인 튜플로 나온다. "
],
"id": "74e32084-28f9-4921-8a55-4e0eb4030104"
},
{
"cell_type": "code",
"execution_count": 269,
"metadata": {},
"outputs": [],
"source": [
"np.linalg.inv(A) @ b "
],
"id": "03d60158-22d5-4d2f-b5d0-92c3d68a2669"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## `@`의 유연성\n",
"\n",
"`-` 엄밀하게는 아래의 행렬곱이 가능하다.\n",
"\n",
"- (2,2) @ (2,1) =\\> (2,1)\n",
"- (1,2) @ (2,2) =\\> (1,2)"
],
"id": "e96ac1c6-d2e4-48e8-9258-29a852fcdd56"
},
{
"cell_type": "code",
"execution_count": 372,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([1,2,3,4]).reshape(2,2) \n",
"b = np.array([1,2]).reshape(2,1) \n",
"A@b"
],
"id": "534be66a-89cc-4df9-b96d-e57c772926c9"
},
{
"cell_type": "code",
"execution_count": 271,
"metadata": {},
"outputs": [],
"source": [
"A.shape, b.shape, (A@b).shape"
],
"id": "dd08237d-5a3f-4430-af12-90f34d42fd6c"
},
{
"cell_type": "code",
"execution_count": 380,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([1,2,3,4]).reshape(2,2) \n",
"b = np.array([1,2]).reshape(1,2) \n",
"b@A "
],
"id": "91641a15-4816-4bea-a44a-a9ca503ada37"
},
{
"cell_type": "code",
"execution_count": 381,
"metadata": {},
"outputs": [],
"source": [
"A.shape, b.shape, (b@A).shape"
],
"id": "b469b513-1374-430d-9499-87a7717b230e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 당연히 아래는 성립안한다."
],
"id": "af72dcbe-4839-4670-96a6-b9c1ce927683"
},
{
"cell_type": "code",
"execution_count": 382,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([1,2,3,4]).reshape(2,2) \n",
"b = np.array([1,2]).reshape(2,1) \n",
"b@A"
],
"id": "30d5b5b1-15c0-4286-ba98-b55e739490f6"
},
{
"cell_type": "code",
"execution_count": 385,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([1,2,3,4]).reshape(2,2) \n",
"b = np.array([1,2]).reshape(1,2) \n",
"A@b"
],
"id": "2154c269-ab58-43aa-af83-a5be388a0de4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 아래는 어떨까? 계산가능할까? $\\to$ 모두 계산가능!\n",
"\n",
"- (2,) @ (2,2) = (2,)\n",
"- (2,2) @ (2,) = (2,)"
],
"id": "e1f6084e-200d-485e-8151-13b4f5d39b5d"
},
{
"cell_type": "code",
"execution_count": 386,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([1,2,3,4]).reshape(2,2)\n",
"b = np.array([1,2]) \n",
"A@b"
],
"id": "fe6cc482-c2b7-49d5-b305-d0b127eaf846"
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"A.shape, b.shape, (A@b).shape "
],
"id": "a8715bc2-d8a8-4d06-97a7-648a8c24105d"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- b를 마치 (2,1)처럼 해석하여 행렬곱하고 결과는 다시 (2,) 로 만든것\n",
" 같다."
],
"id": "50597669-f246-4fb2-8b5d-2f3a59b7539d"
},
{
"cell_type": "code",
"execution_count": 387,
"metadata": {},
"outputs": [],
"source": [
"b@A"
],
"id": "46bf60ca-8595-4bb5-bc1f-c66baf383814"
},
{
"cell_type": "code",
"execution_count": 289,
"metadata": {},
"outputs": [],
"source": [
"A.shape, b.shape, (b@A).shape "
],
"id": "339a4fff-0d3a-407a-ba05-fefc3e18b85c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 이때는 $b$를 마치 (1,2)처럼 해석하여 행렬곱하고 결과는 다시 (2,)로\n",
" 만든것 같다.\n",
"\n",
"`-` 아래는 어떠할까?"
],
"id": "8a79bfed-c440-450a-8525-9b412d58dfb6"
},
{
"cell_type": "code",
"execution_count": 394,
"metadata": {},
"outputs": [],
"source": [
"b1 = np.array([1,2,3,4]) \n",
"b2 = np.array([1,2,3,4]) \n",
"b1@b2 "
],
"id": "7e56ef62-0454-4744-83d8-140c629dc794"
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"b1.shape, b2.shape, (b1@b2).shape "
],
"id": "12b15df4-7a05-4da0-83e4-0b3ce798b5b0"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- (1,4) @ (4,1) = (1,1) 로 생각\n",
"\n",
"`-` 즉 위는 아래와 같이 해석하고 행렬곱한것과 결과가 같다."
],
"id": "1d039a0f-727e-44e8-904c-a673db8e930c"
},
{
"cell_type": "code",
"execution_count": 395,
"metadata": {},
"outputs": [],
"source": [
"b1 = np.array([1,2,3,4]).reshape(1,4) \n",
"b2 = np.array([1,2,3,4]).reshape(4,1) \n",
"b1@b2 "
],
"id": "ad90a7bc-6eae-46df-983b-42702c08f914"
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"b1.shape, b2.shape, (b1@b2).shape "
],
"id": "85e0b3e1-33f6-4bf7-a1f0-40948c62f6cd"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 때로는 (4,1) @ (1,4)와 같은 계산결과를 얻고 싶을 수 있는데 이때는\n",
"차원을 명시해야함"
],
"id": "6b82ed51-2444-48db-971b-a61bdd5c1f56"
},
{
"cell_type": "code",
"execution_count": 396,
"metadata": {},
"outputs": [],
"source": [
"b1 = np.array([1,2,3,4]).reshape(4,1) \n",
"b2 = np.array([1,2,3,4]).reshape(1,4) \n",
"b1@b2 "
],
"id": "48657603-5709-451b-8835-d7e0fd8639fc"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**참고: 평균을 구하는 신기한 코드**"
],
"id": "f401b6bc-f963-402e-9d4f-1b1b14ccb070"
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [],
"source": [
"x = np.random.randn(100)"
],
"id": "a7b03e21-35a5-4aec-85b8-8ecc42b370d8"
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [],
"source": [
"np.array([1/100]*100) @ x ## x의 평균을 구하라는 의미"
],
"id": "311dda6e-6771-4587-a847-71bc0aea625e"
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [],
"source": [
"np.mean(x)"
],
"id": "ff94e0ad-a67d-4ef5-84dc-ab3170631fbb"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HW\n",
"\n",
"`1`. shape이 ()인 numpy array를 만들어라. (즉 차원이 0인 np.array를\n",
"만들어라) // 2022-중간고사-1-(19)\n",
"\n",
"(풀이)"
],
"id": "7fd6b709-31c0-4fac-ac67-2391d2c5c769"
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"a= np.array(0)\n",
"a.shape"
],
"id": "2c931a66-68a4-4fac-bf3b-c1f10125f602"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`2` a의 모든 원소에 1을 더하는 코드를 작성하라. // 2022-중간고사-1-(21)\n",
"\n",
"``` python\n",
"a=[1,3,2,5,-3,3,8,2,3,1] \n",
"```\n",
"\n",
"(풀이)"
],
"id": "3252ec35-735d-4345-aeeb-4e0a1353acf2"
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"a=[1,3,2,5,-3,3,8,2,3,1]\n",
"np.array(a)+1"
],
"id": "fe036db4-db61-4eec-bffe-175d67a278bd"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`3` 아래와 같은 수열을 생생성하라. // 2022-중간고사-1-(23)\n",
"\n",
" 0,1,2,3,4,5,...,99 \n",
"\n",
"위의 수열에서 1,4,7,10,13,… 번째의 원소를 뽑아라. (첫번째 원소는 0이다)\n",
"\n",
"(풀이)"
],
"id": "e8a3e7d6-0974-44c5-9124-fa3218994aeb"
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"np.arange(100)[::3]"
],
"id": "de7e582a-d54b-404a-8eb6-2afaba9c92d8"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`4`. shape이 (5,5)인 단위행렬을 만들어라. // 2022-중간고사-1-(20)\n",
"\n",
"(풀이)"
],
"id": "19e1784f-35bb-4c8a-9c2b-da72ecd965ca"
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"np.eye(5)"
],
"id": "869af8fe-cc2b-465a-a3ea-9218c86f1857"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`5`. numpy를 이용하여 아래의 역행렬을 구하라. // 2022-중간고사-1-(24)\n",
"\n",
"$$\\begin{bmatrix}\n",
"1& 0 \\\\\n",
"0& 3\n",
"\\end{bmatrix}$$\n",
"\n",
"(풀이)"
],
"id": "2e1fbf51-18f3-4a83-a748-7723b7a94506"
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"A = np.array([[1,0],[0,3]])\n",
"np.linalg.inv(A)"
],
"id": "62bb878a-d1aa-4bf5-a945-14a9ec2f1812"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`6`. 본인의 학번으로 랜덤시드를 설정하고 표준정규분포에서 1000개의\n",
"난수를 생성하라. // 2022-중간고사-1-(34)\n",
"\n",
"``` python\n",
"# 학번이 2021-43052 라면 아래와 같이 시드설정 \n",
"np.random.seed(202143052)\n",
"```\n",
"\n",
"(풀이)"
],
"id": "1e13876a-e10f-49a4-a902-9eac59fa2830"
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(202143052)\n",
"x = np.random.randn(1000)"
],
"id": "2f083326-54dd-43d4-baaf-f028736677a9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`7`. `6`의 결과로 나온 1000개의 난수중 1.96보다 크거나 -1.96보다 작은\n",
"작은값은 모두 몇개인가?\n",
"\n",
"(풀이)"
],
"id": "b731b019-bd6c-4794-a5fb-5e908e5313a2"
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"sum(x>1.96) + sum(x<-1.96)"
],
"id": "7fd1d0f9-2dff-4c86-8977-e4111c413699"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`8`. 아래와 같은 배열을 선언하라. // 2022-중간고사-1-(38)"
],
"id": "8befccf5-fec9-4eae-b4fa-a7211185c033"
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"a=np.arange(12).reshape(3,4)\n",
"a"
],
"id": "37262d80-b95f-474d-a5bc-3e42cd7a6d86"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"차원의 수를 유지하면서 1열을 추출하는 코드를 작성하라. 즉 결과가 아래와\n",
"같이 나오도록 하라.\n",
"\n",
" array([[0],\n",
" [4],\n",
" [8]])\n",
"\n",
"(풀이)"
],
"id": "a80da231-6084-4085-8cbb-a3670aa0c740"
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"a[:,[0]]"
],
"id": "53b38aa6-00d2-4874-94b4-aa49cba7010b"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`9`. 아래의 코드를 관찰하라. // 2022-중간고사-1-(40)"
],
"id": "82fd61f4-1ec7-4bce-b1fb-d6ab972f28e2"
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(43052)\n",
"a=np.random.randint(low=20,high=25,size=(10,10))"
],
"id": "73129713-cc1c-4d82-bf00-716502bb0468"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"a에서 20,21,22는 각각 몇개씩 있는가?\n",
"\n",
"**note** 공부를 위하여 `np.random.randint()`의 기능을 chatGPT등으로\n",
"확인해 볼 것\n",
"\n",
"(풀이)"
],
"id": "1c6f8ddb-7b1f-4c41-b53b-2e799342a886"
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"(a==20).sum(), (a==21).sum(), (a==22).sum()"
],
"id": "58112de7-b624-40c8-985a-92e3c46ac7b1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`10`. 아래의 코드를 관찰하라."
],
"id": "0a86b560-31ad-4a65-914b-bef593e8f88b"
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"lst = [[i==j for i in range(5)] for j in range(5)]\n",
"lst"
],
"id": "a5135cb2-1de0-490b-82a7-8d2cf9940bb5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"lst를 이용하여 shape이 (5,5)인 단위행렬을 만들어라."
],
"id": "c60e2d0d-e83e-4b02-903a-e8b3d30d0f75"
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 출력결과는 아래와 같이 나와야 한다. "
],
"id": "e870dfc5-d523-4b17-ab00-66b886d22669"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "7290576c-8efc-4c2c-898b-ae9f1277db51"
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"np.array([[i==j for i in range(5)] for j in range(5)])*1.0"
],
"id": "26e179f1-a261-4187-8db8-2bcebb4ba6b6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`12`. 적당한 코드를 이용하여 아래와 같은 리스트를 생성하라."
],
"id": "ef26d11b-31c3-4aa1-b4c4-94b1d51433b8"
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"lst"
],
"id": "abdd5ec4-0920-43de-ac16-ef1a7dc9c430"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"이를 이용하여 아래와 같은 행렬을 선언하라."
],
"id": "6c34d3d8-fd52-4c30-8187-3f2902f74d65"
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"arr "
],
"id": "d7582a15-4043-4773-a93a-aa143c6a8c9f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "b8a5e053-c2eb-46d8-8988-5a98e76d82d0"
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"np.array([[i+j for i in range(5)] for j in range(5)] )"
],
"id": "7fbc9c6a-5689-4c8b-b080-db663dd6e4b6"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`13`. 적당한 코드를 이용하여 아래와 같은 행렬을 선언하라."
],
"id": "c5254e76-363f-421d-a917-32a1f0cde52e"
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"# 출력결과는 아래와 같아야 한다."
],
"id": "40851a95-0678-40cf-a86b-ffb4667f8669"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "252b9f15-a884-4c68-b921-32fe54bb5255"
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [],
"source": [
"np.array([[abs(i-j)<2 for i in range(10)] for j in range(10)])*1"
],
"id": "b8025317-3a1d-4d05-9cb8-725615bc3db5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`14`. 아래와 같은 배열이 있다고 하자. // 2022-중간고사-1-(31)"
],
"id": "82569f0d-25e1-43c9-a866-29ee4e0c458d"
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"a=np.array([1,2,3,4,5])\n",
"b=np.array([3,2,1,1,2])"
],
"id": "4dff41f9-7076-4c9b-9299-f51e885c4ce9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"numpy의 `@` 연산자를 이용하여 $\\sum_{i=1}^{5}a_ib_i$를 계산하라.\n",
"\n",
"(풀이)"
],
"id": "815d71bf-2fdf-47aa-a3ab-90fb1556afbf"
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"a@b"
],
"id": "0f5a5f4e-e84e-40da-a5b0-1cbd44bdaf76"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`15`. 아래와 같은 배열을 생성하라. // 2022-중간고사-1-(32)\n",
"\n",
"``` python\n",
"x=np.random.randn(100)\n",
"```\n",
"\n",
"numpy의 `@`연산자를 이용하여 $\\sum_{i=1}^{100}x_i^2$을 계산하라.\n",
"\n",
"(풀이)"
],
"id": "1d941d09-a0fd-46e1-bb3b-d9b566c8a3d5"
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"x@x"
],
"id": "6b2b53d9-9f9a-4ed4-b61c-30e321e39689"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`16`. 아래와 같은 배열을 생성하라. // 2022-중간고사-1-(33)\n",
"\n",
"``` python\n",
"a=np.array([1/100]*100)\n",
"x=np.random.randn(100)\n",
"```\n",
"\n",
"numpy의 `@`연산자를 이용하여 $\\frac{1}{n}\\sum_{i=1}^{n}x_i$를 계산하라.\n",
"\n",
"(풀이)"
],
"id": "0fbf6cf6-4ebf-4871-a93e-ad85a219466a"
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"x@x/100 "
],
"id": "0b040c20-4383-4550-b043-9b75a7e8b268"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`17`. 성공확률이 45%인 시행을 100번 반복하여 성공하면 True, 실패하면\n",
"False를 기록하는 난수열을 생성하라.\n",
"\n",
"**hint:**"
],
"id": "2b074eba-9a50-48a8-8ff1-c92d8b99ef0f"
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"np.random.rand() # 0~1사이의 값이 임의로 생성"
],
"id": "86a54fc4-6a8c-4249-9c6e-f0f3c9888265"
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [],
"source": [
"np.random.rand() < 0.45 # 0~1사이에 랜덤으로 생성된 값이 0.45보다 작다면 True를 리턴 "
],
"id": "653d2478-eb3b-4e54-9396-ecb1fea21687"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(풀이)"
],
"id": "6796af23-b5a3-4d4e-bc00-207b8b9d788c"
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"np.random.rand(100)<0.45"
],
"id": "7eadc854-423d-4b4f-9a00-980b40666b1c"
}
],
"nbformat": 4,
"nbformat_minor": 5,
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3 (ipykernel)",
"language": "python"
},
"language_info": {
"name": "python",
"codemirror_mode": {
"name": "ipython",
"version": "3"
},
"file_extension": ".py",
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.16"
}
}
}