<jupyter_start><jupyter_text>1. Read data<jupyter_code>data_file = "../data/bdata.log.txt"

data = pandas.read_csv(data_file, sep = ',')
data.head()<jupyter_output><empty_output><jupyter_text>2. To one hot<jupyter_code>selected_mhc = 'HLA-A*03:01'

indexes = data['mhc'][data['mhc'] == selected_mhc].index
indexes = data.iloc[indexes]['mhc'][data['peptide_length'] == 9].index
#indexes = data.index
selected_X = data['sequence'][indexes]
selected_y = data['meas'][indexes]

letters_X = selected_X.apply(list)

selected_X = pandas.DataFrame(list(letters_X))
selected_X = pandas.get_dummies(selected_X)

XX = selected_X[range(0, 20)].as_matrix() + selected_X[range(20, 40)].as_matrix() + selected_X[range(40, 60)].as_matrix() + selected_X[range(60, 80)].as_matrix() + selected_X[range(80, 100)].as_matrix() + selected_X[range(100, 120)].as_matrix() + selected_X[range(120, 140)].as_matrix() + selected_X[range(140, 160)].as_matrix() + selected_X[range(160, 180)].as_matrix()
selected_X[selected_X == 1] = 0.9
selected_X[selected_X == 0] = 0.05
selected_X = pandas.concat([selected_X, pandas.DataFrame(XX)], axis=1)

selected_X.shape<jupyter_output><empty_output><jupyter_text>3. ml stuff<jupyter_code>#normalize output
# m = selected_y.mean()
# d = selected_y.max() - selected_y.min()
# selected_y = (selected_y - m) / d

# train/test split
random_number = 52
X_train, X_test, y_train, y_test = train_test_split(selected_X, selected_y,
                                                    test_size = 0.33, random_state = random_number)
def ridge_regression(X, y, alpha):
    #Fit the model
    ridgereg = Ridge(alpha=alpha,normalize=True)
    ridgereg.fit(X, y)
    y_pred = ridgereg.predict(X)
    
    #Return the result in pre-defined format
    rss = sum((y_pred - y)**2)
    return rss, ridgereg.intercept_, ridgereg
alpha_ridge = [1e-15, 1e-10, 1e-8, 1e-4, 1e-3,1e-2]

n = 6

rss = [0] * n
intercept = [0] * n
predictors = [0] * n

for i in range(n):
    rss[i], intercept[i], predictors[i] = ridge_regression(X_train, y_train, alpha_ridge[i])
# predict test data

test_rss = [0] * n
for i in range(n):
    m_pred = predictors[i]
    test_pred = m_pred.predict(X_test)
    test_rss[i] = sum((test_pred - y_test)**2)
    
    print '-' * 10
    print "alpha = ", alpha_ridge[i]
    print "train rss = ", rss[i]
    print "test rss = ", test_rss[i]
    
m_pred = predictors[5]
test_pred = m_pred.predict(X_test)
test_pred = pandas.DataFrame(test_pred)
y_test = pandas.DataFrame(y_test)
test_pred['1'] = np.array(y_test)
test_pred.corr()<jupyter_output><empty_output><jupyter_text>NN<jupyter_code>X_train = X_train.as_matrix()
X_test = X_test.as_matrix()

y_train = y_train.as_matrix().flatten()
y_test = y_test.as_matrix().flatten()
model = Sequential()
model.add(Dense(128, input_dim=X_train.shape[1]))
model.add(Activation('relu'))
model.add(Dropout(0.15))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.15))
model.add(Dense(1))

model.compile(loss='mse', optimizer='rmsprop')
model.fit(X_train, y_train, batch_size=32, nb_epoch=500, verbose=2, validation_split=0.15)
preds = model.predict(X_test, verbose=0).flatten()
sum((preds - y_test)**2)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16520924422236102, 'fraction_numerical': 0.03591505309181761, 'mean_word_length': 5.2194174757281555, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '448976', 'license': 'MIT', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1268, 'n_words': 210, 'path': 'vladimirgl/code/onehotreg.ipynb', 'repo_name': 'imminfo/mhc-prediction-summer-school-project', 'size': '3202'}