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
| #Choose optimizer: | |
| optim = optimizers.Adam(lr=0.00015) | |
| # create NN for news clissification: | |
| news_DNN = Sequential() | |
| news_DNN.add(Dense(40, input_dim=512, activation = 'relu',kernel_regularizer=l2(0.1))) | |
| news_DNN.add(Dropout(0.25)) | |
| news_DNN.add(Dense(40, activation = 'relu',kernel_regularizer=l2(0.1))) | |
| news_DNN.add(Dropout(0.25)) |
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
| #Resampling: | |
| [X_train_r, y_train_r] = SMOTE().fit_resample(X_train, y_train) | |
| #model fitting: | |
| m_h = news_DNN.fit(X_train_r, y_train_r, epochs=400, \ | |
| validation_data=(X_test, y_test), batch_size=32, verbose=0) |
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
| # Plot accuracy curves: | |
| with sns.color_palette("Accent", n_colors=8): | |
| plt.figure(figsize=(8,6)) | |
| sns.lineplot(data=np.asarray(m_h.history['acc'])) | |
| sns.lineplot(data=np.asarray(m_h.history['val_acc'])) | |
| plt.xlabel("Epochs") | |
| plt.ylabel("Accuracy") | |
| plt.title("Accuracy for Media Bias Classification") # change title here | |
| plt.legend(labels=['training', 'validation'],loc='lower right') | |
| plt.text(330,0.755,'val Ac = ' + str(round(accuracy_score(y_test,model.predict_classes(X_test)),2))) |
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
| # split into shuffled folds: | |
| #Note that you should edit the class array accordingly: Bias (classes_Bias) or Outlet (classes_All) | |
| sss = StratifiedShuffleSplit(n_splits=1, test_size=0.33) # chose one split to make analysis faster. change it if required | |
| for t, te in sss.split(eAll,classes_All): | |
| # Scale the data with StandardScaler before splitting: | |
| X_train, X_test = scaler.fit_transform(eAll)[t], \ | |
| scaler.fit_transform(eAll)[te] | |
| y_train, y_test = classes_All[t]-1,classes_All[te]-1 |
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
| #fit the network. You can change parameters to see how this affects your training. | |
| m_h = news_DNN.fit(X_train, y_train, epochs=400, \ | |
| validation_data=(X_test, y_test), batch_size=32, verbose=0) |
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
| # Regular modules for data science and visualization: | |
| import numpy as np | |
| import pandas | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| # Keras (2.2.4) and tensorflow (1.13). | |
| import tensorflow as tf | |
| import tensorflow_hub as hub |
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
| #All required modules in the permutation pipeline: | |
| import numpy as np | |
| from sklearn import datasets | |
| import scipy.stats as stats | |
| import matplotlib.pyplot as plt | |
| import seaborn as sns | |
| import random | |
| import copy | |
| irisd = datasets.load_iris() |
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
| #Copy pooled distribution: | |
| pS = copy.copy(pV) | |
| #Initialize permutation: | |
| pD = [] | |
| #Define p (number of permutations): | |
| p=1000 | |
| # Permutation loop: | |
| for i in range(0,p): | |
| # Shuffle the data: | |
| random.shuffle(pS) |
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
| #Compute ground truth correlation: | |
| [r,pv] =stats.pearsonr(feat_set[:,1],feat_set[:,0]) | |
| #Copy one of the features: | |
| pS = copy.copy(feat_set[:,1]) | |
| #Initialize variables: | |
| pR = [] | |
| #Choose number of permutations: | |
| p=10000 | |
| #Initialize permutation loop: | |
| for i in range(0,p): |
OlderNewer