{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 06wk-2: Numpy (2)\n",
"\n",
"최규빈 \n",
"2023-04-12\n",
"\n",
"
\n",
"\n",
"# 강의영상\n",
"\n",
"> youtube:\n",
"> \n",
"\n",
"# import"
],
"id": "fb7a7723-2298-4b60-9db3-889313565bf8"
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
],
"id": "3e137183-e755-43bc-adaf-1f4dba948ffb"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 넘파이 공부 4단계: 축\n",
"\n",
"## np.concatenate\n",
"\n",
"`-` 1차원: concat의 기본예제 (쓸모 없어보임)"
],
"id": "b1cb3707-937f-450e-b81c-90a40af71704"
},
{
"cell_type": "code",
"execution_count": 145,
"metadata": {},
"outputs": [],
"source": [
"a= np.array([1,2,3]) \n",
"b= -a "
],
"id": "784b5a29-9c7e-4091-bb60-00165eb3c956"
},
{
"cell_type": "code",
"execution_count": 146,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a,b])"
],
"id": "2ff9e6ae-d3fc-46d6-aba7-e7545d770ef2"
},
{
"cell_type": "code",
"execution_count": 147,
"metadata": {},
"outputs": [],
"source": [
"np.array(list(a)+list(b))"
],
"id": "1ccbff05-8fee-4e53-bbc8-0a1f680ba57f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 2차원: 이건 좀 의미가 있어보임"
],
"id": "a6326e28-1d6d-433d-be4b-aedaf3cc0a12"
},
{
"cell_type": "code",
"execution_count": 148,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([1,2,3,4]).reshape(2,2)\n",
"b = -a "
],
"id": "320eb487-5720-4812-846b-03b2a7cf91ea"
},
{
"cell_type": "code",
"execution_count": 149,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a,b])"
],
"id": "197e1a9c-4b6f-4948-93c4-0fee4cae8bac"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"만약에 a,b를 좌우로 붙이고 싶다면? axis=1이라고 옵션을 주면 된다."
],
"id": "1a6a5d0b-f3d4-48d1-b5c6-3f574387a78f"
},
{
"cell_type": "code",
"execution_count": 150,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a,b],axis=1) "
],
"id": "ff082039-05e0-460f-b336-a4fe5c779efc"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- axis=1의 의미는 무엇일까?\n",
"\n",
"`-` axis=1 의 의미를 파악하기 전에, axis=1만 가능한것인지 알아보자.\n",
"\n",
"(시도1)"
],
"id": "2f29b9c4-7deb-4395-97c9-4987e1c86d24"
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a,b],axis=0)\n",
"# np.concatenate([a,b]) 와 같음 \n",
"# np.concatenate([a,b])은 np.concatenate([a,b],axis=0)의 생략버전이라 유추가능"
],
"id": "61116523-870e-4008-b1bb-7dbb30ff0ee3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- axis=0 도 가능!!\n",
"\n",
"(시도2)"
],
"id": "11c0ed6a-6d67-453b-829b-77cffbac9d99"
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a,b],axis=2)\n",
"# 에러가 난다. "
],
"id": "8d0e64e5-2143-4b61-8597-fb99f86ecd85"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- axis=1은 불가능!!\n",
"\n",
"`-` axis의 의미를 알아보자."
],
"id": "65c35a6a-4ef7-45dd-9899-c119790f95c9"
},
{
"cell_type": "code",
"execution_count": 151,
"metadata": {},
"outputs": [],
"source": [
"a= np.array([1,2,3,4,5,6]).reshape(3,2)\n",
"b= -a "
],
"id": "7d9d17a2-6b38-475a-9851-6cf51b58a4e3"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(예시1)"
],
"id": "dd94c1c3-baba-4f65-8d6c-02be331a810e"
},
{
"cell_type": "code",
"execution_count": 152,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape, np.concatenate([a,b],axis=0).shape"
],
"id": "c34c58e3-5745-494a-8c5a-61cb7a3e777f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 관찰: `(3,2)` concat `(3,2)` -\\> `(6,2)` // 첫번째 숫자가 바뀜 \\<==\n",
" axis=0\n",
"\n",
"(예시2)"
],
"id": "41e89350-eb89-4f5b-a6cd-57f70605b311"
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape, np.concatenate([a,b],axis=1).shape"
],
"id": "bc702826-f0c0-4ca1-8878-4fcf64c66d55"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 관찰: `(3,2)` concat `(3,2)` -\\> `(3,4)` // 두번째 숫자가 바뀜 \\<==\n",
" axis=1\n",
"\n",
"(예시3)"
],
"id": "6bb3b00c-e1fb-4135-8117-2e1543e67798"
},
{
"cell_type": "code",
"execution_count": 154,
"metadata": {},
"outputs": [],
"source": [
"a= np.arange(24).reshape(2,3,4) \n",
"b= -a "
],
"id": "59fc3b0d-9166-47b4-9bcf-69ae0b161555"
},
{
"cell_type": "code",
"execution_count": 155,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape,np.concatenate([a,b],axis=0).shape"
],
"id": "2b1eda2b-7ed1-4923-9338-02343bb12b14"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 유추: `(2,3,4)` concat `(2,3,4)` -\\> `(4,3,4)` // 첫번째 숫자가 바뀔\n",
" 것 같았는데 실제로 그러함 \\<== axis=0\n",
"\n",
"(예시4)"
],
"id": "d5181eee-9f74-4df6-9a18-795f6d657349"
},
{
"cell_type": "code",
"execution_count": 156,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape,np.concatenate([a,b],axis=1).shape"
],
"id": "cbd90f57-f736-4950-a038-9a3014e73703"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `(2,3,4)` concat `(2,3,4)` -\\> `(2,6,4)` // 두번째 숫자가 바뀜 \\<==\n",
" axis=1\n",
"\n",
"(예시5)"
],
"id": "1cbfc55a-b595-499b-b8b8-f4b664cb7065"
},
{
"cell_type": "code",
"execution_count": 157,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape,np.concatenate([a,b],axis=2).shape"
],
"id": "de40ffda-13ac-4a01-96a3-eebbc181447e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `(2,3,4)` concat `(2,3,4)` -\\> `(2,3,8)` // 세번째 숫자가 바뀜 \\<==\n",
" axis=2\n",
"\n",
"(예시5)"
],
"id": "20cb3fe2-a4b4-4c65-bc5e-99998218c56a"
},
{
"cell_type": "code",
"execution_count": 158,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape,np.concatenate([a,b],axis=-1).shape"
],
"id": "cd46ec59-c9a8-48ff-85b0-a04ddc2bacbd"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `(2,3,4)` concat `(2,3,4)` -\\> `(2,3,8)` // 마지막 숫자가 바뀜 \\<==\n",
" axis=-1\n",
"\n",
"(예시6)"
],
"id": "22183d56-bd38-41dc-a466-991cb29b58c7"
},
{
"cell_type": "code",
"execution_count": 159,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape,np.concatenate([a,b],axis=-2).shape"
],
"id": "6be3e6f9-23ad-4c80-b0cb-a58c23f28e46"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `(2,3,4)` concat `(2,3,4)` -\\> `(2,6,4)` // 마지막에서 두번째 숫자가\n",
" 바뀜 \\<== axis=-2\n",
"\n",
"(예시7)"
],
"id": "e4a63024-5275-4596-a52c-142c4f9f6dee"
},
{
"cell_type": "code",
"execution_count": 160,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape,np.concatenate([a,b],axis=-3).shape"
],
"id": "6aee3d80-3f11-449b-9f57-f4393f0ebe0a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `(2,3,4)` concat `(2,3,4)` -\\> `(4,3,4)` // 마지막에서 세번째 숫자가\n",
" 바뀜 \\<== axis=-3\n",
"\n",
"(예시8)"
],
"id": "862bc2be-d488-450f-a925-5327235e4272"
},
{
"cell_type": "code",
"execution_count": 161,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape,np.concatenate([a,b],axis=-4).shape"
],
"id": "fc619570-2df2-4f65-80be-b73ce8e2babd"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `(2,3,4)` concat `(2,3,4)` -\\> `???` // 마지막에서 네번째 숫자는\n",
" 없어서 에러남 \\<== axis=-4\n",
"\n",
"(예시9)"
],
"id": "fb16662a-0be6-4a81-8dfe-71b12d6786de"
},
{
"cell_type": "code",
"execution_count": 162,
"metadata": {},
"outputs": [],
"source": [
"a.shape,b.shape,np.concatenate([a,b],axis=3).shape"
],
"id": "b442a747-d2ff-4cb6-93af-69861af46fe2"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `(2,3,4)` concat `(2,3,4)` -\\> `???` // 네번째 숫자는 없어서 에러남\n",
" \\<== axis=3\n",
"\n",
"## np.stack\n",
"\n",
"`-` 아래의 자료를 관찰하자."
],
"id": "211d4519-88c4-47e0-b513-7a74638f05e1"
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([1,2,3])\n",
"b = -a "
],
"id": "aae19ae7-3772-4730-ab5d-a5fe8da01236"
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"a,b"
],
"id": "5daddd50-aedf-4cb6-aab0-1f739876419c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 혹시 a,b를 위아래로 쌓아서 결과를 `(2,3)` 매트릭스로 만들 수 있을까?"
],
"id": "17ee2f39-c111-4981-9fe5-50be504fcece"
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a,b]) # 실패1"
],
"id": "4189f2e4-77b1-47b9-aad4-ccf22044d361"
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a,b],axis=1) # 실패2"
],
"id": "1e19584c-819f-49f9-958a-3364498ef7e9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 실패이유를 분석: `(3,) concat (3,)` 으로 `(2,3)` 를 만들려고 해서\n",
"불가능했음.\n",
"\n",
"- 전략: reshape을 이용하여 `(3,), (3,) => (1,3), (1,3)` 으로 변경후\n",
" concat을 하면 가능할듯"
],
"id": "2933cbd9-e59e-4c0c-b662-e1f94a49caad"
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a.reshape(1,3),b.reshape(1,3)],axis=0) # 성공"
],
"id": "dc1bf3e0-0991-4011-868e-d77c113dd875"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"또 다른 방법!"
],
"id": "c6a73f10-ae40-470e-8f8c-1c5215b69c54"
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=0)"
],
"id": "15d3b137-d528-4ab9-abb4-36d76898cebc"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 이번에는 a,b를 좌우로 쌓아서 결과를 `(3,2)` 매트릭스로를 만들어\n",
"보자."
],
"id": "8d57f11f-6301-451d-abc4-e6a37037628e"
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a.reshape(3,1),b.reshape(3,1)],axis=1)"
],
"id": "1467fe76-f709-4150-87bb-ce13d31db655"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"또 다른 방법!"
],
"id": "82d68f8c-8538-4035-9304-e36ecf8bbd6f"
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=1)"
],
"id": "0e7824b7-4147-461b-8e2f-53111294375a"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 요약: stack은 `axis=` 위치에 축을 추가하고 concat을 한 것 이라\n",
"이해할 수 있다.\n",
"\n",
"**강의에서는 하지 않은 예제**"
],
"id": "6f899881-370d-46bb-b353-d59602029abb"
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [],
"source": [
"a=np.arange(3*4*5).reshape(3,4,5) \n",
"b=-a"
],
"id": "30c2c3b1-7e6d-4e58-b0ce-f1e61b376cc4"
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [],
"source": [
"a.shape, b.shape"
],
"id": "b01a838c-7517-44f5-bf69-f5cd06b7b194"
},
{
"cell_type": "code",
"execution_count": 170,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=0).shape # (3,4,5) => (1,3,4,5) // 첫 위치에 축이 추가되고 스택 "
],
"id": "bf5b5d88-58ac-47eb-ad6f-d355ec08c802"
},
{
"cell_type": "code",
"execution_count": 174,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=1).shape # (3,4,5) => (3,1,4,5) // 두번째 위치에 축이 추가되고 스택 "
],
"id": "0f7523ed-8964-4c1f-9bcc-a1f502c75e1f"
},
{
"cell_type": "code",
"execution_count": 175,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=2).shape # (3,4,5) => (3,4,1,5) // 세번째 위치에 축이 추가되고 스택 "
],
"id": "0bb51cb0-94c5-4a62-be7d-e06b06e6f3bf"
},
{
"cell_type": "code",
"execution_count": 179,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=3).shape # (3,4,5) => (3,4,5,1) // 네번째 위치에 축이 추가되고 스택 "
],
"id": "fbf2854c-26b3-4e47-9021-eac93220ee37"
},
{
"cell_type": "code",
"execution_count": 181,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=-1).shape # axis=-1 <=> axis=3 "
],
"id": "912a44ca-5f4f-45fe-b61f-ca596f8144be"
},
{
"cell_type": "code",
"execution_count": 182,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=-2).shape # axis=-2 <=> axis=2"
],
"id": "f4af567e-5a87-41b3-890f-8da9df19977e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***np.concatenate*** 는 축의 총 갯수를 유지하면서 결합, ***np.stack***은\n",
"축의 갯수를 하나 증가시키면서 결합\n",
"\n",
"## sum\n",
"\n",
"`-` 1차원 array의 sum"
],
"id": "d44a4057-ad38-47f5-9c56-8a3087dc4d9a"
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([1,2,3,4])\n",
"a"
],
"id": "159152c0-c10d-4a53-ab92-7a4fe5086a9c"
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [],
"source": [
"a.sum()"
],
"id": "b869b5a9-9ea5-4356-8f41-4f4086d59783"
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [],
"source": [
"np.sum(a)"
],
"id": "19c11236-f5c4-44b7-9856-de5d523834a4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` 2차원 array일 경우"
],
"id": "3098434a-4ce4-416d-ab70-bb8e9d4aa7c8"
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [],
"source": [
"a= np.arange(4*3).reshape(4,3)\n",
"a"
],
"id": "454c10d9-c470-433c-a853-a2ffc64576ff"
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [],
"source": [
"a.sum(axis=-1)"
],
"id": "c657fd6f-9ca3-4bd7-b942-b16221804ea4"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 마지막 축이 삭제됨\n",
"\n",
"`-` 그렇다면?"
],
"id": "cb904f14-8cd3-4f6c-a941-848e26979d27"
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [],
"source": [
"a.sum(axis=(0,1))"
],
"id": "ceebadb7-3c0c-42a8-a4a8-0f4031217b2f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 첫번째 축과 두번째 축이 삭제됨\n",
"\n",
"`-` 연습 (강의에서 하지 않은 예제)"
],
"id": "62c6f661-8ade-40f8-8935-929819a307f1"
},
{
"cell_type": "code",
"execution_count": 197,
"metadata": {},
"outputs": [],
"source": [
"a=np.array(range(10)).reshape(5,2) \n",
"a"
],
"id": "7abb3fdc-6b7b-4de5-84b4-80e9f9eb9e63"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(문제1) 1열의 합, 2열의 합을 계산하고 싶다면?\n",
"\n",
"(풀이) 차원이 (5,2) =\\> (2,) 로 나와야 한다. (그럼 첫번째 축이\n",
"삭제되어야 하네?)"
],
"id": "9c203ad0-85ae-4370-a8f6-c0778ab37e93"
},
{
"cell_type": "code",
"execution_count": 198,
"metadata": {},
"outputs": [],
"source": [
"a.sum(axis=0)"
],
"id": "95c07915-507c-4c52-8d72-48009ddd74be"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(문제2) 1행의 합, 2행의 합, … , 5행의 합을 계산하고 싶다면?\n",
"\n",
"(풀이) 차원이 (5,2) =\\> (5,)로 나와야 한다. (그럼 두번째 축이 삭제되어야\n",
"하네?)"
],
"id": "380617e8-40f7-4fa6-96ef-870dec7ce378"
},
{
"cell_type": "code",
"execution_count": 199,
"metadata": {},
"outputs": [],
"source": [
"a.sum(axis=1)"
],
"id": "1dcf9442-af55-45e1-b409-902b60842df5"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(문제3) a의 모든원소의 합을 계산하고 싶다면?\n",
"\n",
"(풀이) 차원이 (5,2) =\\> () 로 나와야 한다. (첫번째축, 두번째축이 모두\n",
"삭제되어야 하네?)"
],
"id": "9c25391d-5874-45b4-bf98-2f41a35f2bde"
},
{
"cell_type": "code",
"execution_count": 201,
"metadata": {},
"outputs": [],
"source": [
"a.sum(axis=(0,1))"
],
"id": "2fb7ae30-d7fb-4683-a50b-1688907fef7a"
},
{
"cell_type": "code",
"execution_count": 203,
"metadata": {},
"outputs": [],
"source": [
"a.sum() # 즉 a.sum(axis=(0,1))이 디폴트값임 "
],
"id": "69b3cbcf-1a9b-4a79-892b-7914d76e16d1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## mean, std, max, min, prod\n",
"\n",
"`-` mean, std, max, min, prod 모두 sum과 유사한 논리임\n",
"\n",
"`-` 평균"
],
"id": "6ee4fd30-5033-47a9-9654-1663c678d726"
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [],
"source": [
"a = np.arange(3*5*5).reshape(3,5,5)\n",
"a"
],
"id": "11f83493-6bfd-479a-b087-edba5dffc561"
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [],
"source": [
"a.mean(axis=0)"
],
"id": "51a9ef53-6adb-48c2-8e40-94145d4a3306"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `(3,5,5) => (5,5)`"
],
"id": "681f1187-9439-420f-87db-bb4bf77dd82c"
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [],
"source": [
"a.mean(axis=(1,2))"
],
"id": "ee033dda-f233-495b-8488-696830bfe2c1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- `(3,5,5) => (3,)`\n",
"\n",
"`-` std"
],
"id": "f80131f2-a97a-4c68-ab27-9745f163e3f1"
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([1,2,3,4,5,6]).reshape(3,2)\n",
"a"
],
"id": "cca87e90-48ee-4da3-8966-5fb6ede72c44"
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"a.std(axis=1)"
],
"id": "6dc7d504-bae6-48ae-b7c9-1280d74d00f1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**참고: 분모를 $n$이 아니라 $n-1$로 나누고 싶다면 아래와 같이\n",
"사용하라.**"
],
"id": "b6eb0657-5871-459c-bfc7-a8c4eda1e5ac"
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"a.std(axis=1,ddof=1)"
],
"id": "59ed331c-fe39-4bc4-851f-1591008b6a6e"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`-` max,min,prod"
],
"id": "7a57ead0-c459-4691-900d-474249fa624b"
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([1,2,3,4,5,6]).reshape(3,2)\n",
"a"
],
"id": "9e9b571a-acae-42f7-92b8-cefdd1656c4d"
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"a.max(axis=1)"
],
"id": "7536b2a6-7e7e-4a3a-8ff7-5f962aa0dc8e"
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"a.min(axis=1)"
],
"id": "f649b54b-651a-4bf3-825e-6f22a2773682"
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"a.prod(axis=1)"
],
"id": "ed0a5b29-2e2e-4746-a0e1-c814993302bd"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## argmax, argmin\n",
"\n",
"`-` 아래와 같은 행렬 `a`를 고려하자."
],
"id": "6fd1ed68-19f7-42c6-8f6a-cc8ba0002f9e"
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.rand(15).reshape(5,3)\n",
"a = np.stack([np.array(l)/l.sum() for l in a]) # 리스트는 5개의 원소가 있으며 각 원소는 차원이 (3,) => stack을 이용해 첫 위치에 축을 추가하여 (5,3) 매트릭스를 만듬 \n",
"a"
],
"id": "a7cf21db-4e2b-4776-a2bb-930ba5256b31"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- 편의상 이 행렬의 each-row 는 어떠한 이미지가 ‘개,고양이,사과’ 일\n",
" 확률을 의미한다고 상상하자.\n",
"\n",
"`-` 위의 `a`에서 최대값을 가지는 index를 리턴하는 함수 (=`np.argmax`)를\n",
"행별로 적용하려면?"
],
"id": "9efacd6e-1215-4f63-ba77-37903895a2a0"
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"np.argmax(a,axis=1)"
],
"id": "9ad1f0a3-7625-4ddd-a522-4b0e16d78d31"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# HW\n",
"\n",
"`1` – `6`. a,b가 아래와 같이 주어졌다고 하자."
],
"id": "b2797543-67b3-4d44-a18e-c48abd0b2cf4"
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"a=np.array([1]*10)\n",
"b=np.array([2]*10)"
],
"id": "409e6391-c522-4867-9964-2164e4f658c7"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`1`. a,b와 np.concatenate를 이용하여 아래와 같은 배열을 만들어라. //\n",
"2022-중간고사-1-(25)\n",
"\n",
" array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])\n",
"\n",
"(풀이)"
],
"id": "48640fc5-4016-44bc-88de-3d5157ba0d73"
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a,b])"
],
"id": "c95f1eb0-8736-4ec8-8d0d-1af4d8f11ad7"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`2`. a,b와 np.concatenate를 이용하여 아래와 같은 배열을 만들어라. //\n",
"2022-중간고사-1-(26)"
],
"id": "4377cc25-47cf-45b6-8e54-93fb9e66b551"
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"a=np.array([1]*10)\n",
"b=np.array([2]*10)"
],
"id": "2dfb208f-c380-44c7-bb4d-b799430bdc32"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" array([[1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [1],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2],\n",
" [2]])\n",
"\n",
"(풀이)"
],
"id": "84a253a1-cd44-46c9-a4e8-a36d42a96084"
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a.reshape(10,1),b.reshape(10,1)],axis=0)"
],
"id": "76fb92bf-4179-4e3c-9040-3b5aedf7ed71"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`3`. a,b와 np.concatenate를 이용하여 아래와 같은 배열을 만들어라. //\n",
"2022-중간고사-1-(27)\n",
"\n",
" array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]]\n",
"\n",
"(풀이)"
],
"id": "3ad5c5dd-5068-44d4-8599-5bf70eab1a3b"
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a.reshape(1,10),b.reshape(1,10)],axis=0)"
],
"id": "d0098d63-70ca-488f-9a0c-359fa3df3aac"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`4`. a,b와 np.concatenate를 이용하여 아래와 같은 배열을 만들어라. //\n",
"2022-중간고사-1-(28)\n",
"\n",
" array([[1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2]])\n",
"\n",
"(풀이)"
],
"id": "68276172-2b06-4d07-b20e-8e5dd1d146cd"
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"np.concatenate([a.reshape(10,1),b.reshape(10,1)],axis=1)"
],
"id": "c48f7570-1d92-4999-b03e-3b88829d2bdc"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`5`. a,b와 np.stack을 이용하여 아래와 같은 배열을 만들어라. //\n",
"2022-중간고사-1-(29)\n",
"\n",
" array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
" [2, 2, 2, 2, 2, 2, 2, 2, 2, 2]]\n",
"\n",
"(풀이)"
],
"id": "dc1781f5-ed27-4057-9d75-43c73f56801f"
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=0)"
],
"id": "1b5b6fae-ed0c-4b4b-b538-b3d7e2cda2c1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`6`. a,b와 np.stack을 이용하여 아래와 같은 배열을 만들어라. //\n",
"2022-중간고사-1-(30)\n",
"\n",
" array([[1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2],\n",
" [1, 2]])\n",
"\n",
"(풀이)"
],
"id": "28ce637e-0f6a-45a9-9781-ec4917fc96bf"
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"np.stack([a,b],axis=1)"
],
"id": "59bf11b0-ecf9-413d-b47c-2d49410b3496"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**`7`–`10`**\n",
"\n",
"아래와 같은 매트릭스를 생성하라."
],
"id": "6197e275-ddc1-4404-8952-9ed5a801d3ae"
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(43052)\n",
"a=np.random.randn(10000).reshape(100,100)\n",
"a"
],
"id": "bd392904-5bad-49c0-9e53-d965f5e84c3c"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`7`. 각 행의 합을 구하라. 즉 1행의 합, 2행의 합, … 100행의 합을\n",
"계산하라. // 2022-중간고사-2-(11)\n",
"\n",
"- 1행의합 = 0.38342049 + 1.0841745 + … + 1.18701443\n",
"\n",
"(풀이)"
],
"id": "16c0a647-e1af-417e-9963-23a79704974f"
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"a.sum(axis=1)"
],
"id": "2460bc4d-0ecf-4e65-9b0c-341c1ead45d1"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`8`. `7`의 결과로 나온 배열의 표준편차를 구하라. // 2022-중간고사-1-(12)\n",
"\n",
"(풀이)"
],
"id": "69fd7a74-7900-4979-8e4e-7bd82e8a5880"
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"a.sum(axis=1).std(ddof=1)"
],
"id": "60e44528-4d22-4d90-8d89-bf57520cba3f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> `a.sum(axis=1).std()` 역시 정답으로 인정\n",
"\n",
"`9`. 각 열의 평균을 구하라. 즉 1열의 평균, 2열의 평균, … , 100열의\n",
"평균을 계산하라. // 2022-중간고사-1-(13)\n",
"\n",
"(풀이)"
],
"id": "6d4d7fce-b4bf-4469-ab73-aa5ff907e631"
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"a.sum(axis=0)"
],
"id": "839f7915-2e99-41eb-b636-8f4c1edac56f"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`10`. `9`의 결과로 나온 배열의 표준편차를 구하라. //\n",
"2022-중간고사-1-(14)\n",
"\n",
"(풀이)"
],
"id": "aa5df209-8652-4421-8e53-34e6942af07e"
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"a.sum(axis=0).std(ddof=1)"
],
"id": "01290361-b742-4177-ab71-58c33baf11c9"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"> `a.sum(axis=0).std()` 역시 정답으로 인정"
],
"id": "ccbe7a82-4381-4671-9e2b-7bcb23ca7c82"
}
],
"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"
}
}
}