Mittwoch, 29. Mai 2019

Künstliche Intelligenz SourceCode Python Teil-10

import numpy as np
from sklearn import svm
fFloat  = open("Autoklassifizierung.csv","r")
dataset = np.loadtxt(fFloat, delimiter=",")
fFloat.close()
y = dataset[:,0]
x = dataset[:,1:3]
xMin = x.min(axis=0); xMax = x.max(axis=0)
x = (x - xMin) / (xMax - xMin)
svmLin = svm.SVC(kernel='linear', C=100) #*\label{code:svm:linear:0}
svmLin.fit(x,y)
w = svmLin.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-1, 1,50)
yy = a * xx - (svmLin.intercept_[0]) / w[1]
margin = 1 / np.sqrt(np.sum(svmLin.coef_ ** 2))  #*\label{code:svm:linear:1}
yMarginDown = yy - np.sqrt(1 + a ** 2) * margin
yMarginUp   = yy + np.sqrt(1 + a ** 2) * margin
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(xx,yy,'k', linewidth=1.5)
plt.plot(xx, yMarginDown, 'k--')
plt.plot(xx, yMarginUp, 'k--')
iA = np.flatnonzero(y==1)
iB = np.flatnonzero(y==0)
ax.scatter(x[iA,0],x[iA,1],alpha=0.6,c='red', marker='+', linewidth=1.5)
ax.scatter(x[iB,0],x[iB,1],alpha=0.6,c='black', marker='o', linewidth=1.5)
ax.set_xlabel('$x_0$'); ax.set_ylabel('$x_1$')
ax.set_ylim([-0.25,1.25]); ax.set_xlim([0,1])
ax.set_title("Berechnet mit C=100
:::::::::::::::::::::::::::::::::::::::::
import numpy as np
from sklearn import svm
fFloat  = open("Auto2MerkmaleClass.csv","r")
dataset = np.loadtxt(fFloat, delimiter=",")
fFloat.close()
yTrain = dataset[:,0]
x = dataset[:,1:3]
xMin = x.min(axis=0); xMax = x.max(axis=0)
XTrain = (x - xMin) / (xMax - xMin)
svmRBF = svm.SVC(kernel='rbf', decision_function_shape='ovr', gamma=1)
svmRBF.fit(XTrain,yTrain)
XX, YY = np.mgrid[0:1:0.01, 0:1:0.01]
X = np.array([XX.ravel(), YY.ravel()]).T
yP = svmRBF.predict(X)
indexA = np.flatnonzero(yTrain==0)
indexB = np.flatnonzero(yTrain==1)
indexC = np.flatnonzero(yTrain==2)
indexD = np.flatnonzero(yTrain==3)
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
yP = yP.reshape(XX.shape)
ax.pcolormesh(XX, YY, yP, cmap=plt.cm.tab20)
ax.scatter(XTrain[indexA,0],XTrain[indexA,1],color='black', marker='o')
ax.scatter(XTrain[indexB,0],XTrain[indexB,1],color='black', marker='x')
ax.scatter(XTrain[indexC,0],XTrain[indexC,1],color='black', marker='+')
ax.scatter(XTrain[indexD,0],XTrain[indexD,1],color='black', marker='*')
ax.set_xlabel('$x_0$')
ax.set_ylabel('$x_1$')
ax.set_xlim([0,1])
ax.set_ylim([0,1])
ax.set_title("Klassifikation mit RBF ($\gamma=1$)")

::::::::::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
from twoMoonsProblem import twoMoonsProblem
from sklearn import svm
(XTrain,yTrain) = twoMoonsProblem()
svmP2 = svm.SVC(kernel='poly', degree=7, decision_function_shape='ovr', C=1000)
svmP2.fit(XTrain,yTrain)
XX, YY = np.mgrid[-1:2:0.01, -1:2:0.01]
X = np.array([XX.ravel(), YY.ravel()]).T
yP = svmP2.predict(X)
indexA = np.flatnonzero(yTrain>0.5)
indexB = np.flatnonzero(yTrain<0 .5="" p="">import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
yP = yP.reshape(XX.shape)
ax.pcolormesh(XX, YY, yP, cmap=plt.cm.Set1)
ax.scatter(XTrain[indexA,0],XTrain[indexA,1],color='white', marker='o')
ax.scatter(XTrain[indexB,0],XTrain[indexB,1],color='black', marker='+')
ax.set_xlabel('$x_0$')
ax.set_ylabel('$x_1$')
ax.set_xlim([-1,2])
ax.set_ylim([-1,2])
ax.set_title("Klassifikation mit Polynom-Kernel (degree=7, C=1000)")

svmRBF = svm.SVC(kernel='rbf', decision_function_shape='ovr')
svmRBF.fit(XTrain,yTrain)
yP = svmRBF.predict(X)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
yP = yP.reshape(XX.shape)
ax.pcolormesh(XX, YY, yP, cmap=plt.cm.Set1)
ax.scatter(XTrain[indexA,0],XTrain[indexA,1],color='white', marker='o')
ax.scatter(XTrain[indexB,0],XTrain[indexB,1],color='black', marker='+')
ax.set_xlabel('$x_0$')
ax.set_ylabel('$x_1$')
ax.set_xlim([-1,2])
ax.set_ylim([-1,2])
ax.set_title("Klassifikation mit RBF-Kernel")
svmLinear = svm.SVC(kernel='linear', decision_function_shape='ovr')
svmLinear.fit(XTrain,yTrain)
yP = svmLinear.predict(X)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
yP = yP.reshape(XX.shape)
ax.pcolormesh(XX, YY, yP, cmap=plt.cm.Set1)
ax.scatter(XTrain[indexA,0],XTrain[indexA,1],color='white', marker='o')
ax.scatter(XTrain[indexB,0],XTrain[indexB,1],color='black', marker='+')
ax.set_xlabel('$x_0$')
ax.set_ylabel('$x_1$')
ax.set_xlim([-1,2])
ax.set_ylim([-1,2])
ax.set_title("Klassifikation mit linearem Kernel")
svmSIG = svm.SVC(kernel='sigmoid', decision_function_shape='ovr')
svmSIG.fit(XTrain,yTrain)
yP = svmSIG.predict(X)
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
yP = yP.reshape(XX.shape)
ax.pcolormesh(XX, YY, yP, cmap=plt.cm.Set1)
ax.scatter(XTrain[indexA,0],XTrain[indexA,1],color='white', marker='o')
ax.scatter(XTrain[indexB,0],XTrain[indexB,1],color='black', marker='+')
ax.set_xlabel('$x_0$')
ax.set_ylabel('$x_1$')
ax.set_xlim([-1,2])
ax.set_ylim([-1,2])
ax.set_title("Klassifikation mit Sigmoid-Kernel")

::::::::::::::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
def twoMoonsProblem( SamplesPerMoon=240, pNoise=2):
    tMoon0 = np.linspace(0, np.pi, SamplesPerMoon)
    tMoon1 = np.linspace(0, np.pi, SamplesPerMoon)
    Moon0x = np.cos(tMoon0)
    Moon0y = np.sin(tMoon0)
    Moon1x = 1 - np.cos(tMoon1)
    Moon1y = 0.5 - np.sin(tMoon1)
    X = np.vstack((np.append(Moon0x, Moon1x), np.append(Moon0y, Moon1y))).T
    X = X + pNoise/100*np.random.normal(size=X.shape)
    Y = np.hstack([np.zeros(SamplesPerMoon), np.ones(SamplesPerMoon)])
    return X, Y
if __name__ == '__main__':
   
    (X,Y) = twoMoonsProblem()
   
    import matplotlib.pyplot as plt
    fig = plt.figure(1)
    ax = fig.add_subplot(1,1,1)
    indexA = np.flatnonzero(Y>0.5)
    indexB = np.flatnonzero(Y<0 .5="" br="">    ax.scatter(X[indexA,0],X[indexA,1],color='red', marker='o')
    ax.scatter(X[indexB,0],X[indexB,1],color='black', marker='+')
    ax.set_xlabel('$x_0$')
    ax.set_ylabel('$x_1$')
    ax.set_ylim([-1,2])
    ax.set_ylim([-1,2])
    ax.set_title("Two Moons Set")

::::::::::::::::::::::::::::::::::::::::::::::::::::::
import numpy as np
np.random.seed(42)
xO = np.array([ [0,0,1], [0,1,0],[1,0,0],[1,1,0]])
y = np.array([0, 1, 1, 0])
x = np.ones( (len(y),4) )
x[:,0:3] = xO
def myHeaviside(x):
    y = np.ones_like(x,dtype=np.float)
    y[x <= 0] = 0
    return(y)
t = 0; tmax=100000
eta = 0.25
Dw = np.zeros(4)
w = np.random.rand(4) - 0.5
convergenz = 1   
while (convergenz > 0) and (t    t = t +1;
    WaehleBeispiel = np.random.randint(len(y))
    xB = x[WaehleBeispiel,:].T
    yB = y[WaehleBeispiel]
    error = yB - myHeaviside(w@xB)
    for j in range(len(xB)):
        Dw[j]= eta*error*xB[j]
        w[j] = w[j] + Dw[j]
    convergenz =  np.linalg.norm(y-myHeaviside(w@x.T))

def predict(x,w):
    xC = np.ones( (x.shape[0],4) )
    xC[:,0:3] = x
    y = w@xC.T
    y[y>0] = 1
    y[y<= 0] = 0
    return(y)
yPredict = predict(xO,w)
print(yPredict)
::::::::::::::::::::::::::::::::::::::::::::::::

Keine Kommentare:

Kommentar veröffentlichen

Hinweis: Nur ein Mitglied dieses Blogs kann Kommentare posten.