# Notebook from Logan1x/DNA-Binder
Path: dna_binding_pred_final.ipynb

# Pre-Processing
_____no_output_____## importing necessary modules_____no_output_____

!pip install biopythonDefaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: biopython in /home/noob/.local/lib/python3.8/site-packages (1.77)
Requirement already satisfied: numpy in /home/noob/.local/lib/python3.8/site-packages (from biopython) (1.19.0)
WARNING: You are using pip version 20.2.3; however, version 20.2.4 is available.
You should consider upgrading via the '/usr/bin/python -m pip install --upgrade pip' command.

## Loading libraries_____no_output_____

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from collections import Counter
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from Bio.SeqUtils.ProtParam import ProteinAnalysis
from sklearn.metrics import accuracy_score
from Bio.Align import AlignInfo
from Bio import AlignIO
_____no_output_____

## getting the data_____no_output_____

training = pd.read_csv('mlba_train.csv') # Edit the path for your data here
validation = pd.read_csv('valid.csv')_____no_output_____training.describe()  #describing the dataset to find anomaies if any_____no_output_____

### deleting non unique sequence_____no_output_____

training.drop(2119,inplace=True)
training.reset_index(inplace=True)_____no_output_____training.head()_____no_output_____

### changing labels to integer type for Algo to understand_____no_output_____

training.loc[training[' Type'] == 'DNA', ' Type'] = 1
training.loc[training[' Type'] == 'NDNA', ' Type'] = -1
training.head()_____no_output_____

### coverting amino acid composition from given sequence_____no_output_____

import collections

def merge_dicts(dicts):
    merged = collections.defaultdict(list)
    for d in dicts:
        for k, v in d.items():
            merged[k].append(v)
    return dict(merged)

seq_dictionary = []
for i,sequence in enumerate(training[' Sequence']):
  X = ProteinAnalysis(sequence)
  seq_dictionary.append(X.get_amino_acids_percent())

merged = merge_dicts(seq_dictionary)
percent_comp = pd.DataFrame(merged)
percent_comp.head()_____no_output_____training.tail()_____no_output_____final_training = pd.concat([training,percent_comp], axis = 1)
final_training.head()_____no_output_____

### drop sequence column_____no_output_____

X = final_training.drop([' Sequence','ID',' Type','index'],axis=1)
y = pd.DataFrame(final_training[' Type'],dtype=np.float32)_____no_output_____X.head()_____no_output_____y.head()_____no_output_____

## split the training data set_____no_output_____

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,)_____no_output_____X_train.describe()_____no_output_____

# Processing_____no_output_____## trying SVM_____no_output_____

svm_classifier = SVC()
svm_classifier.fit(X_train,y_train)_____no_output_____y_pred_svm = svm_classifier.predict(X_test)_____no_output_____

## trying k- neighborhood_____no_output_____

knn_classifier = KNeighborsClassifier(n_neighbors=2)
knn_classifier.fit(X_train,y_train)_____no_output_____y_pred_knn = knn_classifier.predict(X_test)_____no_output_____

## trying naive bayes_____no_output_____

from sklearn.naive_bayes import GaussianNB
nb_classifier = GaussianNB()
nb_classifier.fit(X_train,y_train)_____no_output_____y_pred_nb = nb_classifier.predict(X_test) _____no_output_____

# Post-Processing_____no_output_____

print(f"SVM accuracy :{accuracy_score(y_test, y_pred_svm)}")_____no_output_____print(f"Naive Bayes accuracy :{accuracy_score(y_test, y_pred_nb)}")_____no_output_____print(f"KNN accuracy :{accuracy_score(y_test, y_pred_knn)}")_____no_output_____

### Working on test data_____no_output_____

validation.head()_____no_output_____print(validation[' Sequence'].shape)_____no_output_____

### Converting sequence to amino acid composition_____no_output_____

seq_dictionary_valid = []
for i,sequence in enumerate(validation[' Sequence']):
  X = ProteinAnalysis(sequence)
  seq_dictionary_valid.append(X.get_amino_acids_percent())

merged_validation = merge_dicts(seq_dictionary_valid)
percent_comp_validation = pd.DataFrame(merged_validation)
percent_comp_validation.head()_____no_output_____final_validation = pd.concat([validation,percent_comp_validation], axis = 1)
print(final_validation.shape)
final_validation.head()_____no_output_____X_val = final_validation.drop([' Sequence','ID'],axis=1)
X_val.head()_____no_output_____

### Prediction generation_____no_output_____

y_pred_svm_valid = svm_classifier.predict(X_val).astype(int)_____no_output_____

### Saving the output file to csv_____no_output_____

def get_label_csv(predictions,name_of_file):
  labels = pd.DataFrame({'Lable':predictions})
  final_output = pd.concat([final_validation['ID'],labels],axis=1)
  final_output.to_csv(name_of_file,index=False)_____no_output_____get_label_csv(y_pred_svm_valid,'svm_result_valid.csv')_____no_output_____y_pred_knn_valid = knn_classifier.predict(X_val)_____no_output_____get_label_csv(y_pred_knn_valid,'knn_result_valid.csv')_____no_output_____y_pred_nb_val = nb_classifier.predict(X_val)_____no_output_____get_label_csv(y_pred_nb_val,'nb_result_valid.csv')_____no_output_____


# Notebook from katewomack/NuPyCEE
Path: DOC/Capabilities/Delayed_extra_sources.ipynb

# Delayed Extra Sources in NuPyCEE_____no_output_____Created by Benoit Côté_____no_output_____This notebook introduces the general delayed-extra set of parameters in NuPyCEE that allows to include any enrichment source that requires a delay-time distribution function (DTD). For example, this implementation can be used for different SNe Ia channels, for compact binary mergers, and for exotic production sites involving interactions between stellar objects.

This notebook focuses on SYGMA, but the implementation can also be used with OMEGA._____no_output_____## 1. Implementation_____no_output_____**Here are the basic simple stellar population (SSP) inputs that need to be provided**:
- The DTD function (rate of occurence as a function of time),
- The total number of events per unit of M$_\odot$ formed,
- The yields (abundance pattern) associated with an event,
- The total mass ejected by an event (which will multiply the yields)._____no_output_____**Here is how these inputs are implemented in NuPyCEE** (see below for examples):

**`delayed_extra_dtd[ nb_sources ][ nb_Z ]`**

- `nb_sources` is the number of different input astrophysical site (e.g., SNe Ia, neutron star mergers, iRAWDs, etc.).
- `nb_Z` is the number of available metallicities available in the delayed-extra yields table.
- `delayed_extra_dtd[i][j]` is a 2D array in the form of [ number_of_times ][ 0-time, 1-rate ].

    The fact that we use 2D arrays provides maximum flexibility. We can then use analytical formulas, population synthesis predictions, outputs from simulations, etc..

**`delayed_extra_dtd_norm[ nb_sources ][ nb_Z ]`**

- Total number of delayed sources occurring per M$_\odot$ formed.

**`delayed_extra_yields[ nb_sources ]`**

- Yields table path (string) for each source.
- There is no [ nb_Z ] since the yields table can contain many metallicities.

**`delayed_extra_yields_norm[ nb_sources ][ nb_Z ]`**

- Fraction (float) of the yield table that will be eject per event, for each source and metallicity. This is the total  mass ejected per event if the yields are in mass fraction (normalized to 1)._____no_output_____## 2. Example with SYGMA_____no_output_____

import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from NuPyCEE import sygma_____no_output_____

### 2.1 One deyaled extra source_____no_output_____

# Create the DTD and yields information for the extra source
# ==========================================================

# Event rate [yr^-1] as a function of time [yr].
# Times need to be in order. No event will occurs before the lowest time and after the largest time.
# The code will interpolate the data point provided (in linear or in log-log space).
t = [0.0, 1.0e9, 2.0e9]  # Can be any length.
R = [1.0, 3.0, 2.0]  # This is only the shape of the DTD, as it will be re-normalized.

# Build the input DTD array
dtd = []
for i in range(0,len(t)):
    dtd.append([t[i], R[i]])

# Add the DTD array in the delayed_extra_dtd array.
delayed_extra_dtd = [[dtd]]
# [[ ]] for the indexes for the number of sources (here 1) and metallicities (here 1)

# Define the total number of event per unit of Msun formed.  This will normalize the DTD.
delayed_extra_dtd_norm = [[1.0e-1]]
# [[ ]] for the indexes for the number of sources (here 1) and metallicities (here 1)

# Define the yields path for the extra source
delayed_extra_yields = ['yield_tables/r_process_arnould_2007.txt']
# [ ] and not [[ ]] because the nb_Z is in the yields table as in SN Ia yields
# See yield_tables/sn1a_ivo12_stable_z.txt for an example of such yields template.

# Define the total mass ejected by an extra source
delayed_extra_yields_norm = [[1.0e-3]]_____no_output_____# Run SYGMA, one SSP with a total mass of 1 Msun at Z = 0.02
mgal = 1.0
s1 = sygma.sygma(iniZ=0.02, delayed_extra_dtd=delayed_extra_dtd, delayed_extra_dtd_norm=delayed_extra_dtd_norm,\
        delayed_extra_yields=delayed_extra_yields, delayed_extra_yields_norm=delayed_extra_yields_norm, mgal=mgal,\
        dt=1e8, special_timesteps=-1, tend=1.1*t[-1])SYGMA run in progress..
   SYGMA run completed - Run time: 0.02s

### Let's test the total number of events and the total mass ejected by those events._____no_output_____

# Predicted number of events
N_pred = delayed_extra_dtd_norm[0][0] * mgal

# Predicted mass ejected by events
M_ej_pred = N_pred * delayed_extra_yields_norm[0][0]

# Calculated number of events
N_sim = sum(s1.delayed_extra_numbers[0])

# Calculated mass ejected by events
M_ej_sim = sum(s1.ymgal_delayed_extra[0][-1])

# Print the test
print ('The following numbers should be 1.0')
print ('   Number of events (predicted/calculated):', N_pred / N_sim)
print ('   Mass ejected by events (predicted/calculated):', M_ej_pred / M_ej_sim)The following numbers should be 1.0
   Number of events (predicted/calculated): 1.0
   Mass ejected by events (predicted/calculated): 0.9999999999998823

### Let's plot the DTD_____no_output_____

%matplotlib nbagg
plt.plot(s1.history.age[1:], np.array(s1.delayed_extra_numbers[0])/s1.history.timesteps)
plt.xlim(0, 1.1*t[-1])
plt.xlabel('Time [yr]', fontsize=12)
plt.ylabel('Rate [event yr$^{-1}$]', fontsize=12)_____no_output_____

### 2.2 Two deyaled extra sources_____no_output_____

# Create the DTD and yields information for the second extra source
# =================================================================

# Event rate [yr^-1] as a function of time [yr].
t2 = [1.4e9, 1.6e9, 1.8e9]
R2 = [4.0, 1.0, 4.0]

# Build the input DTD array
dtd2 = []
for i in range(0,len(t2)):
    dtd2.append([t2[i], R2[i]])

# Add the DTD array in the delayed_extra_dtd array.
delayed_extra_dtd = [[dtd],[dtd2]]

# Define the total number of event per unit of Msun formed.  This will normalize the DTD.
delayed_extra_dtd_norm = [[1.0e-2],[5.0e-3]]

# Define the yields path for the extra source
delayed_extra_yields = ['yield_tables/r_process_arnould_2007.txt','yield_tables/r_process_arnould_2007.txt']

