Machine Learning - Perceptron

i have no clue bad code

In [16]:
from matplotlib import pyplot as plt
import seaborn as sb
import pandas as pd
import numpy as np

Loading data with seaborn

In [17]:
ds = sb.load_dataset("iris")
In [18]:
print(
    "Number of attributes:", ds.shape[1],
    "\nNumber of patterns: ", ds.shape[0]
)
Number of attributes: 5 
Number of patterns:  150

Creating a DataFrame with 4 patterns of each species

In [4]:
x = pd.DataFrame()
for i in ds.species.unique():
    x = x.append(ds.loc[ds["species"] == i].head(4))
x    
Out[4]:
sepal_length sepal_width petal_length petal_width species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
50 7.0 3.2 4.7 1.4 versicolor
51 6.4 3.2 4.5 1.5 versicolor
52 6.9 3.1 4.9 1.5 versicolor
53 5.5 2.3 4.0 1.3 versicolor
100 6.3 3.3 6.0 2.5 virginica
101 5.8 2.7 5.1 1.9 virginica
102 7.1 3.0 5.9 2.1 virginica
103 6.3 2.9 5.6 1.8 virginica

Plotting the data

Plot of 4 patterns of each species

In [25]:
sb.scatterplot(x="sepal_length",
                y="petal_length",
                data=x,
                hue="species",
                style="species")
Out[25]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f69f7dcdee0>

Plot of all patterns

In [6]:
sb.scatterplot(x="sepal_length",
                y="petal_length",
                data=ds,
                hue="species",
                style="species")
Out[6]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f69f7fed340>
In [24]:
t = np.zeros(ds.shape[0])
ans = True

while ans:
    choice = input(
        "1. Διαχωριςμόσ Iris-setosa από ( Iris-versicolor και Iris-virginica)\n"
        "2. Διαχωριςμόσ Iris-virginica από ( Iris-setosa και Iris-versicolor)\n"
        "3. Διαχωριςμόσ Iris-versicolor από ( Iris-setosa και Iris-virginica)\n"
    )

    map_dict = dict()
    if choice == "1":
        map_dict = {"setosa": 1, "versicolor":0, "virginica":0}
    elif choice == "2":
        map_dict = {"setosa": 0, "versicolor":1, "virginica":0}
    elif choice == "3":
        map_dict = {"setosa": 0, "versicolor":0, "virginica":1}
    
    for i in range(len(t)):
        t[i] = map_dict[ds["species"][i]]

    ans = False
    
print(t)
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.
 1. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0.]
In [25]:
ds["New_column"] = 1

Χωρισμός προτύπων σε πρότυπα εκπαίδευσης και ανάκλησης

In [26]:
xtrain = pd.DataFrame()
for i in ds.species.unique():
    xtrain = xtrain.append(ds.loc[ds["species"] == i].head(40))
xtrain.head()
Out[26]:
sepal_length sepal_width petal_length petal_width species New_column
0 5.1 3.5 1.4 0.2 setosa 1
1 4.9 3.0 1.4 0.2 setosa 1
2 4.7 3.2 1.3 0.2 setosa 1
3 4.6 3.1 1.5 0.2 setosa 1
4 5.0 3.6 1.4 0.2 setosa 1
In [27]:
xtest = pd.DataFrame()
for i in ds.species.unique():
    xtest = xtest.append(ds.loc[ds["species"] == i].tail(10))
xtest.head()
Out[27]:
sepal_length sepal_width petal_length petal_width species New_column
40 5.0 3.5 1.3 0.3 setosa 1
41 4.5 2.3 1.3 0.3 setosa 1
42 4.4 3.2 1.3 0.2 setosa 1
43 5.0 3.5 1.6 0.6 setosa 1
44 5.1 3.8 1.9 0.4 setosa 1
In [28]:
ttrain = np.zeros(len(xtrain.index))
j=0
for i in xtrain.index:
    ttrain[j] = t[i]
    j+=1
ttrain
Out[28]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.,
       1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
       0.])
In [29]:
ttest = np.zeros(len(xtest.index))
j=0
for i in xtest.index:
    ttest[j] = t[i]
    j+=1
ttest
Out[29]:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0.,
       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

Plotting time... again..

In [16]:
plot = sb.scatterplot(x="sepal_length",
                      y="petal_length",
                      data=xtrain)
plot = sb.scatterplot(x="sepal_length",
                      y="petal_length",
                      data=xtest)
