Skip to content
This repository was archived by the owner on Aug 17, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions High_Dimensionality_Sprint_Challenge.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "High Dimensionality Sprint Challenge.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"[View in Colaboratory](https://colab.research.google.com/github/vinitrg/ML-Precourse/blob/master/High_Dimensionality_Sprint_Challenge.ipynb)"
]
},
{
"metadata": {
"id": "8dtL-3V557G6",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# High Dimensionality Sprint Challenge\n",
"\n",
"High Dimensionality is characterized by the _Curse of Dimensionality_. Humans can visualize 2d and 3d, but not anything higher. How do we reason about data?\n",
"\n",
"High Dimensionality Objectives include:\n",
"* Normalize a vector into a magnitude and unit vector\n",
"* Compute L1 and L2 distances of a pair of vectors\n",
"* Compute the distance between pairs of vectors and select the smallest"
]
},
{
"metadata": {
"id": "PMajVGoY54DR",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"# LAMBDA SCHOOL\n",
"#\n",
"# MACHINE LEARNING\n",
"#\n",
"# MIT LICENSE\n",
"\n",
"import numpy as np"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "YUo0Gg2UbieJ",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 1. Normalize a vector into magnitude and unit vector"
]
},
{
"metadata": {
"id": "RuZFC7k8bnRu",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"v = np.array([17,7,5,2,1])\n",
"\n",
"# m = Magnitude of v\n",
"m = None\n",
"\n",
"# v_u = unit vector of v such that m * v_u = v\n",
"v_u = None"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "sgNZ1QDCdErt",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 2. Compute the L1 and L2 distances of a pair of vectors\n",
"\n",
"Recall that the L1 distance is the \"manhattan\" distance, the sum of absolute values of a vector:\n",
"\n",
"$d_{v}^{L1} = \\sum{|v_i|}$\n",
"\n",
"The L2 distance or \"euclidean\" distance is the square root of the sum of the squares of a vector:\n",
"\n",
"$d_{v}^{L2} = \\sqrt{\\sum{v_i^2}}$"
]
},
{
"metadata": {
"id": "d__wWc1CdSv7",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"a = np.array([1,2,0,1,2,0,1,2])\n",
"b = np.array([9,2,4,1,1,5,0,2])\n",
"\n",
"# l1 = the L1 distance between a and b\n",
"l1 = None\n",
"\n",
"# l1 = the L2 distance between a and b\n",
"l2 = None"
],
"execution_count": 0,
"outputs": []
},
{
"metadata": {
"id": "mlKMlSmreNvk",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"# 3. Compute the distance between pairs of vectors and select the smallest\n",
"\n",
"For this sprint challenge goal, you are _NOT_ required to compute the pairwise distance of every point in a set. Instead, find the row $i$ with the shortest distance between $x_i$ and $y_i$, two separate sets of $2d$ points."
]
},
{
"metadata": {
"id": "N33-3PgVeIkR",
"colab_type": "code",
"colab": {}
},
"cell_type": "code",
"source": [
"x = np.array([[10., 2., 0., 3., -4., 1., -5., 10., 7., 6.],\n",
" [-4., 4., -0., 4., -9., 1., -2., 2., 1., -1.]]).T\n",
"\n",
"y = np.array([[-7., 10., -8., 7., -5., -4., 0., 7., -9., -2.],\n",
" [-7., 2., 4., 7., -5., 10., -2., -2., 1., -2.]]).T\n",
"\n",
"# Your code to find the pair of points at row i with the shortest distance between them \n",
"def get_nearest_pair():\n",
" None\n",
"\n",
"# i\n",
"i = None\n",
"\n",
"# The distance between x_i and y_i\n",
"d = None"
],
"execution_count": 0,
"outputs": []
}
]
}
Binary file added __pycache__/.DS_Store
Binary file not shown.
71 changes: 71 additions & 0 deletions precourse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,77 @@

# Free example function definition
# This function passes one of the 11 tests contained inside of test.py. Write the rest, defined in README.md, here, and execute python test.py to test. Passing this precourse work will greatly increase your odds of acceptance into the program.
import math
import numpy as np

def derivative(func, x):
epsilon = math.e**-18
return round((func(x+epsilon)-func(x))/epsilon, 3)

def f(x):
return x**2

def f_2(x):
return x**3

def f_3(x):
return x**3 + 5*x

def d_f(x):
return derivative(f, x)

def d_f_2(x):
return derivative(f_2, x)

def d_f_3(x):
return derivative(f_3, x)

def vector_sum(vectA, vectB):
vectorAdd = []
if(len(vectA) == len(vectB)):
for x,y in zip(vectA, vectB):
vectorAdd.append(x+y)
return vectorAdd

def vector_less(vectA, vectB):
vectorSub = []
if(len(vectA) == len(vectB)):
for x,y in zip(vectA, vectB):
vectorSub.append(x-y)

return vectorSub

def vector_magnitude(vectA):
squaredAddition = 0
for x in vectA:
squaredAddition = squaredAddition + x**2

return math.sqrt(squaredAddition)

def vec3():
return np.zeros(3)

def vec5():
return np.ones(5)

def vec2_1():
return np.identity(2)[0]

def vec2_2():
return np.identity(2)[1]

def matrix_multiply(matrixA, matrixB):
try:
matrixAColNos = np.shape(matrixA)[0]
except IndexError as e:
matrixAColNos = 1
try:
matrixBrowNos = np.shape(matrixB)[1]
except Exception as e:
matrixBrowNos = 1

if (matrixAColNos == matrixBrowNos):
return np.dot(matrixA, matrixB)
else:
raise Exception('The dot multiplication is not possible in this case')