In [4]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
In [2]:
from scipy.stats import multivariate_normal
x = np.linspace(0, 5, 10, endpoint=False)
y = multivariate_normal.pdf(x, mean=2.5, cov=0.5)
In [5]:
plt.plot(x, y)
plt.show()
In [6]:
df= pd.read_csv("./pokemon.csv")
In [7]:
df.columns
Out[7]:
In [8]:
pknormal = df[df['attack_strong_type'] == 'Normal']
In [9]:
pkflying = df[df['attack_strong_type'] == 'Flying']
In [10]:
pknormal
Out[10]:
In [11]:
pkflying
Out[11]:
In [12]:
normal_f1 = pknormal['hp']
normal_f2 = pknormal['weight']
flying_f1 = pkflying['hp']
flying_f2 = pkflying['weight']
In [13]:
x_c1 = np.array(zip(normal_f1.tolist(),normal_f2.tolist()))
x_c2 = np.array(zip(flying_f1.tolist(),flying_f2.tolist()))
c1_mean = np.mean(x_c1,axis=0)
c2_mean = np.mean(x_c2,axis=0)
xc1_xc1 = x_c1-c1_mean
xc2_xc2 = x_c2-c2_mean
In [14]:
cov_c1 = np.matmul(xc1_xc1.transpose(),xc1_xc1)/c1_mean.shape[0]
cov_c2 = np.matmul(xc2_xc2.transpose(),xc2_xc2)/c2_mean.shape[0]
In [15]:
x = np.append(x_c1,x_c2,axis=0)
In [16]:
y = [0] * x_c1.shape[0] + [1]*x_c2.shape[0]
In [17]:
cov = (cov_c1*len(x_c1) + cov_c2*len(x_c2))/(len(x_c1)+len(x_c2))
y1 = multivariate_normal.pdf(x, mean=c1_mean, cov=cov_c1)
y2 = multivariate_normal.pdf(x, mean=c2_mean, cov=cov_c2)
plt.plot(y1)
plt.plot(y2)
plt.show()
In [18]:
error =0
for label,c1p,c2p in zip(y,y1,y2):
if c1p > c2p and label == 1:
error += 1
elif c1p < c2p and label ==0:
error +=1
print("錯了 {} 個".format(error))
In [19]:
cov = (cov_c1*len(x_c1) + cov_c2*len(x_c2))/(len(x_c1)+len(x_c2))
y1 = multivariate_normal.pdf(x, mean=c1_mean, cov=cov)
y2 = multivariate_normal.pdf(x, mean=c2_mean, cov=cov)
plt.plot(y1)
plt.plot(y2)
plt.show()
In [20]:
error =0
for label,c1p,c2p in zip(y,y1,y2):
if c1p > c2p and label == 1:
error += 1
elif c1p < c2p and label ==0:
error +=1
print("錯了 {} 個".format(error))
In [ ]: