ML_course/assignment 2/iml_assignment2b_solved.ipynb

393 lines
7.7 KiB
Plaintext
Raw Permalink Normal View History

2023-03-31 11:53:42 +00:00
{
"cells": [
{
"cell_type": "code",
2023-04-27 13:24:21 +00:00
"execution_count": 2,
2023-03-31 11:53:42 +00:00
"outputs": [],
"source": [
"import numpy as np"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Create a 10x50 matrix with random numbers using numpy"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(10, 50)\n"
]
}
],
"source": [
"x = np.random.rand(10,50)\n",
"# Print the shape of the matrix\n",
"print(x.shape)"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 6,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0.23987495 0.91661312 0.86479877 0.97246676 0.2996044 0.19370428\n",
" 0.38302033 0.45876654 0.23953333 0.88275397 0.39823726 0.68814433\n",
" 0.69320487 0.87894669 0.12043348 0.56381261 0.41057224 0.44619567\n",
" 0.83936425 0.11512131 0.10305665 0.30666838 0.10695437 0.77828681\n",
" 0.26825193 0.88529244 0.13623807 0.85569497 0.56312584 0.83247308\n",
" 0.4950099 0.66125172 0.45603826 0.41389193 0.04471278 0.74099649\n",
" 0.82862858 0.73879875 0.10928491 0.47473116 0.1124304 0.02034965\n",
" 0.3651702 0.24298615 0.02315607 0.63034624 0.86772443 0.45138212\n",
" 0.68984731 0.45618784]\n"
]
}
],
"source": [
"# Ask from the user for a row number and print the row given\n",
"row = int(input(\"Enter a row number: \"))\n",
"print(x[row])\n",
"# Do the same for a column\n",
"col = int(input(\"Enter a column number: \"))\n",
"print(x[:,col])"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Create the following arrays and calculate their dot product\n",
"| 5 | 1 | 3 |\n",
"| 1 | 2 | 1 |\n",
"| 1 | 2 | 1 |\n",
"\n",
"| 1 | 2 | 3 |"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
2023-04-27 13:24:21 +00:00
"execution_count": 9,
2023-03-31 11:53:42 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[5 1 3]\n",
" [1 2 1]\n",
" [1 2 1]]\n",
2023-04-27 13:24:21 +00:00
"[1 2 3]\n",
"[16 8 8]\n"
2023-03-31 11:53:42 +00:00
]
}
],
"source": [
"tmp_array = np.array([[5,1,3],[1,2,1],[1,2,1]])\n",
2023-04-27 13:24:21 +00:00
"tmp_array2 = np.array([1,2,3])\n",
2023-03-31 11:53:42 +00:00
"print(tmp_array)\n",
"print(tmp_array2)\n",
2023-04-27 13:24:21 +00:00
"# print dot product of tmp_array and tmp_array2\n",
"print(np.dot(tmp_array,tmp_array2))"
2023-03-31 11:53:42 +00:00
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Given the following matrices calculate their (matrix) multiplication\n",
"A = | 1 | 0 |\n",
"| 0 | 1 |\n",
"\n",
"B = | 4 | 1 |\n",
"| 2 | 2 |\n",
"\n",
"c = | 1 | 2 |"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
2023-04-27 13:24:21 +00:00
"execution_count": 11,
2023-03-31 11:53:42 +00:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[4 1]\n",
" [2 2]]\n",
2023-04-27 13:24:21 +00:00
"[1 2]\n"
2023-03-31 11:53:42 +00:00
]
}
],
"source": [
"# A*B\n",
"A = np.array([[1,0],[0,1]])\n",
"B = np.array([[4,1],[2,2]])\n",
"print(np.dot(A,B))\n",
"# A*c\n",
2023-04-27 13:24:21 +00:00
"c = np.array([1,2])\n",
2023-03-31 11:53:42 +00:00
"print(np.dot(A,c.T))"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Find the third power of this matrix\n",
"\n",
"| 0 | 1 |\n",
"| -1 | 0 |"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"tmp_array = np.array([[0,1],[-1,0]])\n",
"print(np.linalg.matrix_power(tmp_array,3))"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Find the determinant of this matrix\n",
"\n",
"| 1 | 2 |\n",
"| 3 | 4 |"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 10,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-2.0000000000000004\n"
]
}
],
"source": [
"tmp_array = np.array([[1,2],[3,4]])\n",
"print(np.linalg.det(tmp_array))"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Use the functions eye and ones from numpy to create 4x4 matrices. Find their ranks."
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 12,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1. 0. 0. 0.]\n",
" [0. 1. 0. 0.]\n",
" [0. 0. 1. 0.]\n",
" [0. 0. 0. 1.]]\n",
"4\n",
"[[1. 1. 1. 1.]\n",
" [1. 1. 1. 1.]\n",
" [1. 1. 1. 1.]\n",
" [1. 1. 1. 1.]]\n",
"1\n"
]
}
],
"source": [
"tmp_array = np.eye(4)\n",
"print(tmp_array)\n",
"print(np.linalg.matrix_rank(tmp_array))\n",
"tmp_array = np.ones((4,4))\n",
"print(tmp_array)\n",
"print(np.linalg.matrix_rank(tmp_array))"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Find the inverse of this matrix\n",
"\n",
"| 2 | 2 | 1 |\n",
"| 1 | 3 | 1 |\n",
"| 1 | 2 | 2 |"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 13,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 0.8 -0.4 -0.2]\n",
" [-0.2 0.6 -0.2]\n",
" [-0.2 -0.4 0.8]]\n"
]
}
],
"source": [
"tmp_array = np.array([[2,2,1],[1,3,1],[1,2,2]])\n",
"print(np.linalg.inv(tmp_array))"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Find the pseudo inverse of this matrix\n",
"\n",
"| 2 | 8 |\n",
"| 1 | 4 |"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 14,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0.02352941 0.01176471]\n",
" [0.09411765 0.04705882]]\n"
]
}
],
"source": [
"tmp_array = np.array([[2,8],[1,4]])\n",
"print(np.linalg.pinv(tmp_array))"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Find the eigenvalues and eigenvectors of this matrix\n",
"\n",
"| 1 | -1 |\n",
"| 1 | 1 |"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 15,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1.+1.j 1.-1.j]\n",
"[[0.70710678+0.j 0.70710678-0.j ]\n",
" [0. -0.70710678j 0. +0.70710678j]]\n"
]
}
],
"source": [
"tmp_array = np.array([[1,-1],[1,1]])\n",
"eigenvalues, eigenvectors = np.linalg.eig(tmp_array)\n",
"print(eigenvalues)\n",
"print(eigenvectors)\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"collapsed": false
}
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 0
}