Skip to content

Instantly share code, notes, and snippets.

@tnantoka
Created March 30, 2020 12:57
Show Gist options
  • Save tnantoka/ddc5263498c6cbc2a11f53b016d6bf06 to your computer and use it in GitHub Desktop.
Save tnantoka/ddc5263498c6cbc2a11f53b016d6bf06 to your computer and use it in GitHub Desktop.
2x3と3x2の行列の内積
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]]\n",
"(2, 3)\n"
]
}
],
"source": [
"A = np.array([[1, 2, 3], [4, 5, 6]])\n",
"print(A)\n",
"A.shape"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2]\n",
" [3 4]\n",
" [5 6]]\n"
]
},
{
"data": {
"text/plain": [
"(3, 2)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"B = np.array([[1, 2], [3, 4], [5, 6]])\n",
"print(B)\n",
"B.shape"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[22, 28],\n",
" [49, 64]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"AB = np.dot(A, B)\n",
"print(AB)\n",
"AB.shape"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[22, 28]\n",
"[49, 64]\n"
]
}
],
"source": [
"print(f'[{1 * 1 + 2 * 3 + 3 * 5}, {1 * 2 + 2 * 4 + 3 * 6}]')\n",
"print(f'[{4 * 1 + 5 * 3 + 6 * 5}, {4 * 2 + 5 * 4 + 6 * 6}]')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment