<jupyter_start><jupyter_text>Winning Primaries By <NAME>---In this module, I intend to find the probability of winning the primaries using predicive models brought by sklearn logistic regression modes found [here](https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html).I will also be using [Support Vector Machines](https://scikit-learn.org/stable/modules/svm.html) and [Decision Tree Classifier](https://scikit-learn.org/stable/modules/generated/sklearn.tree.DecisionTreeClassifier.html) from the sklearn libraries.The purpose of this model is find the best predictor of winning the primary elections. I will use features such as endorsement points and money raised to classify my data by whether or not they won the primary elections. Using this, I will train the machine learning models and test their accuracy with a set of data taken out of the data file but omitted from the testing set.---Information about the data used are found below.  Endorsements through June 30This folder contains data behind the story [Pols And Polls Say The Same Thing: Jeb Bush Is A Weak Front-Runner](http://fivethirtyeight.com/features/pols-and-polls-say-the-same-thing-jeb-bush-is-a-weak-front-runner/).This data includes something we call "endorsement points," an attempt to quantify the importance of endorsements by weighting each one according to the position held by the endorser: 10 points for each governor, 5 points for each senator and 1 point for each representative.Header | Definition---|---------`year` | Election year`party` | Political party`candidate` | Candidate running in primary`endorsement_points` | Weighted endorsements through June 30th of the year before the primary`percentage_endorsement_points` | Percentage of total weighted endorsement points for the candidate's political party through June 30th of the year before the primary`money_raised` | Money raised through June 30th of the year before the primary`percentage_of_money` | Percentage of total money raised by the candidate's political party through June 30th of the year before the primary`primary_vote_percentage` | Percentage of votes won in the primary`won_primary` | Did the candidate win the primary?<jupyter_code>import pandas as pd
import numpy as np


df = pd.read_csv('endorsements-june-30.csv')

df[df.columns[2:]].head()<jupyter_output><empty_output><jupyter_text>ClassificationsI will break these columns as my classifiers:  1. Percentage of total weighted endorsement points for the candidate's political party through June 30th of the year before the primary  2.	Money raised through June 30th of the year before the primary  3. 	Percentage of total money raised by the candidate's political party through June 30th of the year before the primary  4. Percentage of votes won in the primary  It may become obvious that winning the primaries highly depends on the last feature: `precentage of votes`. However, if we test a logistic regression against another column, like the `money raised`, we will notice a significant impact on the classifiers.<jupyter_code>X = df[df.columns[3:8]].values
y = df[df.columns[8]].values


# take out every 5th element for accuracy testing
test_idx = [x for x in range(len(y)) if x%5 == 0]

y_train = np.delete(y, test_idx)
X_train = np.delete(X, test_idx, axis = 0)

y_test = [y[i] for i in test_idx]
X_test = [X[i] for i in test_idx]<jupyter_output><empty_output><jupyter_text>Now I will use `LogisticRegression` to create the predictive model: `LogReg`<jupyter_code>from sklearn.linear_model import LogisticRegression

LogReg = LogisticRegression(random_state=0, solver='lbfgs',
                         multi_class='multinomial')

LogReg = LogReg.fit(X_train, y_train)

from sklearn.metrics import accuracy_score

predictions = LogReg.predict(X_test)

print("Using LogisticRegression, the accuracy compared to test values is {:.3}".format(
    accuracy_score(y_test, predictions))
)
from sklearn import tree

DecTree = tree.DecisionTreeClassifier()
DecTree.fit(X_train, y_train)

predictions = DecTree.predict(X_test)

print("Using Decision Tree Classifier, the accuracy compared to test values is {:.3}".format(
    accuracy_score(y_test, predictions))
)
from sklearn import svm

SuppVecM = svm.SVC(gamma='scale')
SuppVecM.fit(X_train, y_train)

predictions = SuppVecM.predict(X_test)

print("Using Support Vector Machine, the accuracy compared to test values is {:.3}".format(
    accuracy_score(y_test, predictions))
)<jupyter_output>Using Support Vector Machine, the accuracy compared to test values is 0.909<jupyter_text>Is the data too correated?Now I will check if the data is too highly correlated. I will use a plotting method since I only want to compare two features; `money raised` and `precentage of votes`.<jupyter_code>import matplotlib.pyplot as plt

u = df['primary_vote_percentage'].values
v = df['money_raised'].values


plt.plot(u, v, 'o')
plt.xlabel('Primary Vote (%)')
plt.ylabel('Money Raised ($)')
plt.show()<jupyter_output><empty_output><jupyter_text>Since it is difficult to come up with any simple polynomial that can relate the two, then they are at least not linearly correlated.Hence, with this I intend to use Decision Tree Classifier because it offers the highest accuracy of 100% compared to 91% by Support Vector Machine and Logistic Regression<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07706592386258125, 'fraction_numerical': 0.00872794800371402, 'mean_word_length': 5.4042806183115335, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '177488', 'license': 'MIT', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1488, 'n_words': 621, 'path': 'WinningPrimaries.ipynb', 'repo_name': 'techshot25/WinningPrimaries', 'size': '5393'}