In [23]:
choice2 = input(
    "1. Υλοποίηση με Perceptron\n"
    "2. Υλοποίηση με Adaline\n"
    "3. Υλοποίηση με Λύση Ελάχιστων Τετραγώνων\n"
    "4. Επιστροφή στο αρχικό Menu\n"
)

if choice2 == "1":
    perceptron()
elif choice2 == "2":
    adaline()
elif choice2 == "3":
    lsq()
elif choice == "4":
    print("Back to menu")
    
ans = input(
    "Do you want to continue (y/n)?\n"
)

if ans == "y":
    ans = True
else:
    ans = False
perceptron
In [20]:
def perceptron():
    print("perceptron")
In [21]:
def adaline():
    print("adaline")
In [22]:
def lsq():
    print("lsq")
In [3]:
ro=0
while(ro<10):
    ro+=1
    if ro==5:
        continue
    print(ro)
1
2
3
4
6
7
8
9
10

Load libraries

In [8]:
from matplotlib import pyplot as plt
import seaborn as sb
import pandas as pd
import numpy as np

Perceptron function

In [ ]:
def perceptron():
    print("perceptron")

Adaline function

In [9]:
def adaline():
    print("adaline")

Least squares function

In [10]:
def lsq():
    print("lsq")

Menu function

In [13]:
def menu(menu_text, valid_options):
    menu_text = (
        "========================================\n"
        + menu_text
        + "\n========================================\n"
        )

    choice = int(input(menu_text))

    while True:
        if choice not in valid_options:
            choice = int(input(menu_text))
        else:
            return choice

Main function

In [15]:
def main():
    ds = sb.load_dataset("iris")
    print(
        "Number of attributes:", ds.shape[1],
        "\nNumber of patterns: ", ds.shape[0]
    )
    
    sb.scatterplot(x="sepal_length",
                   y="petal_length",
                   data=ds,
                   hue="species",
                   style="species")
    plt.show()

    t = np.zeros(ds.shape[0])
    ans = True
    
    while ans:
        choice = menu(
            "1. Διαχωριςμόσ Iris-setosa από ( Iris-versicolor και Iris-virginica)\n"
            "2. Διαχωριςμόσ Iris-virginica από ( Iris-setosa και Iris-versicolor)\n"
            "3. Διαχωριςμόσ Iris-versicolor από ( Iris-setosa και Iris-virginica)",
            [1,2,3]
        )
    
        map_dict = dict()
        if choice == 1:
            map_dict = {"setosa": 1, "versicolor":0, "virginica":0}
        elif choice == 2:
            map_dict = {"setosa": 0, "versicolor":1, "virginica":0}
        else:
            map_dict = {"setosa": 0, "versicolor":0, "virginica":1}
    
        for i in range(len(t)):
            t[i] = map_dict[ds["species"][i]]
        
        ds["New_column"] = 1
    
        xtrain = pd.DataFrame()
        for i in ds.species.unique():
            xtrain = xtrain.append(ds.loc[ds["species"] == i].head(40))
        
        xtest = pd.DataFrame()
        for i in ds.species.unique():
            xtest = xtest.append(ds.loc[ds["species"] == i].tail(10))
        
        ttrain = np.zeros(len(xtrain.index))
        j=0
        for i in xtrain.index:
            ttrain[j] = t[i]
            j+=1
        
        ttest = np.zeros(len(xtest.index))
        j=0
        for i in xtest.index:
            ttest[j] = t[i]
            j+=1
        
        plot = sb.scatterplot(x="sepal_length",
                              y="petal_length",
                              data=xtrain)
        plot = sb.scatterplot(x="sepal_length",
                              y="petal_length",
                              data=xtest)
        plt.show()
    
        choice2 = menu(
            "1. Υλοποίηση με Perceptron\n"
            "2. Υλοποίηση με Adaline\n"
            "3. Υλοποίηση με Λύση Ελάχιστων Τετραγώνων\n"
            "4. Επιστροφή στο αρχικό Menu",
            [1,2,3,4]
        )
    
        if choice2 == 1:
            perceptron()
        elif choice2 == 2:
            adaline()
        elif choice2 == 3:
            lsq()
        else:
            continue

        if menu("1.Again\n2.exit\n", [1,2]) == 1:
            ans = True
        else:
            ans = False

math meme I can't do sh$t

In [ ]:
# variables
maxepochs = int(input("Max: "))
beta = float(input("Beta: "))
In [ ]:
def perceptron(xtrain, ttrain, maxepochs, beta)
In [33]:
 
In [34]:
 
In [15]:
 
In [15]:
 
In [ ]: