-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupport_machine_vector.py
More file actions
48 lines (40 loc) · 1.29 KB
/
Support_machine_vector.py
File metadata and controls
48 lines (40 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#with fake generated data
import random
import pandas as pd
from scipy import stats
from sklearn.metrics import r2_score
import statsmodels.api as sm
from mpl_toolkits import mplot3d
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.preprocessing import scale
from sklearn import svm,datasets
def creatClusterData(N,k):
random.seed(5) #to get same data everytime
pointsPerCluster=float(N)/k
X=[]
y=[]
for i in range(k):
incomeCentroid=random.uniform(20000.0,200000.0)
ageCentroid=random.uniform(20.0,70.0)
for j in range(int(pointsPerCluster)):
X.append([np.random.normal(incomeCentroid,10000.0),np.random.normal(ageCentroid,2.0)])
y.append(i)
X=np.array(X)
y=np.array(y)
return X,y
def plotPrediction(X,clf):
xx,yy=np.meshgrid(np.arange(0,250000,10),np.arange(10,70,0.5))
z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
plt.figure(figsize=(10,7))
z=z.reshape(xx.shape)
plt.contourf(xx,yy,z,cmap=plt.cm.Paired,alpha=0.8)
plt.scatter(X[:,0],X[:,1],c=y.astype(np.float))
plt.show()
X,y=creatClusterData(100,5)
plt.figure(figsize=(10,7))
plt.scatter(X[:,0],X[:,1],c=y.astype(np.float))
plt.show()
svc=svm.SVC(kernel='linear',C=1.0).fit(X,y)
plotPrediction(X,svc)