Commit e13e2056 by Paktalin

Initial commit. Section 2

parents
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"#create a normal python list and numpy array\n",
"L = [1,2,3]\n",
"A = np.array([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Printing a python list\n",
"1\n",
"2\n",
"3\n",
"Printing a numpy array\n",
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"print(\"Printing a python list\")\n",
"for e in L:\n",
" print(e)\n",
"print(\"Printing a numpy array\")\n",
"for e in A:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Printing python list with appended value\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#adding new values to the list\n",
"L.append(4)\n",
"print(\"Printing python list with appended value\")\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Printing python list with added value\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#adding a new value to the python list by addition\n",
"L = L + [5]\n",
"print(\"Printing python list with added value\")\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'numpy.ndarray' object has no attribute 'append'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-20-3888ccf82513>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#appending and addition don't work for numpy arrays\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mA\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mA\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mA\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mA\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mA\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'append'"
]
}
],
"source": [
"#appending and addition don't work for numpy arrays\n",
"A.append(4)\n",
"A\n",
"A = A = [5]\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix addition with python lists\n"
]
},
{
"data": {
"text/plain": [
"[2, 4, 6, 8, 10]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Matrix addition with python lists\")\n",
"L2 = []\n",
"for e in L:\n",
" L2.append(e + e)\n",
"L2"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix addition with numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([2, 4, 6])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Matrix addition with numpy array\")\n",
"A + A"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix multiplication with numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([2, 4, 6])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Matrix multiplication with numpy array\")\n",
"2*A"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Multiplying list by a scalar results in doubling of the list\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Multiplying list by a scalar results in doubling of the list\")\n",
"2*L"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Squaring the list results in an error\n"
]
},
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for ** or pow(): 'list' and 'int'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-27-c0bd6cd82294>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Squaring the list results in an error\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mL\u001b[0m\u001b[1;33m**\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'list' and 'int'"
]
}
],
"source": [
"print(\"Squaring the list results in an error\")\n",
"L**2"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is how list squaring actually works\n"
]
},
{
"data": {
"text/plain": [
"[1, 4, 9, 16, 25]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"This is how list squaring actually works\")\n",
"L2 = []\n",
"for e in L:\n",
" L2.append(e**2)\n",
"L2"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"While it works perfectly with numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([1, 4, 9], dtype=int32)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"While it works perfectly with numpy array\")\n",
"A**2"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting square roots of all elements of numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([1. , 1.41421356, 1.73205081])"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Getting square roots of all elements of numpy array\")\n",
"np.sqrt(A)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting log of all elements of a numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([0. , 0.69314718, 1.09861229])"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Getting log of all elements of a numpy array\")\n",
"np.log(A)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Exponential with numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([ 2.71828183, 7.3890561 , 20.08553692])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Exponential with numpy array\")\n",
"np.exp(A)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#creating two numpy vectors\n",
"a = np.array([1,2])\n",
"b = np.array([2,1])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"zip(L1, L2) will result in (1,4), (2,5), (3, 6)\n"
]
}
],
"source": [
"#zip() explanation\n",
"L1 = [1, 2, 3]\n",
"L2 = [4, 5, 6]\n",
"print(\"zip(L1, L2) will result in (1,4), (2,5), (3, 6)\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"#calculating dot product by looping\n",
"dot = 0\n",
"for e, f in zip(a, b):\n",
" dot = dot + e*f\n",
"print(dot)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Multiplication of two vectors results in their values multiplication\n"
]
},
{
"data": {
"text/plain": [
"array([2, 2])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Multiplication of two vectors results in their values multiplication\")\n",
"a*b"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sum up all the elements\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Sum up all the elements\")\n",
"np.sum(a*b)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The same with (a*b).sum()\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"The same with (a*b).sum()\")\n",
"(a*b).sum()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The most convinient way to compute a dot product is using np.dot(a, b)\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"The most convinient way to compute a dot product is using np.dot(a, b)\")\n",
"np.dot(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Or it may be called by a.dot(b)\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Or it may be called by a.dot(b)\")\n",
"a.dot(b)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Or b.dot(a)\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Or b.dot(a)\")\n",
"b.dot(a)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculating vector length\n"
]
},
{
"data": {
"text/plain": [
"2.23606797749979"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Calculating vector length\")\n",
"amag = np.sqrt((a*a).sum())\n",
"amag"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculate vector length easier with np.linalg.norm(a)\n"
]
},
{
"data": {
"text/plain": [
"2.23606797749979"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Calculate vector length easier with np.linalg.norm(a)\")\n",
"np.linalg.norm(a)\n",
"amag"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculating the cosine of the agle between two vectors\n"
]
},
{
"data": {
"text/plain": [
"0.7999999999999998"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Calculating the cosine of the agle between two vectors\")\n",
"cosangle = a.dot(b) / (np.linalg.norm(a) * np.linalg.norm(b))\n",
"cosangle"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The actual angle is\n"
]
},
{
"data": {
"text/plain": [
"0.6435011087932847"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"The actual angle is\")\n",
"angle = np.arccos(cosangle)\n",
"angle"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from datetime import datetime"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randn(100)\n",
"b = np.random.randn(100)\n",
"T = 100000"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def slow_dot_product(a, b):\n",
" result = 0\n",
" for e, f in zip(a, b):\n",
" result = result + e*f\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#calculating time for the slower version\n",
"#initial time\n",
"t0 = datetime.now()\n",
"for t in range(T):\n",
" slow_dot_product(a, b)\n",
"#final time\n",
"dt1 = datetime.now() - t0"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#calculating time for the faster version\n",
"t0 = datetime.now()\n",
"for t in range(T):\n",
" a.dot(b)\n",
"dt2 = datetime.now() - t0"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dt1 / dt2: 38.599624631055946\n"
]
}
],
"source": [
"print(\"dt1 / dt2: \", dt1.total_seconds() / dt2.total_seconds())"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#initializing a matrix\n",
"M = np.array([ [1, 2], [3, 4] ])\n",
"M"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"creating a list of lists\n"
]
},
{
"data": {
"text/plain": [
"[[1, 2], [3, 4]]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"creating a list of lists\")\n",
"L = [ [1, 2], [3, 4] ]\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"calling the first element of the list - the first row\n"
]
},
{
"data": {
"text/plain": [
"[1, 2]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"calling the first element of the list - the first row\")\n",
"L[0]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First row, first column\n"
]
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"First row, first column\")\n",
"L[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The same with numpy array\n"
]
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"The same with numpy array\")\n",
"M[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shortcut: M[0,0]\n"
]
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Shortcut: M[0,0]\")\n",
"M[0,0]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"M2 = np.matrix([ [1, 2], [3, 4] ])\n",
"M2"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"To convert a matrix to an array: A = np.array(M2)\n"
]
},
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"To convert a matrix to an array: A = np.array(M2)\")\n",
"A = np.array(M2)\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Transpose the matrix (array operation!)\n"
]
},
{
"data": {
"text/plain": [
"array([[1, 3],\n",
" [2, 4]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Transpose the matrix (array operation!)\")\n",
"A.T"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating a numpy array with all zeros\n",
"Z = np.zeros(10)\n",
"Z"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Z = np.zeros((10, 10))\n",
"Z"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating a numpy array with all ones\n",
"O = np.ones(10)\n",
"O"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"O = np.ones((10, 10))\n",
"O"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.21204189, 0.91299105, 0.75941769, 0.65631123, 0.10221989,\n",
" 0.8200861 , 0.89756825, 0.88276053, 0.36960569, 0.00582751])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating a numpy array with random numbers\n",
"R = np.random.random(10)\n",
"R"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4.31949374e-01, 1.75383508e-01, 2.04671587e-02, 9.53508410e-01,\n",
" 8.02341040e-01, 9.23835988e-01, 2.43198528e-01, 5.77208835e-01,\n",
" 3.09885826e-01, 8.34308220e-01],\n",
" [6.01005815e-01, 1.63282856e-01, 2.67571839e-01, 6.53611221e-01,\n",
" 9.34804313e-01, 4.35434337e-02, 3.81528151e-01, 2.75326691e-01,\n",
" 3.09560551e-01, 2.58534701e-01],\n",
" [3.65515638e-01, 7.30899760e-01, 2.93047472e-02, 3.42055073e-01,\n",
" 4.37645291e-01, 5.69707147e-01, 7.00926767e-01, 4.60217353e-01,\n",
" 5.71254806e-01, 8.89942989e-01],\n",
" [4.46374074e-01, 1.76288047e-01, 2.71927652e-01, 4.13943851e-01,\n",
" 6.07155443e-01, 5.62757919e-02, 1.98985778e-01, 3.52880573e-01,\n",
" 9.05484131e-01, 9.25024569e-01],\n",
" [8.74791793e-01, 7.39830079e-01, 4.49716143e-01, 6.74855574e-02,\n",
" 1.10341278e-02, 7.78607479e-01, 6.95214011e-01, 3.32358592e-01,\n",
" 9.13320518e-01, 1.43864059e-02],\n",
" [6.39645056e-01, 1.28130195e-01, 8.97966085e-01, 5.12966039e-01,\n",
" 6.46753264e-01, 1.42459919e-01, 3.96183424e-01, 6.71837299e-01,\n",
" 1.06867987e-01, 7.78940670e-01],\n",
" [9.56650974e-01, 6.71784365e-01, 5.29041002e-01, 2.96626156e-01,\n",
" 6.11087876e-01, 8.53478512e-01, 8.21243178e-01, 9.65488814e-01,\n",
" 3.87606244e-01, 6.39583776e-01],\n",
" [4.48856736e-01, 6.19294418e-01, 6.62224294e-03, 9.59007065e-01,\n",
" 2.74406809e-01, 9.60806815e-01, 9.76457538e-01, 9.10541826e-01,\n",
" 3.80827071e-01, 5.30047331e-02],\n",
" [8.69492332e-01, 3.77928841e-01, 6.52242742e-02, 4.99760183e-01,\n",
" 5.06597556e-02, 1.77806155e-01, 8.73653085e-01, 5.79150818e-01,\n",
" 6.23448788e-03, 8.83616693e-01],\n",
" [5.51911964e-01, 8.76896114e-01, 6.22016732e-01, 7.78606888e-04,\n",
" 8.14065566e-01, 3.50728407e-01, 1.42883053e-01, 4.06901356e-01,\n",
" 1.33318011e-01, 1.58589579e-01]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"R = np.random.random((10, 10))\n",
"R"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2.46682589e-01, -2.03814030e+00, 4.08411833e-01, -1.28800481e+00,\n",
" -1.59198894e+00, 4.66533193e-01, -8.61716578e-01, 3.29878148e-01,\n",
" 6.76653338e-01, 8.97786683e-04])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#create a numpy array with random gaussian distribution numbers\n",
"G = np.random.randn(10)\n",
"G"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object cannot be interpreted as an integer",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-12-b6345e5dbed4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mG\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mG\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.RandomState.randn\u001b[1;34m()\u001b[0m\n",
"\u001b[1;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.RandomState.standard_normal\u001b[1;34m()\u001b[0m\n",
"\u001b[1;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.cont0_array\u001b[1;34m()\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object cannot be interpreted as an integer"
]
}
],
"source": [
"#wrong way!!!\n",
"G = np.random.randn((10, 10))\n",
"G"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.17807509, -0.99806662, 0.17365912, -0.95857996, -0.15603788,\n",
" -0.82934727, -2.21130147, 0.48672379, -0.66771044, 0.98419288],\n",
" [-0.20003559, -2.15071017, 0.61678198, 0.51781085, 0.65959277,\n",
" -0.98303601, -1.41803518, -0.00764794, -0.68809637, -0.02150812],\n",
" [-1.65471932, 0.25635708, -0.30812349, -0.66253725, 0.50562028,\n",
" 0.97246907, 1.44669811, -2.47173159, -0.78896533, -0.31029103],\n",
" [ 0.46103287, -0.74147118, -1.48784364, -0.48384939, -0.96056611,\n",
" -0.32045397, 0.1506943 , 0.71510368, 2.1505094 , -0.6140377 ],\n",
" [ 1.93591126, -0.11843801, 0.80948247, -0.53460123, -0.9341195 ,\n",
" 0.11490823, -0.14713725, -0.96111687, 1.6096978 , 2.12114133],\n",
" [ 0.49951608, -0.0176546 , -0.44431157, -0.92546415, 0.01829041,\n",
" 0.59096219, -1.07893755, 0.97677733, 0.49476401, 0.55708143],\n",
" [ 0.1510502 , 0.55754975, -0.86569854, 0.89159582, 0.34715981,\n",
" -0.50378756, 0.57672941, 0.36276265, -1.51198015, 0.85076848],\n",
" [-0.01802723, -0.53146861, -1.41605913, 0.57172368, -2.1343569 ,\n",
" 0.46337675, 0.05827041, -0.39599624, -1.12264142, 1.5860646 ],\n",
" [-1.67276466, -0.36035166, -0.73159676, -0.26775514, 0.25279221,\n",
" 1.81439395, -0.41639858, 0.97598081, -1.401598 , 0.59324907],\n",
" [ 0.55305072, 2.93221243, 1.9079762 , 0.54484195, 1.57813092,\n",
" -0.10954807, 1.94873863, 0.51301775, -2.32423946, -2.43710101]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#correct way :)\n",
"G = np.random.randn(10, 10)\n",
"G"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mean\n"
]
},
{
"data": {
"text/plain": [
"-0.0544256287200197"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Mean\")\n",
"G.mean()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variance\n"
]
},
{
"data": {
"text/plain": [
"1.1744360608657385"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Variance\")\n",
"G.var()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([ [1, 2], [3, 4] ])\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[5, 6],\n",
" [7, 8]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([ [5, 6], [7, 8]])\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"element by element multiplication\n"
]
},
{
"data": {
"text/plain": [
"array([[ 5, 12],\n",
" [21, 32]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"element by element multiplication\")\n",
"a*b"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"matrix multiplication\n"
]
},
{
"data": {
"text/plain": [
"array([[19, 22],\n",
" [43, 50]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"matrix multiplication\")\n",
"a.dot(b)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([ [1, 2], [3, 4]])\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"matrix inverse\n"
]
},
{
"data": {
"text/plain": [
"array([[-2. , 1. ],\n",
" [ 1.5, -0.5]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"matrix inverse\")\n",
"Ainv = np.linalg.inv(A)\n",
"Ainv"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.0000000e+00, 4.4408921e-16],\n",
" [0.0000000e+00, 1.0000000e+00]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Ainv.dot(A)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"matrix determinant\n"
]
},
{
"data": {
"text/plain": [
"-2.0000000000000004"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"matrix determinant\")\n",
"Adet = np.linalg.det(A)\n",
"Adet"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"matrix diagonal\n"
]
},
{
"data": {
"text/plain": [
"array([1, 4])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"matrix diagonal\")\n",
"Adiag = np.diag(A)\n",
"Adiag"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 0],\n",
" [0, 2]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.diag([1,2])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([1, 2])\n",
"b = np.array([3, 4])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3, 4],\n",
" [6, 8]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#outer product\n",
"np.outer(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.inner(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.dot(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.diag(A).sum()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# same as np.diag(A).sum()\n",
"np.trace(A)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2.61108044e+00, -5.56266784e-01, 4.58576683e-01],\n",
" [-2.22513350e+00, 2.07857449e+00, -9.23179469e-01],\n",
" [-8.84919070e-01, 2.78100129e+00, -1.46277317e-01],\n",
" [-1.68683425e+00, 9.34315573e-02, -7.27786398e-01],\n",
" [-1.25178566e+00, -1.67057324e-01, 3.03391571e-01],\n",
" [-7.22548044e-01, 3.64771039e-01, -2.07898488e+00],\n",
" [-1.67466253e+00, 1.32372428e+00, 3.06764286e-02],\n",
" [ 4.77250966e-01, 1.00202064e+00, -1.76902835e-01],\n",
" [-2.96482920e-01, 9.04995602e-01, 1.26159644e+00],\n",
" [ 9.60858870e-01, -1.24789005e-02, 6.02272746e-01],\n",
" [ 9.18769502e-01, 7.65606362e-01, -2.31953484e-01],\n",
" [-3.05593815e-01, 5.51882385e-01, 1.95580091e-01],\n",
" [ 1.02987209e+00, 7.34572814e-01, 2.12978243e-01],\n",
" [ 1.52108393e+00, -9.67386084e-01, -1.65085713e+00],\n",
" [ 1.43920888e+00, 1.13398954e-01, 8.38158227e-01],\n",
" [-5.19608078e-01, 2.91131785e+00, -1.42457818e-01],\n",
" [ 2.19459137e+00, 1.08915816e+00, 4.23594916e-01],\n",
" [ 1.82371161e-01, -1.01114486e+00, 1.20743768e+00],\n",
" [-1.55648549e+00, -1.76848701e+00, 2.40346678e-01],\n",
" [ 1.86505240e-01, 9.69112716e-01, -3.32478850e-01],\n",
" [ 1.55199402e+00, 2.20728008e-01, -7.85698880e-01],\n",
" [ 7.47955741e-01, -1.92574368e-01, -1.17141855e-02],\n",
" [ 1.50491057e+00, -4.77195731e-01, -1.99922017e+00],\n",
" [-1.76434938e-01, -2.24758556e+00, 3.30069979e-01],\n",
" [ 8.87484186e-02, -1.45007814e+00, -1.94713343e-01],\n",
" [ 3.13410211e-01, -8.21536829e-01, 1.29678729e+00],\n",
" [-4.25776314e-01, 3.79905265e-02, 9.30669790e-01],\n",
" [ 4.76842582e-01, 6.77686429e-01, 1.89410329e-01],\n",
" [ 4.67556012e-01, -7.39035948e-01, 1.07248047e-01],\n",
" [ 9.22512027e-01, 1.84440806e+00, -2.44866462e+00],\n",
" [-5.07145740e-01, -1.85098765e-01, -3.01003712e-01],\n",
" [-1.30399778e-01, -2.98949575e-01, 1.75053209e+00],\n",
" [ 7.65765930e-01, 9.71163177e-01, 1.71582433e+00],\n",
" [ 3.40321887e-01, 8.49901118e-01, -3.22712395e-02],\n",
" [-4.56610359e-01, 1.36689490e+00, 5.50837059e-01],\n",
" [-6.54436670e-02, -2.10783681e-01, -6.83214624e-01],\n",
" [ 1.77410482e+00, -1.20843474e+00, -3.67338114e-02],\n",
" [-2.04045157e-01, 2.19891581e-01, -4.36996646e-01],\n",
" [-1.71914628e-01, 6.83046401e-02, 1.34346841e-01],\n",
" [ 2.05912048e-01, 4.34663008e-01, 1.28316493e+00],\n",
" [ 7.78833372e-01, -7.38565868e-01, -1.26354148e-01],\n",
" [-1.16728817e+00, 7.39378390e-01, -1.87908311e+00],\n",
" [-9.70711454e-01, -9.79958006e-03, 1.28691653e-01],\n",
" [-9.97513035e-01, 1.05766549e+00, -4.39228726e-01],\n",
" [-3.71095540e-01, 4.60607544e-01, 1.74094100e-01],\n",
" [-3.30395694e-01, -1.63192336e+00, -1.77436300e-01],\n",
" [ 3.34153752e-01, 3.36794691e-01, 8.99404945e-01],\n",
" [-3.60003170e-01, -1.14369700e+00, -9.02629658e-01],\n",
" [-4.45215964e-01, -8.54034921e-01, -3.56032469e-01],\n",
" [-1.20407497e+00, 1.05702928e-01, -1.17169456e-01],\n",
" [-2.14782081e+00, -1.06292312e-01, -1.04402486e-01],\n",
" [ 5.74722949e-01, 2.54267035e+00, -8.07237637e-01],\n",
" [ 1.68383604e+00, 8.70103374e-02, -1.17644477e+00],\n",
" [ 4.94009656e-01, -2.72620997e-02, 7.23878469e-01],\n",
" [-1.32687369e+00, 2.51846079e-01, 6.24502809e-02],\n",
" [-1.07079340e+00, 7.70366363e-01, 1.49262585e-01],\n",
" [-3.85836415e-01, 6.21442357e-01, -4.71471398e-01],\n",
" [-8.85711912e-01, -2.99986702e-03, 5.78767560e-01],\n",
" [ 8.66246446e-01, -1.20004016e+00, 2.79604001e+00],\n",
" [-8.53168717e-01, 7.17109218e-01, 7.66138770e-01],\n",
" [ 1.71244258e-01, -3.95696869e-01, -3.22700176e-01],\n",
" [-1.37447503e+00, -2.27170544e+00, 7.38748727e-01],\n",
" [ 5.99453126e-02, 7.82720589e-01, 1.28525303e+00],\n",
" [ 5.75407620e-01, -6.00293112e-01, -2.89292528e-01],\n",
" [ 8.87240033e-01, 1.71823130e+00, -2.17991847e+00],\n",
" [ 8.86368641e-02, 7.82270015e-01, -3.75386072e-01],\n",
" [-8.52778256e-02, 1.40941071e-01, 3.82118130e-01],\n",
" [ 7.88809681e-01, -9.39589515e-01, -2.19524792e-01],\n",
" [ 1.54445519e+00, -2.12899601e-01, -1.02156711e+00],\n",
" [ 1.34901926e+00, 1.51663360e+00, -7.24454530e-01],\n",
" [ 1.62497754e+00, 1.27545294e+00, -2.88324037e-01],\n",
" [ 1.26129814e+00, 1.25155959e-01, 6.55634489e-01],\n",
" [-8.41232987e-01, -1.36651033e+00, 1.22744137e+00],\n",
" [-1.35853990e+00, -1.34783624e-01, 1.29667390e-01],\n",
" [ 3.88329662e-02, 6.32259963e-01, -9.96554084e-02],\n",
" [-9.84586582e-01, -3.04334008e+00, 1.95038669e-01],\n",
" [ 4.80067874e-01, -5.04731266e-01, 1.28498909e+00],\n",
" [ 4.99269392e-01, -1.01518345e+00, -5.19439145e-03],\n",
" [ 7.87193893e-01, 3.88792593e-01, 7.08178475e-01],\n",
" [-1.12967069e+00, 3.04813333e+00, -3.77039400e-01],\n",
" [ 3.51544144e-01, -1.43965809e+00, 1.24027171e+00],\n",
" [-1.43442518e+00, 5.41268549e-01, 5.64921430e-01],\n",
" [-8.82715099e-01, -2.01315223e+00, 7.92007482e-02],\n",
" [-3.50660753e-01, -1.80476241e+00, 3.57488485e-01],\n",
" [ 6.99615112e-01, -4.15872630e-01, 8.16935034e-02],\n",
" [-2.69229271e-01, 3.20002080e-01, -1.60160589e+00],\n",
" [ 1.84989123e+00, -2.24852195e+00, -2.44026491e-01],\n",
" [ 1.62100664e+00, -3.55769692e-01, 2.69362657e+00],\n",
" [-1.30450006e+00, 9.38899254e-01, -1.61421502e+00],\n",
" [ 5.49645172e-01, -1.45513348e+00, -3.62391079e-01],\n",
" [-1.15640945e-01, -9.77568942e-01, -6.68643782e-02],\n",
" [-9.56179179e-01, -9.03901368e-02, 1.64342782e-01],\n",
" [-4.33143636e-01, -1.64748450e-01, -2.26735264e-01],\n",
" [ 2.91295696e-01, 2.33907574e-01, 5.89351404e-01],\n",
" [-3.15367052e-01, 6.32528168e-01, -7.82641581e-01],\n",
" [ 8.81177600e-01, 4.24552664e-01, 1.35073538e-01],\n",
" [-6.87815834e-01, -1.46481280e-01, -2.80698445e-01],\n",
" [-1.67085288e+00, 2.65334254e+00, 1.12738531e+00],\n",
" [ 6.38150040e-01, -5.23153878e-01, 7.48051713e-01],\n",
" [ 3.84629606e-01, 3.56374801e-01, -4.98173382e-02]])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"G = np.random.randn(100, 3)\n",
"G"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([ [1, 2], [3, 4] ])\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([1, 2])\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2. , 1. ],\n",
" [ 1.5, -0.5]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Ainv = np.linalg.inv(A)\n",
"Ainv"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2.22044605e-16, 5.00000000e-01])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = Ainv.dot(b)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"better way is using solve()\n"
]
},
{
"data": {
"text/plain": [
"array([0. , 0.5])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"better way is using solve()\")\n",
"np.linalg.solve(A, b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# $1.50 for kids - X1\n",
"# $4.00 for adults - X2\n",
"# 2200 people\n",
"# $5050 collected\n",
"\n",
"# 1.5X1 + 4X2 = 5050\n",
"# X1 + X2 = 2200"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1500., 700.])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([ [1, 1], [1.5, 4]])\n",
"b = np.array([2200, 5050])\n",
"np.linalg.solve(A, b)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"#create a normal python list and numpy array\n",
"L = [1,2,3]\n",
"A = np.array([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Printing a python list\n",
"1\n",
"2\n",
"3\n",
"Printing a numpy array\n",
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"print(\"Printing a python list\")\n",
"for e in L:\n",
" print(e)\n",
"print(\"Printing a numpy array\")\n",
"for e in A:\n",
" print(e)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Printing python list with appended value\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#adding new values to the list\n",
"L.append(4)\n",
"print(\"Printing python list with appended value\")\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Printing python list with added value\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#adding a new value to the python list by addition\n",
"L = L + [5]\n",
"print(\"Printing python list with added value\")\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "AttributeError",
"evalue": "'numpy.ndarray' object has no attribute 'append'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-20-3888ccf82513>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m#appending and addition don't work for numpy arrays\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mA\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[0mA\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mA\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mA\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[0mA\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'append'"
]
}
],
"source": [
"#appending and addition don't work for numpy arrays\n",
"A.append(4)\n",
"A\n",
"A = A = [5]\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix addition with python lists\n"
]
},
{
"data": {
"text/plain": [
"[2, 4, 6, 8, 10]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Matrix addition with python lists\")\n",
"L2 = []\n",
"for e in L:\n",
" L2.append(e + e)\n",
"L2"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix addition with numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([2, 4, 6])"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Matrix addition with numpy array\")\n",
"A + A"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Matrix multiplication with numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([2, 4, 6])"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Matrix multiplication with numpy array\")\n",
"2*A"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Multiplying list by a scalar results in doubling of the list\n"
]
},
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Multiplying list by a scalar results in doubling of the list\")\n",
"2*L"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Squaring the list results in an error\n"
]
},
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for ** or pow(): 'list' and 'int'",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-27-c0bd6cd82294>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Squaring the list results in an error\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mL\u001b[0m\u001b[1;33m**\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for ** or pow(): 'list' and 'int'"
]
}
],
"source": [
"print(\"Squaring the list results in an error\")\n",
"L**2"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is how list squaring actually works\n"
]
},
{
"data": {
"text/plain": [
"[1, 4, 9, 16, 25]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"This is how list squaring actually works\")\n",
"L2 = []\n",
"for e in L:\n",
" L2.append(e**2)\n",
"L2"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"While it works perfectly with numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([1, 4, 9], dtype=int32)"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"While it works perfectly with numpy array\")\n",
"A**2"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting square roots of all elements of numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([1. , 1.41421356, 1.73205081])"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Getting square roots of all elements of numpy array\")\n",
"np.sqrt(A)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Getting log of all elements of a numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([0. , 0.69314718, 1.09861229])"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Getting log of all elements of a numpy array\")\n",
"np.log(A)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Exponential with numpy array\n"
]
},
{
"data": {
"text/plain": [
"array([ 2.71828183, 7.3890561 , 20.08553692])"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Exponential with numpy array\")\n",
"np.exp(A)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#creating two numpy vectors\n",
"a = np.array([1,2])\n",
"b = np.array([2,1])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"zip(L1, L2) will result in (1,4), (2,5), (3, 6)\n"
]
}
],
"source": [
"#zip() explanation\n",
"L1 = [1, 2, 3]\n",
"L2 = [4, 5, 6]\n",
"print(\"zip(L1, L2) will result in (1,4), (2,5), (3, 6)\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4\n"
]
}
],
"source": [
"#calculating dot product by looping\n",
"dot = 0\n",
"for e, f in zip(a, b):\n",
" dot = dot + e*f\n",
"print(dot)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Multiplication of two vectors results in their values multiplication\n"
]
},
{
"data": {
"text/plain": [
"array([2, 2])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Multiplication of two vectors results in their values multiplication\")\n",
"a*b"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Sum up all the elements\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Sum up all the elements\")\n",
"np.sum(a*b)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The same with (a*b).sum()\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"The same with (a*b).sum()\")\n",
"(a*b).sum()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The most convinient way to compute a dot product is using np.dot(a, b)\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"The most convinient way to compute a dot product is using np.dot(a, b)\")\n",
"np.dot(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Or it may be called by a.dot(b)\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Or it may be called by a.dot(b)\")\n",
"a.dot(b)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Or b.dot(a)\n"
]
},
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Or b.dot(a)\")\n",
"b.dot(a)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculating vector length\n"
]
},
{
"data": {
"text/plain": [
"2.23606797749979"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Calculating vector length\")\n",
"amag = np.sqrt((a*a).sum())\n",
"amag"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculate vector length easier with np.linalg.norm(a)\n"
]
},
{
"data": {
"text/plain": [
"2.23606797749979"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Calculate vector length easier with np.linalg.norm(a)\")\n",
"np.linalg.norm(a)\n",
"amag"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Calculating the cosine of the agle between two vectors\n"
]
},
{
"data": {
"text/plain": [
"0.7999999999999998"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Calculating the cosine of the agle between two vectors\")\n",
"cosangle = a.dot(b) / (np.linalg.norm(a) * np.linalg.norm(b))\n",
"cosangle"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The actual angle is\n"
]
},
{
"data": {
"text/plain": [
"0.6435011087932847"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"The actual angle is\")\n",
"angle = np.arccos(cosangle)\n",
"angle"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from datetime import datetime"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"a = np.random.randn(100)\n",
"b = np.random.randn(100)\n",
"T = 100000"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def slow_dot_product(a, b):\n",
" result = 0\n",
" for e, f in zip(a, b):\n",
" result = result + e*f\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"#calculating time for the slower version\n",
"#initial time\n",
"t0 = datetime.now()\n",
"for t in range(T):\n",
" slow_dot_product(a, b)\n",
"#final time\n",
"dt1 = datetime.now() - t0"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#calculating time for the faster version\n",
"t0 = datetime.now()\n",
"for t in range(T):\n",
" a.dot(b)\n",
"dt2 = datetime.now() - t0"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"dt1 / dt2: 38.599624631055946\n"
]
}
],
"source": [
"print(\"dt1 / dt2: \", dt1.total_seconds() / dt2.total_seconds())"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#initializing a matrix\n",
"M = np.array([ [1, 2], [3, 4] ])\n",
"M"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"creating a list of lists\n"
]
},
{
"data": {
"text/plain": [
"[[1, 2], [3, 4]]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"creating a list of lists\")\n",
"L = [ [1, 2], [3, 4] ]\n",
"L"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"calling the first element of the list - the first row\n"
]
},
{
"data": {
"text/plain": [
"[1, 2]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"calling the first element of the list - the first row\")\n",
"L[0]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"First row, first column\n"
]
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"First row, first column\")\n",
"L[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The same with numpy array\n"
]
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"The same with numpy array\")\n",
"M[0][0]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Shortcut: M[0,0]\n"
]
},
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Shortcut: M[0,0]\")\n",
"M[0,0]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"matrix([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"M2 = np.matrix([ [1, 2], [3, 4] ])\n",
"M2"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"To convert a matrix to an array: A = np.array(M2)\n"
]
},
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"To convert a matrix to an array: A = np.array(M2)\")\n",
"A = np.array(M2)\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Transpose the matrix (array operation!)\n"
]
},
{
"data": {
"text/plain": [
"array([[1, 3],\n",
" [2, 4]])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Transpose the matrix (array operation!)\")\n",
"A.T"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating a numpy array with all zeros\n",
"Z = np.zeros(10)\n",
"Z"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],\n",
" [0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Z = np.zeros((10, 10))\n",
"Z"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating a numpy array with all ones\n",
"O = np.ones(10)\n",
"O"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.],\n",
" [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"O = np.ones((10, 10))\n",
"O"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0.21204189, 0.91299105, 0.75941769, 0.65631123, 0.10221989,\n",
" 0.8200861 , 0.89756825, 0.88276053, 0.36960569, 0.00582751])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# creating a numpy array with random numbers\n",
"R = np.random.random(10)\n",
"R"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[4.31949374e-01, 1.75383508e-01, 2.04671587e-02, 9.53508410e-01,\n",
" 8.02341040e-01, 9.23835988e-01, 2.43198528e-01, 5.77208835e-01,\n",
" 3.09885826e-01, 8.34308220e-01],\n",
" [6.01005815e-01, 1.63282856e-01, 2.67571839e-01, 6.53611221e-01,\n",
" 9.34804313e-01, 4.35434337e-02, 3.81528151e-01, 2.75326691e-01,\n",
" 3.09560551e-01, 2.58534701e-01],\n",
" [3.65515638e-01, 7.30899760e-01, 2.93047472e-02, 3.42055073e-01,\n",
" 4.37645291e-01, 5.69707147e-01, 7.00926767e-01, 4.60217353e-01,\n",
" 5.71254806e-01, 8.89942989e-01],\n",
" [4.46374074e-01, 1.76288047e-01, 2.71927652e-01, 4.13943851e-01,\n",
" 6.07155443e-01, 5.62757919e-02, 1.98985778e-01, 3.52880573e-01,\n",
" 9.05484131e-01, 9.25024569e-01],\n",
" [8.74791793e-01, 7.39830079e-01, 4.49716143e-01, 6.74855574e-02,\n",
" 1.10341278e-02, 7.78607479e-01, 6.95214011e-01, 3.32358592e-01,\n",
" 9.13320518e-01, 1.43864059e-02],\n",
" [6.39645056e-01, 1.28130195e-01, 8.97966085e-01, 5.12966039e-01,\n",
" 6.46753264e-01, 1.42459919e-01, 3.96183424e-01, 6.71837299e-01,\n",
" 1.06867987e-01, 7.78940670e-01],\n",
" [9.56650974e-01, 6.71784365e-01, 5.29041002e-01, 2.96626156e-01,\n",
" 6.11087876e-01, 8.53478512e-01, 8.21243178e-01, 9.65488814e-01,\n",
" 3.87606244e-01, 6.39583776e-01],\n",
" [4.48856736e-01, 6.19294418e-01, 6.62224294e-03, 9.59007065e-01,\n",
" 2.74406809e-01, 9.60806815e-01, 9.76457538e-01, 9.10541826e-01,\n",
" 3.80827071e-01, 5.30047331e-02],\n",
" [8.69492332e-01, 3.77928841e-01, 6.52242742e-02, 4.99760183e-01,\n",
" 5.06597556e-02, 1.77806155e-01, 8.73653085e-01, 5.79150818e-01,\n",
" 6.23448788e-03, 8.83616693e-01],\n",
" [5.51911964e-01, 8.76896114e-01, 6.22016732e-01, 7.78606888e-04,\n",
" 8.14065566e-01, 3.50728407e-01, 1.42883053e-01, 4.06901356e-01,\n",
" 1.33318011e-01, 1.58589579e-01]])"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"R = np.random.random((10, 10))\n",
"R"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 2.46682589e-01, -2.03814030e+00, 4.08411833e-01, -1.28800481e+00,\n",
" -1.59198894e+00, 4.66533193e-01, -8.61716578e-01, 3.29878148e-01,\n",
" 6.76653338e-01, 8.97786683e-04])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#create a numpy array with random gaussian distribution numbers\n",
"G = np.random.randn(10)\n",
"G"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "TypeError",
"evalue": "'tuple' object cannot be interpreted as an integer",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-12-b6345e5dbed4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mG\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandom\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrandn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0mG\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.RandomState.randn\u001b[1;34m()\u001b[0m\n",
"\u001b[1;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.RandomState.standard_normal\u001b[1;34m()\u001b[0m\n",
"\u001b[1;32mmtrand.pyx\u001b[0m in \u001b[0;36mmtrand.cont0_array\u001b[1;34m()\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: 'tuple' object cannot be interpreted as an integer"
]
}
],
"source": [
"#wrong way!!!\n",
"G = np.random.randn((10, 10))\n",
"G"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 0.17807509, -0.99806662, 0.17365912, -0.95857996, -0.15603788,\n",
" -0.82934727, -2.21130147, 0.48672379, -0.66771044, 0.98419288],\n",
" [-0.20003559, -2.15071017, 0.61678198, 0.51781085, 0.65959277,\n",
" -0.98303601, -1.41803518, -0.00764794, -0.68809637, -0.02150812],\n",
" [-1.65471932, 0.25635708, -0.30812349, -0.66253725, 0.50562028,\n",
" 0.97246907, 1.44669811, -2.47173159, -0.78896533, -0.31029103],\n",
" [ 0.46103287, -0.74147118, -1.48784364, -0.48384939, -0.96056611,\n",
" -0.32045397, 0.1506943 , 0.71510368, 2.1505094 , -0.6140377 ],\n",
" [ 1.93591126, -0.11843801, 0.80948247, -0.53460123, -0.9341195 ,\n",
" 0.11490823, -0.14713725, -0.96111687, 1.6096978 , 2.12114133],\n",
" [ 0.49951608, -0.0176546 , -0.44431157, -0.92546415, 0.01829041,\n",
" 0.59096219, -1.07893755, 0.97677733, 0.49476401, 0.55708143],\n",
" [ 0.1510502 , 0.55754975, -0.86569854, 0.89159582, 0.34715981,\n",
" -0.50378756, 0.57672941, 0.36276265, -1.51198015, 0.85076848],\n",
" [-0.01802723, -0.53146861, -1.41605913, 0.57172368, -2.1343569 ,\n",
" 0.46337675, 0.05827041, -0.39599624, -1.12264142, 1.5860646 ],\n",
" [-1.67276466, -0.36035166, -0.73159676, -0.26775514, 0.25279221,\n",
" 1.81439395, -0.41639858, 0.97598081, -1.401598 , 0.59324907],\n",
" [ 0.55305072, 2.93221243, 1.9079762 , 0.54484195, 1.57813092,\n",
" -0.10954807, 1.94873863, 0.51301775, -2.32423946, -2.43710101]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#correct way :)\n",
"G = np.random.randn(10, 10)\n",
"G"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mean\n"
]
},
{
"data": {
"text/plain": [
"-0.0544256287200197"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Mean\")\n",
"G.mean()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variance\n"
]
},
{
"data": {
"text/plain": [
"1.1744360608657385"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"Variance\")\n",
"G.var()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = np.array([ [1, 2], [3, 4] ])\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[5, 6],\n",
" [7, 8]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([ [5, 6], [7, 8]])\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"element by element multiplication\n"
]
},
{
"data": {
"text/plain": [
"array([[ 5, 12],\n",
" [21, 32]])"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"element by element multiplication\")\n",
"a*b"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"matrix multiplication\n"
]
},
{
"data": {
"text/plain": [
"array([[19, 22],\n",
" [43, 50]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"matrix multiplication\")\n",
"a.dot(b)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([ [1, 2], [3, 4]])\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"matrix inverse\n"
]
},
{
"data": {
"text/plain": [
"array([[-2. , 1. ],\n",
" [ 1.5, -0.5]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"matrix inverse\")\n",
"Ainv = np.linalg.inv(A)\n",
"Ainv"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.0000000e+00, 4.4408921e-16],\n",
" [0.0000000e+00, 1.0000000e+00]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Ainv.dot(A)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"matrix determinant\n"
]
},
{
"data": {
"text/plain": [
"-2.0000000000000004"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"matrix determinant\")\n",
"Adet = np.linalg.det(A)\n",
"Adet"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"matrix diagonal\n"
]
},
{
"data": {
"text/plain": [
"array([1, 4])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"matrix diagonal\")\n",
"Adiag = np.diag(A)\n",
"Adiag"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 0],\n",
" [0, 2]])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.diag([1,2])"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"a = np.array([1, 2])\n",
"b = np.array([3, 4])"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[3, 4],\n",
" [6, 8]])"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#outer product\n",
"np.outer(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.inner(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.dot(a, b)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.diag(A).sum()"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"5"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# same as np.diag(A).sum()\n",
"np.trace(A)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2.61108044e+00, -5.56266784e-01, 4.58576683e-01],\n",
" [-2.22513350e+00, 2.07857449e+00, -9.23179469e-01],\n",
" [-8.84919070e-01, 2.78100129e+00, -1.46277317e-01],\n",
" [-1.68683425e+00, 9.34315573e-02, -7.27786398e-01],\n",
" [-1.25178566e+00, -1.67057324e-01, 3.03391571e-01],\n",
" [-7.22548044e-01, 3.64771039e-01, -2.07898488e+00],\n",
" [-1.67466253e+00, 1.32372428e+00, 3.06764286e-02],\n",
" [ 4.77250966e-01, 1.00202064e+00, -1.76902835e-01],\n",
" [-2.96482920e-01, 9.04995602e-01, 1.26159644e+00],\n",
" [ 9.60858870e-01, -1.24789005e-02, 6.02272746e-01],\n",
" [ 9.18769502e-01, 7.65606362e-01, -2.31953484e-01],\n",
" [-3.05593815e-01, 5.51882385e-01, 1.95580091e-01],\n",
" [ 1.02987209e+00, 7.34572814e-01, 2.12978243e-01],\n",
" [ 1.52108393e+00, -9.67386084e-01, -1.65085713e+00],\n",
" [ 1.43920888e+00, 1.13398954e-01, 8.38158227e-01],\n",
" [-5.19608078e-01, 2.91131785e+00, -1.42457818e-01],\n",
" [ 2.19459137e+00, 1.08915816e+00, 4.23594916e-01],\n",
" [ 1.82371161e-01, -1.01114486e+00, 1.20743768e+00],\n",
" [-1.55648549e+00, -1.76848701e+00, 2.40346678e-01],\n",
" [ 1.86505240e-01, 9.69112716e-01, -3.32478850e-01],\n",
" [ 1.55199402e+00, 2.20728008e-01, -7.85698880e-01],\n",
" [ 7.47955741e-01, -1.92574368e-01, -1.17141855e-02],\n",
" [ 1.50491057e+00, -4.77195731e-01, -1.99922017e+00],\n",
" [-1.76434938e-01, -2.24758556e+00, 3.30069979e-01],\n",
" [ 8.87484186e-02, -1.45007814e+00, -1.94713343e-01],\n",
" [ 3.13410211e-01, -8.21536829e-01, 1.29678729e+00],\n",
" [-4.25776314e-01, 3.79905265e-02, 9.30669790e-01],\n",
" [ 4.76842582e-01, 6.77686429e-01, 1.89410329e-01],\n",
" [ 4.67556012e-01, -7.39035948e-01, 1.07248047e-01],\n",
" [ 9.22512027e-01, 1.84440806e+00, -2.44866462e+00],\n",
" [-5.07145740e-01, -1.85098765e-01, -3.01003712e-01],\n",
" [-1.30399778e-01, -2.98949575e-01, 1.75053209e+00],\n",
" [ 7.65765930e-01, 9.71163177e-01, 1.71582433e+00],\n",
" [ 3.40321887e-01, 8.49901118e-01, -3.22712395e-02],\n",
" [-4.56610359e-01, 1.36689490e+00, 5.50837059e-01],\n",
" [-6.54436670e-02, -2.10783681e-01, -6.83214624e-01],\n",
" [ 1.77410482e+00, -1.20843474e+00, -3.67338114e-02],\n",
" [-2.04045157e-01, 2.19891581e-01, -4.36996646e-01],\n",
" [-1.71914628e-01, 6.83046401e-02, 1.34346841e-01],\n",
" [ 2.05912048e-01, 4.34663008e-01, 1.28316493e+00],\n",
" [ 7.78833372e-01, -7.38565868e-01, -1.26354148e-01],\n",
" [-1.16728817e+00, 7.39378390e-01, -1.87908311e+00],\n",
" [-9.70711454e-01, -9.79958006e-03, 1.28691653e-01],\n",
" [-9.97513035e-01, 1.05766549e+00, -4.39228726e-01],\n",
" [-3.71095540e-01, 4.60607544e-01, 1.74094100e-01],\n",
" [-3.30395694e-01, -1.63192336e+00, -1.77436300e-01],\n",
" [ 3.34153752e-01, 3.36794691e-01, 8.99404945e-01],\n",
" [-3.60003170e-01, -1.14369700e+00, -9.02629658e-01],\n",
" [-4.45215964e-01, -8.54034921e-01, -3.56032469e-01],\n",
" [-1.20407497e+00, 1.05702928e-01, -1.17169456e-01],\n",
" [-2.14782081e+00, -1.06292312e-01, -1.04402486e-01],\n",
" [ 5.74722949e-01, 2.54267035e+00, -8.07237637e-01],\n",
" [ 1.68383604e+00, 8.70103374e-02, -1.17644477e+00],\n",
" [ 4.94009656e-01, -2.72620997e-02, 7.23878469e-01],\n",
" [-1.32687369e+00, 2.51846079e-01, 6.24502809e-02],\n",
" [-1.07079340e+00, 7.70366363e-01, 1.49262585e-01],\n",
" [-3.85836415e-01, 6.21442357e-01, -4.71471398e-01],\n",
" [-8.85711912e-01, -2.99986702e-03, 5.78767560e-01],\n",
" [ 8.66246446e-01, -1.20004016e+00, 2.79604001e+00],\n",
" [-8.53168717e-01, 7.17109218e-01, 7.66138770e-01],\n",
" [ 1.71244258e-01, -3.95696869e-01, -3.22700176e-01],\n",
" [-1.37447503e+00, -2.27170544e+00, 7.38748727e-01],\n",
" [ 5.99453126e-02, 7.82720589e-01, 1.28525303e+00],\n",
" [ 5.75407620e-01, -6.00293112e-01, -2.89292528e-01],\n",
" [ 8.87240033e-01, 1.71823130e+00, -2.17991847e+00],\n",
" [ 8.86368641e-02, 7.82270015e-01, -3.75386072e-01],\n",
" [-8.52778256e-02, 1.40941071e-01, 3.82118130e-01],\n",
" [ 7.88809681e-01, -9.39589515e-01, -2.19524792e-01],\n",
" [ 1.54445519e+00, -2.12899601e-01, -1.02156711e+00],\n",
" [ 1.34901926e+00, 1.51663360e+00, -7.24454530e-01],\n",
" [ 1.62497754e+00, 1.27545294e+00, -2.88324037e-01],\n",
" [ 1.26129814e+00, 1.25155959e-01, 6.55634489e-01],\n",
" [-8.41232987e-01, -1.36651033e+00, 1.22744137e+00],\n",
" [-1.35853990e+00, -1.34783624e-01, 1.29667390e-01],\n",
" [ 3.88329662e-02, 6.32259963e-01, -9.96554084e-02],\n",
" [-9.84586582e-01, -3.04334008e+00, 1.95038669e-01],\n",
" [ 4.80067874e-01, -5.04731266e-01, 1.28498909e+00],\n",
" [ 4.99269392e-01, -1.01518345e+00, -5.19439145e-03],\n",
" [ 7.87193893e-01, 3.88792593e-01, 7.08178475e-01],\n",
" [-1.12967069e+00, 3.04813333e+00, -3.77039400e-01],\n",
" [ 3.51544144e-01, -1.43965809e+00, 1.24027171e+00],\n",
" [-1.43442518e+00, 5.41268549e-01, 5.64921430e-01],\n",
" [-8.82715099e-01, -2.01315223e+00, 7.92007482e-02],\n",
" [-3.50660753e-01, -1.80476241e+00, 3.57488485e-01],\n",
" [ 6.99615112e-01, -4.15872630e-01, 8.16935034e-02],\n",
" [-2.69229271e-01, 3.20002080e-01, -1.60160589e+00],\n",
" [ 1.84989123e+00, -2.24852195e+00, -2.44026491e-01],\n",
" [ 1.62100664e+00, -3.55769692e-01, 2.69362657e+00],\n",
" [-1.30450006e+00, 9.38899254e-01, -1.61421502e+00],\n",
" [ 5.49645172e-01, -1.45513348e+00, -3.62391079e-01],\n",
" [-1.15640945e-01, -9.77568942e-01, -6.68643782e-02],\n",
" [-9.56179179e-01, -9.03901368e-02, 1.64342782e-01],\n",
" [-4.33143636e-01, -1.64748450e-01, -2.26735264e-01],\n",
" [ 2.91295696e-01, 2.33907574e-01, 5.89351404e-01],\n",
" [-3.15367052e-01, 6.32528168e-01, -7.82641581e-01],\n",
" [ 8.81177600e-01, 4.24552664e-01, 1.35073538e-01],\n",
" [-6.87815834e-01, -1.46481280e-01, -2.80698445e-01],\n",
" [-1.67085288e+00, 2.65334254e+00, 1.12738531e+00],\n",
" [ 6.38150040e-01, -5.23153878e-01, 7.48051713e-01],\n",
" [ 3.84629606e-01, 3.56374801e-01, -4.98173382e-02]])"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"G = np.random.randn(100, 3)\n",
"G"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2],\n",
" [3, 4]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([ [1, 2], [3, 4] ])\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1, 2])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = np.array([1, 2])\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[-2. , 1. ],\n",
" [ 1.5, -0.5]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Ainv = np.linalg.inv(A)\n",
"Ainv"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([2.22044605e-16, 5.00000000e-01])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = Ainv.dot(b)\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"better way is using solve()\n"
]
},
{
"data": {
"text/plain": [
"array([0. , 0.5])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print(\"better way is using solve()\")\n",
"np.linalg.solve(A, b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# $1.50 for kids - X1\n",
"# $4.00 for adults - X2\n",
"# 2200 people\n",
"# $5050 collected\n",
"\n",
"# 1.5X1 + 4X2 = 5050\n",
"# X1 + X2 = 2200"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([1500., 700.])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = np.array([ [1, 1], [1.5, 4]])\n",
"b = np.array([2200, 5050])\n",
"np.linalg.solve(A, b)"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment