In [103]:
import numpy as np
import scipy as sp
import pandas as pd
import statsmodels.api as sm
import statsmodels.formula.api as smf
import sklearn as sk
import matplotlib as mpl
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sns
sns.set()
sns.set_color_codes()

%matplotlib inline
%config InlineBackend.figure_format='png'

# python 2
from __future__ import division

# 플랏 크기 조절
fig_size = plt.rcParams["figure.figsize"]
fig_size[0] = 15
fig_size[1] = 9
plt.rcParams["figure.figsize"] = fig_size
mpl.rc('xtick', labelsize = 20)
mpl.rc('ytick', labelsize = 20)

In [5]:
a = np.array([
        [1],
        [2]
    ])

In [36]:
# 점찍기
plt.plot(0,0, 'ro', ms = 10)
plt.plot(a[0],a[1], 'ro', ms=10)

# 화살표 그리기
plt.annotate("",xy=a,xytext=(0,0), arrowprops=dict(facecolor="black"))

# 텍스트 기입하기
plt.text(0.2, 1.0, "$a$", fontsize=30)

# x축 / y축 범위
plt.xlim(-1, 2)
plt.ylim(-0.5, 2.5)
plt.show()


벡터의 길이 구하기 (norm)

$||a|| = \sqrt{a^T a} = \sqrt{a_1^2 + ... a_n^2}$


In [39]:
a = np.array([[2],[2]])
np.linalg.norm(a)


Out[39]:
2.8284271247461903

단위 벡터

길이가 1인 벡터를 단위벡터라고 한다.


In [80]:
# 단위 벡터를 찾는 함수

def unit_vector_finder(vector_list):
    return [ i  for i in vector_list if np.linalg.norm(i) == 1]


# 벡터의 길이를 구해서 딕셔너리로 리턴해주는 함수

def vector_norm(vector_list):
    return { idx: np.linalg.norm(data) for idx, data in enumerate(vector_list)}

In [100]:
import random
generato_r = random.Random()
vector_list = np.array([])
for _ in range(100):
    a = generato_r.gauss(10,5)
    b = generato_r.uniform(1,7)
    temp_vector = np.array([[a],[b]])
    vector_list = np.append(vector_list, temp_vector)
    print(temp_vector)
vector_list = vector_list.reshape(100,2)

vector_norm(unit_vector_finder(vector_list))


[[ 10.93132989]
 [  2.51071484]]
[[ 9.83128955]
 [ 4.97456637]]
[[ 8.77087748]
 [ 6.44491589]]
[[ 12.65060591]
 [  3.85718319]]
[[ 7.20492815]
 [ 1.64714067]]
[[ 9.8762974 ]
 [ 1.30030716]]
[[ 23.63232971]
 [  5.84722112]]
[[ 12.66026517]
 [  2.84168478]]
[[ 16.95924166]
 [  5.62962803]]
[[ 1.25504479]
 [ 6.53944252]]
[[ 7.48699903]
 [ 4.68772292]]
[[ 20.95919784]
 [  5.01024967]]
[[ 8.14096807]
 [ 5.63729524]]
[[ 11.4603505 ]
 [  5.81022565]]
[[ 2.47576888]
 [ 6.53274507]]
[[ 3.20121709]
 [ 5.48381056]]
[[ 9.92834209]
 [ 5.42127011]]
[[ 17.27766124]
 [  3.26773867]]
[[ 6.13208732]
 [ 5.23077569]]
[[ 6.98081811]
 [ 5.72955296]]
[[ 8.2288794 ]
 [ 4.82606121]]
[[ 5.05359914]
 [ 1.27475034]]
[[ 10.50306792]
 [  3.39071744]]
[[ 12.89504011]
 [  3.72019675]]
[[ 6.19026459]
 [ 1.0466502 ]]
[[ 7.99578936]
 [ 6.4842416 ]]
[[ 5.28203206]
 [ 6.88549665]]
[[ 8.820302]
 [ 4.080219]]
[[ 14.02185195]
 [  4.21247   ]]
[[ 11.41531049]
 [  5.97955182]]
[[ 11.63370281]
 [  1.34147979]]
[[ 8.16207896]
 [ 3.69191529]]
[[ 6.50413423]
 [ 4.60876155]]
[[ 15.19542363]
 [  1.78077801]]
[[ 13.93103493]
 [  4.98230087]]
[[ 11.99029559]
 [  3.70491717]]
[[ 8.03869522]
 [ 2.29923308]]
[[ 17.22209178]
 [  4.2622991 ]]
[[ 21.20356817]
 [  1.727959  ]]
[[ 7.88905222]
 [ 3.48132183]]
[[ 4.75617727]
 [ 4.00711599]]
[[ 15.0113219 ]
 [  4.72117304]]
[[ 10.42806547]
 [  3.47623471]]
[[ 2.94456053]
 [ 4.56793233]]
[[ 9.1027695 ]
 [ 6.08754256]]
[[ 6.22989929]
 [ 3.09522723]]
[[ 7.39191455]
 [ 1.86811915]]
[[ 19.97707471]
 [  3.68607569]]
[[ 4.49897997]
 [ 2.9722509 ]]
[[ 15.95531918]
 [  3.98346809]]
[[ 10.68040796]
 [  5.71741383]]
[[ 9.50539932]
 [ 3.359812  ]]
[[ 13.56085137]
 [  5.68379882]]
[[ 7.34662962]
 [ 3.04990792]]
[[ 8.32031343]
 [ 1.04632822]]
[[ 10.64281964]
 [  1.05312217]]
[[ 5.93517063]
 [ 5.27111752]]
[[ 12.11248745]
 [  1.86100131]]
[[ 11.75967663]
 [  6.70783954]]
[[ 8.53311945]
 [ 5.37354761]]
[[ 12.75016375]
 [  1.9219751 ]]
[[ 3.43178918]
 [ 3.07599622]]
[[ 12.55071458]
 [  4.79420986]]
[[ 15.49486738]
 [  4.75578184]]
[[ 12.14086736]
 [  3.1104516 ]]
[[ 20.94523725]
 [  1.42594952]]
[[ 0.9052099 ]
 [ 6.50173577]]
[[ 7.71089876]
 [ 5.47551036]]
[[ 15.98265551]
 [  6.31285205]]
[[ 12.12025109]
 [  5.22345322]]
[[ 8.23453607]
 [ 4.50978856]]
[[ 10.9870497 ]
 [  2.93140544]]
[[ 12.896495  ]
 [  2.29719282]]
[[ 10.67005175]
 [  4.85191762]]
[[ 16.11476472]
 [  3.87120107]]
[[ 10.2873966 ]
 [  1.29164854]]
[[ 8.95338074]
 [ 6.40754139]]
[[ 5.59805996]
 [ 4.99337561]]
[[ 9.98094217]
 [ 6.02461527]]
[[ 10.01240369]
 [  3.06254582]]
[[ 7.6467122 ]
 [ 1.51011773]]
[[ 9.91241023]
 [ 4.59350351]]
[[ 9.44568526]
 [ 3.72314367]]
[[ 5.27054225]
 [ 4.90992054]]
[[ 13.91374393]
 [  4.91485365]]
[[ 11.87967998]
 [  1.950201  ]]
[[ 3.53106103]
 [ 2.24248608]]
[[ 8.95773278]
 [ 3.20803412]]
[[ 2.59341604]
 [ 5.35716467]]
[[ 6.92630555]
 [ 2.83053681]]
[[ 10.03958998]
 [  1.85119309]]
[[ 15.90150896]
 [  1.56711866]]
[[ 12.02941786]
 [  2.15376994]]
[[ 5.75946962]
 [ 1.5893711 ]]
[[ 0.22942181]
 [ 2.84243955]]
[[ 5.68653423]
 [ 4.17689895]]
[[ 15.17753943]
 [  1.18554927]]
[[ 9.04141807]
 [ 6.1632557 ]]
[[ 7.81840591]
 [ 3.27583945]]
[[ 13.57139258]
 [  1.77049901]]
Out[100]:
{}

In [ ]:
b = generato_r.uniform

In [ ]:
a = generato_r.gauss

In [84]:
{i:i for i in range(3)}


Out[84]:
{0: 0, 1: 1, 2: 2}

In [76]:



<enumerate object at 0x7ff06be837d0>

In [56]:
vector_list.reshape(100,2)


Out[56]:
array([[  34.,   54.],
       [  25.,   36.],
       [  19.,   10.],
       [  77.,   78.],
       [  49.,   96.],
       [  81.,   20.],
       [  15.,   21.],
       [  93.,   79.],
       [  84.,   28.],
       [  24.,   15.],
       [  66.,   58.],
       [  10.,   54.],
       [  79.,   62.],
       [  26.,   75.],
       [  34.,    2.],
       [  55.,   66.],
       [  97.,   47.],
       [  18.,   24.],
       [   6.,   45.],
       [  67.,   16.],
       [   3.,   72.],
       [  80.,   28.],
       [  30.,   85.],
       [  21.,   36.],
       [   3.,   45.],
       [   1.,   90.],
       [  87.,   58.],
       [  10.,   53.],
       [   7.,    0.],
       [  91.,   58.],
       [  89.,   92.],
       [ 100.,    8.],
       [  47.,   35.],
       [  84.,   24.],
       [  53.,   61.],
       [  19.,   52.],
       [  47.,   60.],
       [  12.,   81.],
       [  41.,   62.],
       [  55.,   75.],
       [  43.,   81.],
       [   0.,   13.],
       [  97.,   64.],
       [  48.,   12.],
       [  81.,   77.],
       [  84.,   71.],
       [  19.,   92.],
       [  68.,   19.],
       [  98.,   21.],
       [   3.,   26.],
       [  90.,   33.],
       [  50.,   68.],
       [  35.,   82.],
       [  13.,   50.],
       [  75.,   13.],
       [  79.,   60.],
       [  69.,   20.],
       [  60.,    0.],
       [  52.,   73.],
       [  49.,   44.],
       [   3.,   55.],
       [  82.,   53.],
       [  48.,   68.],
       [  36.,   99.],
       [  91.,   66.],
       [  92.,   79.],
       [  92.,   14.],
       [  51.,   28.],
       [  72.,   78.],
       [  12.,   93.],
       [  88.,   26.],
       [  58.,   34.],
       [  52.,   94.],
       [  36.,   74.],
       [  67.,   41.],
       [  41.,   98.],
       [  50.,   47.],
       [  16.,   55.],
       [  70.,   26.],
       [  74.,   40.],
       [  59.,   87.],
       [  59.,   98.],
       [  60.,   86.],
       [  61.,   68.],
       [  93.,    1.],
       [  56.,   52.],
       [  87.,   72.],
       [  56.,   20.],
       [  33.,   38.],
       [  49.,    8.],
       [  19.,   53.],
       [  11.,   47.],
       [  91.,    1.],
       [  75.,   45.],
       [  74.,   21.],
       [  61.,    5.],
       [  95.,   99.],
       [  26.,   14.],
       [  86.,    2.],
       [  37.,   96.]])

In [86]:
random.random?

벡터의 합


In [130]:
a = np.array([1,2])
b = np.array([2,1])
c = a + b

plt.annotate('', xy=a, xytext=(0,0), arrowprops=dict(facecolor='gray'))
plt.annotate('', xy=b, xytext=(0,0), arrowprops=dict(facecolor='gray'))
plt.annotate('', xy=c, xytext=(0,0), arrowprops=dict(facecolor='black'))
plt.plot([a[0],c[0]],[a[1],c[1]], 'k--')
plt.plot([b[0],c[0]],[b[1],c[1]], 'k--')


plt.plot(a[0],a[1],'ro',ms=10)
plt.plot(b[0],b[1],'ro',ms=10)

plt.xlim(-1,4)

plt.ylim(-1,5)


Out[130]:
(-1, 5)

In [120]:
plt.annotate?

In [131]:
a = np.array([1,1])
b = np.array([-1,1])

In [132]:
np.dot(a,b)


Out[132]:
0

In [138]:
a = np.array([1,2])
b = np.array([2,0])
a2 = np.dot(a,b) / np.linalg.norm(b) * np.array([1,0])

In [139]:
a1 = a- a2

In [140]:



Out[140]:
array([ 0.,  2.])

In [ ]: