Skip to content

Instantly share code, notes, and snippets.

#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):
#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)
#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()
# 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
#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)
# 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
# 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)))
#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)
#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))
# Load the encoder:
g = tf.Graph()
with g.as_default():
text_input = tf.placeholder(dtype=tf.string, shape=[None])
embed = hub.Module("https://tfhub.dev/google/universal-sentence-encoder-large/3")
embedded_text = embed(text_input)
init_op = tf.group([tf.global_variables_initializer(), tf.tables_initializer()])
g.finalize()
# Initialize session: