Example #1
0
import sys
sys.path.append('../')
from tools import arff
from perceptron import PerceptronClassifier
mat = arff.Arff('linsep2nonorigin.arff')
data = mat.data[:, 0:-1]
labels = mat.data[:, -1].reshape(-1, 1)
PClass = PerceptronClassifier(lr=0.1, shuffle=False, deterministic=10)
PClass.fit(data, labels)
Accuracy = PClass.score(data, labels)
weights = PClass.get_weights()
print("Accuracy = [{:.2f}]".format(Accuracy))
print("Final Weights =", PClass.get_weights())
Example #2
0
from tools import arff
import itertools
from decisiontree import DTClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_breast_cancer
from sklearn import tree
import graphviz
import pprint

print("*******************PART 1************************************")

arff_path_train = r"training/lenses.arff"
arff_path_test = r"training/all_lenses.arff"
mat = arff.Arff(arff_path_train)

counts = []  ## this is so you know how many types for each column

for i in range(mat.data.shape[1]):
    counts += [mat.unique_value_count(i)]
data = mat.data[:, 0:-1]
labels = mat.data[:, -1].reshape(-1, 1)
# print(data)
# print(labels)
DTClass = DTClassifier(counts)
DTClass.fit(data, labels)
mat2 = arff.Arff(arff_path_test)
data2 = mat2.data[:, 0:-1]
labels2 = mat2.data[:, -1].reshape(-1, 1)
pred = DTClass.predict(data2)
Example #3
0
import sys

sys.path.append('../')
from tools import arff
from perceptron import PerceptronClassifier
# arff_file = "data_banknote_authentication.arff"
arff_file = "dataset_1.arff"
# arff_file = "dataset_2.arff"
mat = arff.Arff(arff_file)
np_mat = mat.data
data = mat[:, :-1]
labels = mat[:, -1].reshape(-1, 1)

#### Make Classifier ####
P2Class = PerceptronClassifier(lr=0.1, shuffle=False, deterministic=10)
P2Class.fit(data, labels)
Accuracy = P2Class.score(data, labels)
print("Accuray = [{:.2f}]".format(Accuracy))
print("Final Weights =", P2Class.get_weights())

import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import scipy
from sklearn import svm

x = data[:, 0]
y = data[:, 1]
labels_flat = np.ndarray.flatten(labels)
weights = P2Class.get_weights()
Example #4
0
import random
import matplotlib.pyplot as plt
## change Path ##
sys.path.append("/home/elkin/university/gradSchool/Fall2020/CS472/CS472")
from tools import arff, splitData, generatePerceptronData, graph_tools, list2csv
import itertools
import mlp
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder

print("PART 1")
print("DATA SET 1")

arff_path = r"training/linsep2nonorigin.arff"
dataRaw = arff.Arff(arff=arff_path, label_count=1)
data = dataRaw.data[:, 0:-1]
labels = dataRaw.data[:, -1].reshape(-1, 1)

## Define the initial Parameters ##
LR = 0.1
DET = 10
SHUFFLE = False
MOMENTUM = 0.5
VALIDATION_SIZE = 0.0
HOTENCODING = True
# data = [[0,0],[0,1]]
# BClass = mlp.MLPClassifier(lr = LR,momentum = MOMENTUM, shuffle = SHUFFLE, deterministic = DET, hidden_layer_widths =[2,2,2] ,validationSize = VALIDATION_SIZE, allWeightsValue = 0)
BClass = mlp.MLPClassifier(lr=LR,
                           momentum=MOMENTUM,
                           shuffle=SHUFFLE,
Example #5
0
    fig, ax = plt.subplots()
    sses = ax.bar(x - width / 2, sse, width, label='SSE')
    # Add some text for labels, title and custom x-axis tick labels, etc.
    ax.set_ylabel('SSE')
    ax.set_title('Total SSE for different k-values')
    ax.set_xticks(x)
    ax.set_xticklabels(k)
    ax.set_xlabel('k')
    ax.legend()
    save_path = save_path + file_name
    fig.savefig(save_path)


arff_path_train = r"training/iris.arff"
mat = arff.Arff(arff_path_train, label_count=0)
raw_data = mat.data
data = raw_data
# data = data[...,:-1]
scaler = preprocessing.MinMaxScaler()
scaler.fit(data)
norm_data = scaler.transform(data)
### KMEANS ###
graphsSSE('k_means', norm_data, 'kmeansSSE')
## HAC SINGLE LINK ###
graphsSSE('single', norm_data, 'HACsinlgesSSE')
## HAC COMPLETE LINK ###
graphsSSE('complete', norm_data, 'HACcompletesSSE')
#
# including last output column
arff_path_train = r"training/iris.arff"
Example #6
0
#                      kernel='rbf', max_iter=-1, probability=False,
#                      random_state=None, shrinking=True, tol=...,
#                      verbose=False),
#        iid=..., n_jobs=None,
#        param_grid=..., pre_dispatch=..., refit=..., return_train_score=...,
#        scoring=..., verbose=...)

### sklearn train / test split
X_train, X_test, y_train, y_test = train_test_split(train_data,
                                                    train_labels,
                                                    test_size=0.33,
                                                    random_state=42)

### Some Arff loading
arff_path = r"./data/creditapproval.arff"
credit_approval = arff.Arff(arff=arff_path, label_count=1)

print(credit_approval.data)  # the numpy array

## Graph 2 of the features as a scatter plot
x = credit_approval[:, 1]
y = credit_approval[:, 2]
train_labels = credit_approval[:, -1]
graph_tools.graph(x=x,
                  y=y,
                  labels=train_labels,
                  xlim=(0, 30),
                  ylim=(0, 30),
                  title="Credit Plot")  # little grapher wrapper

## Same idea, but manually graphing each label / category