# Define the total mass ejected by an extra source
delayed_extra_yields_norm = [[1.0e-3],[2.0e-3]]_____no_output_____# Run SYGMA, one SSP with a total mass of 1 Msun at Z = 0.02
mgal = 1.0
s2 = sygma.sygma(iniZ=0.02, delayed_extra_dtd=delayed_extra_dtd, delayed_extra_dtd_norm=delayed_extra_dtd_norm,\
        delayed_extra_yields=delayed_extra_yields, delayed_extra_yields_norm=delayed_extra_yields_norm, mgal=mgal,\
        dt=1e8, special_timesteps=-1, tend=1.1*t[-1])SYGMA run in progress..
   SYGMA run completed - Run time: 0.02s
%matplotlib nbagg
plt.plot(s2.history.age[1:], np.array(s2.delayed_extra_numbers[0])/s2.history.timesteps)
plt.plot(s2.history.age[1:], np.array(s2.delayed_extra_numbers[1])/s2.history.timesteps)
plt.xlim(0, 1.1*t[-1])
plt.xlabel('Time [yr]', fontsize=12)
plt.ylabel('Rate [event yr$^{-1}$]', fontsize=12)_____no_output_____


# Notebook from noelnamai/machine-learning
Path: recommender-systems.ipynb

<code>
%reset -f
%matplotlib inline_____no_output_____import pprint
import numpy as np
import pandas as pd
import scipy.io as sio
import sebaba.ml as sbbml
import sebaba.utils as utils
import matplotlib.pyplot as plt
import matplotlib.ticker as tkr_____no_output_____pd.options.display.float_format = "{:.4f}".format_____no_output_____mat = sio.loadmat("data/ex8movies.mat")
pprint.pprint(mat){'R': array([[1, 1, 0, ..., 1, 0, 0],
       [1, 0, 0, ..., 0, 0, 1],
       [1, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
 'Y': array([[5, 4, 0, ..., 5, 0, 0],
       [3, 0, 0, ..., 0, 0, 5],
       [4, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint8),
 '__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Thu Dec '
               b' 1 17:19:26 2011',
 '__version__': '1.0'}
#y        = [n_movies, n_users] i.e. y[i,j] is rating for movie i by user j
#is_rated = [n_movies, n_users] i.e. r[i,j] = 1 if user j rated movie i else 0
y        = mat["Y"]
is_rated = mat["R"]_____no_output_____mat = sio.loadmat("data/ex8movies-params.mat")
pprint.pprint(mat){'Theta': array([[ 0.28544362, -1.68426509,  0.26293877, ...,  0.76723235,
        -1.10460164, -0.25186708],
       [ 0.50501321, -0.45464846,  0.31746244, ...,  1.09306336,
        -1.20029436, -0.39161676],
       [-0.43191656, -0.47880449,  0.84671111, ...,  1.36333976,
        -0.32523542, -0.19468212],
       ...,
       [ 1.0586926 , -0.8087176 ,  0.56935771, ...,  0.80421422,
        -0.74346778, -0.11985885],
       [ 0.89058932, -0.12894734,  0.35560466, ...,  1.37426807,
        -0.7526549 , -0.81135311],
       [ 0.82414136, -0.38892594,  1.06940502, ...,  1.28423703,
        -0.98349993, -0.53184838]]),
 'X': array([[ 1.0486855 , -0.40023196,  1.19411945, ...,  0.861721  ,
        -0.69728994,  0.28874563],
       [ 0.78085123, -0.38562591,  0.52119779, ...,  0.70402073,
        -0.48583521, -0.56462407],
       [ 0.64150886, -0.54785385, -0.08379638, ...,  0.83854643,
        -0.69483208, -1.13479631],
       ...,
       [ 0.21952237, -0.20047886,  0.09257965, ...,  0.14595183,
        -0.0431316 ,  0.17830451],
       [ 0.16044028, -0.16015395,  0.23570946, ...,  0.2073381 ,
        -0.33234766,  0.0428813 ],
       [ 0.07677118, -0.19720738,  0.08186445, ...,  0.40012757,
        -0.28747906, -0.03233498]]),
 '__globals__': [],
 '__header__': b'MATLAB 5.0 MAT-file, Platform: GLNXA64, Created on: Thu Dec '
               b' 1 17:16:53 2011',
 '__version__': '1.0',
 'num_features': array([[10]], dtype=uint8),
 'num_movies': array([[1682]], dtype=uint16),
 'num_users': array([[943]], dtype=uint16)}
#x     = [n_movies, n_features] i.e. x[i,j] is feature j for movie i
#theta = [n_users, n_features] i.e. theta[i,j] is parameter feature j for user i
x     = mat["X"]
theta = mat["Theta"]_____no_output_____movie_ids = dict()

with open("data/ex8movie-ids.txt") as f:
    for line in f.readlines():
        line = line.strip()
        token = line.split(" ")
        movie_ids[int(token[0]) -1] = " ".join(token[1: ])_____no_output_____fig, ax = plt.subplots(figsize = (10.0, 10.0))
ax.imshow(y[:100,:100], cmap = "binary", interpolation = "nearest")
ax.set_title("Movie Ratings", fontsize = 20)
ax.set_xlabel("User", fontsize = 18)
ax.set_ylabel("Movie", fontsize = 18)
ax.margins(0.05)
ax.axis("tight")
ax.grid(True)
fig.tight_layout()

plt.show()_____no_output_____#add ratings for new user
ratings = np.zeros((1682, 1))

ratings[0]   = 4.0
ratings[6]   = 3.0
ratings[11]  = 5.0
ratings[53]  = 4.0
ratings[63]  = 5.0
ratings[65]  = 3.0
ratings[68]  = 5.0
ratings[97]  = 2.0
ratings[182] = 4.0
ratings[225] = 5.0
ratings[354] = 5.0

y        = np.append(y, ratings, axis = 1)
is_rated = np.append(is_rated, ratings != 0, axis = 1)_____no_output_____model = sbbml.CollaborativeFiltering(alpha = 10.0, gamma = 10.0, n_features = 10)
model.fit(y, is_rated)_____no_output_____utils.plot_cost_function(cost = model.cost, width = 10.0, height = 6.5)_____no_output_____y_pred     = np.dot(model.x, model.theta.T)
y_pred_new = y_pred[:, -1]
sorted_idx = np.argsort(y_pred_new, axis = 0)[::-1]

for i in range(20):
    k = sorted_idx[i]
    print(f"{movie_ids[k]: <50} {y_pred_new[k]:.10f}")Star Wars (1977)                                   4.2523348369
Titanic (1997)                                     4.2507075503
Raiders of the Lost Ark (1981)                     4.0104690729
Return of the Jedi (1983)                          3.9914614085
Empire Strikes Back, The (1980)                    3.9385709127
Shawshank Redemption, The (1994)                   3.9182563640
Good Will Hunting (1997)                           3.9124259510
Braveheart (1995)                                  3.8898653355
Godfather, The (1972)                              3.8132934031
Schindler's List (1993)                            3.7787410035
As Good As It Gets (1997)                          3.7621882825
Terminator 2: Judgment Day (1991)                  3.7472800732
L.A. Confidential (1997)                           3.7140839367
Usual Suspects, The (1995)                         3.6919081939
Fugitive, The (1993)                               3.6675130845
Terminator, The (1984)                             3.6659467384
Indiana Jones and the Last Crusade (1989)          3.6450029021
Forrest Gump (1994)                                3.6444509970
Apt Pupil (1998)                                   3.6317870316
Men in Black (1997)                                3.5796172390
fig, ax = plt.subplots(figsize = (10.0, 10.0))
ax.imshow(y_pred[:100,:100], cmap = "binary", interpolation = "nearest")
ax.set_title("Movie Ratings", fontsize = 20)
ax.set_xlabel("User", fontsize = 18)
ax.set_ylabel("Movie", fontsize = 18)
ax.margins(0.05)
ax.axis("tight")
ax.grid(True)
fig.tight_layout()

plt.show()_____no_output_____
</code>