Skip to content

Instantly share code, notes, and snippets.

@kabaoh
Created June 17, 2016 04:56
Show Gist options
  • Save kabaoh/94c485ce9f590d8bc55a5ad5d98a71c1 to your computer and use it in GitHub Desktop.
Save kabaoh/94c485ce9f590d8bc55a5ad5d98a71c1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"lst1 = [1,2,3]\n",
"lst2 = [1,2,3,4,5,6]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"6\n"
]
}
],
"source": [
"print(len(lst1))\n",
"print(len(lst2))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"arrlst1 = np.array(lst1)\n",
"arrlst2 = np.array(lst2)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"6\n"
]
}
],
"source": [
"print(len(arrlst1))\n",
"print(len(arrlst2))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"arrlst1とarrlst2をこのまま足し算しようとしても、次元が異なるため当然足し算はできない"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"ename": "ValueError",
"evalue": "operands could not be broadcast together with shapes (3,) (6,) ",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-25ff5121ceaa>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0marrlst1\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0marrlst2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: operands could not be broadcast together with shapes (3,) (6,) "
]
}
],
"source": [
"arrlst1 + arrlst2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"arrlst1を0埋めする"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"diff = abs(len(arrlst1) - len(arrlst2))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"newlist = np.lib.pad(arrlst1,(0,diff),\"constant\",constant_values = 0)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2, 3, 0, 0, 0])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"newlist"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"これで足し算ができる"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"array([2, 4, 6, 4, 5, 6])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"newlist + arrlst2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.5.1"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment