Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(Boruta)
- library(funModeling)
- library(arules)
- library(rpart)
- library(datasets)
- # Ładujemy pliki
- data_row = read.csv(file = 'r/b2.csv')
- data_all = data_row[2:(length(data_row))]
- # Pobieramy nazyw kolumn
- cols = colnames(data_all)
- # Tworzmy obiekt data.frame
- df = data.frame(data_all)
- # Odzielamy cechy
- X = df[,1:ncol(df)-1]
- # Oraz nazwy cech
- X_cols = colnames(X)
- # Odzielamy etykiety próbek
- y = df[,ncol(df)]
- #Tutaj korzystam z zadanego algortmu do ustalenia wag cech
- boruta_train <- Boruta(y~.,data=X)
- print(boruta_train)
- # Tablica wag (bedziemy ja rysować)
- importance_table = attStats(boruta_train)
- plot(boruta_train)
- # Interesuja nas same średnie z powyższych tablic
- importance_mean_list = attStats(boruta_train)[,1]
- print(attStats(boruta_train))
- # Uporządkujmy co mamy, czyli połączmy waga cechy + nazwa cechy
- cols_importance = data.frame(importance=importance_mean_list, feature=X_cols)
- print(cols_importance) # - tablica ze dokumentu ręcznego
- # Dyskretyzacja
- discretized_features = discretizeDF(cols_importance, default = list(method = "cluster", breaks = 2)) # dykretyzacja,
- # Sortowanie (tylko w celach etetycznych)
- discretized_features = discretized_features[order(discretized_features$importance),]
- print(discretized_features)
- ### Grupowanie ###
- # Będzimy rozdzielać cechy wg przedziałów,
- groups = list()
- intervals = unique(discretized_features[1])
- for (i in 1:nrow(intervals)){
- # lista dla i-przedzialu
- single_interval_values = subset( discretized_features,importance==intervals[i,1] )
- print(single_interval_values)
- str = single_interval_values[,2]
- groups[[i]] = str
- }
- print(groups)
- # Dla kazdej grupy policzymy nowa liste cech, cech będzie tyle ile powstałych grup
- new_X = data.frame(V1 = 1:length(y)) # tworzymy liste o dlugosc ilosci danych
- for (i in 1:length(groups)){
- ### sortowanie po kolumnahc - > X[ , order(names(X))]
- group = groups[[i]] # wezmy grupe cech np WC.TA i S.TA
- #paste( unlist(group), collapse=',')
- #
- str = paste( unlist(group), collapse=',')
- str_list = strsplit(str,',')
- old_features = X[unlist(str_list)] # weźmy pierwotne cechy które przydzielono do tej grupy
- weights = cols_importance$importance[cols_importance$feature %in% group] #weźmy obliczone wagi dla tych cech
- weights_matrix = data.frame(id=1:length(y), data.frame(t(weights)) ) #utwórzmy macierz mnoznikow
- weights_matrix = weights_matrix[1:length(old_features)+1] #odrzućmy iterator (licznik), bo nie jest potrzebny
- colnames(weights_matrix) = group
- # Dane w old_features zmieniły kolejnośc, wiec wszystko wysortujmy po kolumnach.
- # Nigdy nie bedzie 2x tego same atrybutu wiec tym sie nie martwimy
- old_features = old_features[ , order(names(old_features))]
- weights_matrix= weights_matrix[ , order(names(weights_matrix))]
- weighted_features = data.frame(old_features * weights_matrix) # pomnózmy cechy * waga
- new_feature = rowSums(weighted_features) # zsumujmy pomnożone cechy
- new_X[i] = new_feature # i tak powstała nowa cecha która leci do listy new_X
- }
- ### TESTOWANIE ###
- # podsumujmy co mamy:
- # X to cechy originalne
- # new_X to cechy wygenerowane wzorem
- # y to etykiety (klasa próbki) wspólne dla X oraz new_X.
- # To wszystko co nam potrzebne, żeby sprawdzić, czy generowanie cech miało sens czy nie.
- #dzielenie cech X i new_X na train-test
- s_value = sample(1:10, length(y), replace = TRUE) # losujemy liczby od 1-10
- #Na przyklad tam gdzie wylosowalismy 1 to bęZdzie testowy zbior, tam gdzie inne to treningowy
- #Ale ze mamy mały zbior to zrobmy 2:10, czyli tam gdzie wylosowano 1 i 2 to bedzie testowy.
- # s_value %in% 3:10 powie nam, gdzie wylosowano liczby np. od 3 do 10
- X_train = X[s_value %in% 3:10,]
- new_X_train = new_X[s_value %in% 3:10,]
- y_train = y[s_value %in% 3:10]
- X_test = X[s_value %in% 1:2,]
- new_X_test = new_X[s_value %in% 1:2,]
- y_test = y[s_value %in% 1:2]
- length(y_train) + length(y_test) == length(y) # musi dac true, wtedy wiemy ze korzystamy ze wszystkich dostepnych danych
- # uczenie 1 drzewa decyzyjnego (rpart https://www.rdocumentation.org/packages/rpart/versions/4.1-15/topics/rpart ):
- tree <- rpart(y_train~., X_train, method="class") # “class” argument tells it is a classification tree
- set_1_predict = predict(tree, X_test, type = "class") # predykcja klas
- # uczenie 2:
- tree2 <- rpart(y_train~., new_X_train, method="class")
- set_2_predict = predict(tree2, new_X_test, type = "class") # predykcja klas dla nowego wektora cech
- #p Prownanie czy etykety dobrze zostały przewidziane
- output_matrix1 = (y_test == set_1_predict)
- single_output1 = table(output_matrix1)[["TRUE"]]/ length(y_test)*100 # obliczenie skuteczności ilość poprawnych / ilość ogólna * 100%
- print("WYNIK bez modyfikacji:")
- print(single_output1)
- #porownanie czy etykety dobrze zostały przewidziane
- output_matrix2 = (y_test == set_2_predict)
- single_output2 = table(output_matrix2)[["TRUE"]]/ length(y_test)*100
- print("WYNIK po modyfikacji:")
- print(single_output2)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement