@@ -0,0 +1,2279 @@
+from sqlalchemy.orm import sessionmaker, joinedload%0Afrom github_database import engine, Commit%0Afrom re import match%0Afrom sklearn.svm import SVC%0Afrom sklearn.cross_validation import train_test_split%0Afrom sklearn.metrics import accuracy_score%0Afrom sklearn.preprocessing import LabelEncoder%0Afrom collections import Counter, OrderedDict%0Afrom operator import itemgetter%0A%0A%0Adef _get_first_word(string):%0A    return match('%5E(%5B%5Cs%5D+)?(%5B%5E%5Cs%5D+)', string).group().strip()%0A%0A%0Adef _get_file_stats(commit_files):%0A    additions = 0%0A    deletions = 0%0A    for commit_file in commit_files:%0A        additions += commit_file.additions%0A        deletions += commit_file.deletions%0A    return %5Badditions, deletions%5D%0A%0A%0Adef _collect_data_by_labels(X, y, filters):%0A    assert len(y) == len(X)%0A%0A    new_x = %5B%5D%0A    new_y = %5B%5D%0A    for data_i in range(len(y)):%0A        if y%5Bdata_i%5D in filters:%0A            new_x.append(X%5Bdata_i%5D)%0A            new_y.append(y%5Bdata_i%5D)%0A    return new_x, new_y%0A%0A%0Adef run():%0A    db_session = sessionmaker(bind=engine)()%0A    commits = db_session %5C%0A        .query(Commit) %5C%0A        .options(joinedload(Commit.files)) %5C%0A        .all()%0A%0A    commit_files_list = %5Bcommit.files for commit in commits%5D%0A    commit_first_words = %5B_get_first_word(commit.message) for commit in commits%5D%0A    commit_first_words_occurances = OrderedDict(sorted(%0A        Counter(commit_first_words).items(), key=itemgetter(1), reverse=True))%0A    label_encoder = LabelEncoder()%0A    filtered_labels = %5Bword_occurances%5B0%5D for word_occurances in commit_first_words_occurances.items()%5B:8%5D%5D%0A%0A    X_all = %5B_get_file_stats(commit_files) for commit_files in commit_files_list%5D%0A    y_all = label_encoder.fit_transform(commit_first_words)%0A    X_all, y_all = _collect_data_by_labels(X_all, y_all, label_encoder.transform(filtered_labels))%0A    num_all = len(X_all)%0A    num_train = num_all*0.25%0A    clf = SVC()%0A    X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, train_size=(num_train / float(num_all)))%0A%0A    print %22Training set size: %7B%7D%22.format(len(X_train))%0A    clf.fit(X_train, y_train)%0A    print %22Accuracy for training set: %7B%7D%22.format(accuracy_score(y_train, clf.predict(X_train)))%0A    print %22Accuracy for test set: %7B%7D%22.format(accuracy_score(y_test, clf.predict(X_test)))%0A%0Aif __name__ == %22__main__%22:%0A    run()%0A

Add prediction components