Created
December 11, 2018 15:04
-
-
Save mwygoda/0f2e8196fb351a61c81c3c12a112a6cc to your computer and use it in GitHub Desktop.
randomSubSpace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Random Forest Classification | |
| # Importing the libraries | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import pandas as pd | |
| import time | |
| class MeasureUnit: | |
| def __init__(self, index, y_predict, cm, m_accuracy): | |
| self.index = index | |
| self.y_predict = y_predict | |
| self.cm = cm | |
| self.m_accuracy = m_accuracy | |
| ESTIMATORS = 10 | |
| fileName = 'result' | |
| testQuantity = 10 | |
| # Importing the dataset | |
| from sklearn.metrics import confusion_matrix, accuracy_score | |
| url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data' | |
| # dataset = pd.read_csv('Social_Network_Ads.csv') | |
| dataset = pd.read_csv('OnlineNewsPopularity.csv') | |
| # dataset = pd.read_csv(url, header=None) | |
| y_alfa = dataset.iloc[:, -1].values | |
| median = np.median(y_alfa) | |
| y = [] | |
| for val in y_alfa: | |
| y.append(val > median) | |
| DTTimeList = [] | |
| DTAccuracyList = [] | |
| # Decision Tree Algorithm | |
| # for x in range(0, 10): | |
| # start = time.time() | |
| # X_dt = dataset.iloc[:, 1:60].values | |
| # from sklearn import tree | |
| # | |
| # # Splitting the dataset into the Training set and Test set | |
| # classifier_dt = tree.DecisionTreeClassifier(); | |
| # from sklearn.cross_validation import train_test_split | |
| # | |
| # X_train_dt, X_test_dt, y_train_dt, y_test_dt = train_test_split(X_dt, y, test_size=0.25, random_state=0) | |
| # | |
| # from sklearn.preprocessing import StandardScaler | |
| # | |
| # sc = StandardScaler() | |
| # X_train = sc.fit_transform(X_train_dt) | |
| # X_test = sc.transform(X_test_dt) | |
| # | |
| # classifier_dt.fit(X_train, y_train_dt) | |
| # | |
| # end = time.time() | |
| # #print(end - start) | |
| # y_predict_dt = classifier_dt.predict(X_test) | |
| # DTTimeList.append(end - start) | |
| # #print("Time for Decision Tree Algorithm", end - start) | |
| # | |
| # # Making the Confusion Matrix | |
| # from sklearn.metrics import confusion_matrix | |
| # | |
| # cm_dt = confusion_matrix(y_test_dt, y_predict_dt) | |
| # | |
| # # Feature Scaling | |
| # from sklearn.preprocessing import StandardScaler | |
| # | |
| # sc = StandardScaler() | |
| # X_train = sc.fit_transform(X_train) | |
| # X_test = sc.transform(X_test) | |
| # accuracy = accuracy_score(y_test_dt, y_predict_dt) | |
| # DTAccuracyList.append(accuracy) | |
| # #print("accurecy dt:", accuracy) | |
| # np.savetxt(fileName + 'dttime.csv', DTTimeList, delimiter=',', fmt='%1.3f') | |
| # np.savetxt(fileName + 'dtacc.csv', DTAccuracyList, delimiter=',', fmt='%1.3f') | |
| # ######################################################### | |
| RFTimeList = [] | |
| RFAccuracy = [] | |
| # for x in range(0, testQuantity): | |
| # # Random Forest Algorithm | |
| # start = time.time() | |
| # x_cols_rf = [x for x in dataset.columns if x != 'shares' and x != 'url'] | |
| # X_rf = dataset.iloc[:, 1:60].values | |
| # | |
| # from sklearn.ensemble import RandomForestClassifier | |
| # | |
| # # Splitting the dataset into the Training set and Test set | |
| # classifier_rf = RandomForestClassifier(n_estimators=ESTIMATORS) | |
| # from sklearn.cross_validation import train_test_split | |
| # | |
| # X_train_rf, X_test_rf, y_train_rf, y_test_rf = train_test_split(X_rf, y, test_size=0.25, random_state=0) | |
| # | |
| # from sklearn.preprocessing import StandardScaler | |
| # | |
| # sc = StandardScaler() | |
| # X_train = sc.fit_transform(X_train_rf) | |
| # X_test = sc.transform(X_test_rf) | |
| # | |
| # classifier_rf.fit(X_train, y_train_rf) | |
| # y_predict_rf = classifier_rf.predict(X_test) | |
| # | |
| # end = time.time() | |
| # RFTimeList.append(end - start) | |
| # #print(end - start) | |
| # #print("Time for Random Forest Algorithm: ", end - start) | |
| # | |
| # # Making the Confusion Matrix | |
| # from sklearn.metrics import confusion_matrix | |
| # | |
| # cm_rf = confusion_matrix(y_test_rf, y_predict_rf) | |
| # | |
| # # Feature Scaling | |
| # from sklearn.preprocessing import StandardScaler | |
| # | |
| # sc = StandardScaler() | |
| # X_train = sc.fit_transform(X_train) | |
| # X_test = sc.transform(X_test) | |
| # accuracy = accuracy_score(y_test_rf, y_predict_rf) | |
| # #print("accurecy rf:", accuracy) | |
| # RFAccuracy.append(accuracy) | |
| # | |
| # np.savetxt(fileName + 'rftime.csv', RFTimeList, delimiter=',', fmt='%1.3f') | |
| # np.savetxt(fileName + 'rfacc.csv', RFAccuracy, delimiter=',', fmt='%1.3f') | |
| # ######################################################### | |
| # Random Subspace with decision tree estimator | |
| RSTimeList = [] | |
| RSAccuracy = [] | |
| iter = 0 | |
| while iter < testQuantity: | |
| start = time.time() | |
| x_cols_rs = [x for x in dataset.columns if x != 'shares' and x != 'url'] | |
| from random import randint | |
| listOfMeasures = [] | |
| predictions = [] | |
| for x in range(0, ESTIMATORS): | |
| # featuresStartingIndex = randint(1, 60 - featuresPerEstimator) | |
| rand1 = randint(1, 60) | |
| rand2 = randint(1, 60) | |
| while rand1 == rand2: | |
| rand1 = randint(1, 60) | |
| rand2 = randint(1, 60) | |
| X_rs = dataset.iloc[:, [rand1, rand2]].values | |
| from sklearn import tree | |
| # Splitting the dataset into the Training set and Test set | |
| classifier_rs = tree.DecisionTreeClassifier(); | |
| from sklearn.cross_validation import train_test_split | |
| X_train_rs, X_test_rs, y_train_rs, y_test_rs = train_test_split(X_rs, y, test_size=0.25, random_state=0) | |
| from sklearn.preprocessing import StandardScaler | |
| sc = StandardScaler() | |
| X_train = sc.fit_transform(X_train_rs) | |
| X_test = sc.transform(X_test_rs) | |
| classifier_rs.fit(X_train, y_train_rs) | |
| y_predict_rs = classifier_rs.predict(X_test) | |
| # Making the Confusion Matrix | |
| from sklearn.metrics import confusion_matrix, accuracy_score | |
| cm_rs = confusion_matrix(y_test_rs, y_predict_rs) | |
| accuracy = accuracy_score(y_test_rs, y_predict_rs) | |
| # Feature Scaling | |
| from sklearn.preprocessing import StandardScaler | |
| sc = StandardScaler() | |
| X_train = sc.fit_transform(X_train) | |
| X_test = sc.transform(X_test) | |
| measure = MeasureUnit(x, y_predict_rs, cm_rs, accuracy) | |
| # print("predict: ",len(y_predict_rs), type(y_predict_rs)) | |
| predictions.append(y_predict_rs) | |
| listOfMeasures.append(measure) | |
| #print( | |
| #"Random Subspace Measure nr: ", listOfMeasures[0].index, " ", listOfMeasures[0].y_predict, "cm: ", listOfMeasures[0].cm, | |
| #"d ", listOfMeasures[0].m_accuracy) | |
| votingResult = [] | |
| length = len(predictions[0]) | |
| for i in range(0, length): | |
| votingResult.append(0) | |
| it = np.nditer(predictions, flags=['f_index']) | |
| # iiterator = 0 | |
| # | |
| # while iiterator < 10: | |
| # temp_val = 0 | |
| # iterator = 0 | |
| # for vote in predictions[iiterator]: | |
| # if vote: | |
| # temp_val = temp_val + 1 | |
| # else: | |
| # temp_val = temp_val - 1 | |
| # iterator = iterator + 1 | |
| # votingResult[iterator] = temp_val | |
| # iiterator = iiterator + 1 | |
| # #global_iterator = global_iterator + 1 | |
| # print("result", votingResult) | |
| iterator = 0 | |
| while iterator < length: | |
| iterator2 = 0 | |
| while iterator2 < ESTIMATORS: | |
| score = 0 | |
| value = predictions[iterator2][iterator] | |
| if value: | |
| score = 1 | |
| else: | |
| score = -1 | |
| votingResult[iterator] = votingResult[iterator] + score | |
| iterator2 = iterator2 + 1 | |
| iterator = iterator + 1 | |
| boolCastedValues = [] | |
| for vote in votingResult: | |
| if vote > 0: | |
| boolCastedValues.append(True) | |
| else: | |
| boolCastedValues.append(False) | |
| end = time.time() | |
| RSTimeList.append(end - start) | |
| #print votingResult | |
| accuracy = accuracy_score(boolCastedValues, y_predict_rs) | |
| #print ("random subspace ftw: ", accuracy) | |
| RFAccuracy.append(accuracy) | |
| # print(len(predictions[0])) | |
| iter = iter + 1 | |
| np.savetxt(fileName + 'rstime.csv', RSTimeList, delimiter=',', fmt='%1.3f') | |
| np.savetxt(fileName + 'rsaccuracy.csv', RFAccuracy, delimiter=',', fmt='%1.3f') | |
| ### Calculate voting results | |
| # score = 0; | |
| # | |
| # for measure in listOfMeasures: | |
| # for vote in measure.y_predict: | |
| # # Fitting Random Forest Classification to the Training set | |
| # from sklearn.ensemble import RandomForestClassifier, BaggingClassifier | |
| # | |
| # treeClassifier = tree.DecisionTreeClassifier() | |
| # forestClassifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0) | |
| # # rsForestClassifier = BaggingClassifier(base_estimator=forestClassifier, n_estimators=2, max_samples=2.0, max_features=2.0, bootstrap=True, bootstrap_features=False, oob_score=False, warm_start=False, n_jobs=1, random_state=None, verbose=0) | |
| # rsTreeClassifier = BaggingClassifier(base_estimator=treeClassifier, n_estimators=2, max_samples=2.0, max_features=2.0, bootstrap=True, bootstrap_features=False, oob_score=False, warm_start=False, n_jobs=1, random_state=None, verbose=0) | |
| # | |
| # classifier.fit(X_train, y_train) | |
| # | |
| # # Predicting the Test set results | |
| # y_pred = classifier.predict(X_test) | |
| # | |
| # #Making the Confusion Matrix | |
| # from sklearn.metrics import confusion_matrix | |
| # cm = confusion_matrix(y_test, y_pred) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment