<jupyter_start><jupyter_text>DAT210x - Programming with Python for DS  Module6- Lab3 This code is intentionally missing! Read the directions on the course lab page!<jupyter_code>import pandas as pd
import numpy as np<jupyter_output><empty_output><jupyter_text>Load up the /Module6/Datasets/parkinsons.data data set into a variable X, being sure to drop the name column.<jupyter_code>X = pd.read_csv('Datasets/parkinsons.data')
X.drop('name', axis = 1, inplace = True)
X.head()
print(X.info)
print(X.describe())<jupyter_output><bound method DataFrame.info of      MDVP:Fo(Hz)  MDVP:Fhi(Hz)  MDVP:Flo(Hz)  MDVP:Jitter(%)  \
0        119.992       157.302        74.997         0.00784   
1        122.400       148.650       113.819         0.00968   
2        116.682       131.111       111.555         0.01050   
3        116.676       137.871       111.366         0.00997   
4        116.014       141.781       110.655         0.01284   
5        120.552       131.162       113.787         0.00968   
6        120.267       137.244       114.820         0.00333   
7        107.332       113.840       104.315         0.00290   
8         95.730       132.068        91.754         0.00551   
9         95.056       120.103        91.226         0.00532   
10        88.333       112.240        84.072         0.00505   
11        91.904       115.871        86.292         0.00540   
12       136.926       159.866       131.276         0.00293   
13       139.173       179.139        76.556         0.00390   
14      [...]<jupyter_text>Check for NaNs and if all object types are correct<jupyter_code>print(X.isnull().sum()) 
print(X.dtypes)<jupyter_output>MDVP:Fo(Hz)         0
MDVP:Fhi(Hz)        0
MDVP:Flo(Hz)        0
MDVP:Jitter(%)      0
MDVP:Jitter(Abs)    0
MDVP:RAP            0
MDVP:PPQ            0
Jitter:DDP          0
MDVP:Shimmer        0
MDVP:Shimmer(dB)    0
Shimmer:APQ3        0
Shimmer:APQ5        0
MDVP:APQ            0
Shimmer:DDA         0
NHR                 0
HNR                 0
status              0
RPDE                0
DFA                 0
spread1             0
spread2             0
D2                  0
PPE                 0
dtype: int64
MDVP:Fo(Hz)         float64
MDVP:Fhi(Hz)        float64
MDVP:Flo(Hz)        float64
MDVP:Jitter(%)      float64
MDVP:Jitter(Abs)    float64
MDVP:RAP            float64
MDVP:PPQ            float64
Jitter:DDP          float64
MDVP:Shimmer        float64
MDVP:Shimmer(dB)    float64
Shimmer:APQ3        float64
Shimmer:APQ5        float64
MDVP:APQ            float64
Shimmer:DDA         float64
NHR                 float64
HNR                 float64
status                int64
RPDE [...]<jupyter_text>Splice out the status column into a variable y and delete it from X.<jupyter_code>y = X.loc[:, ['status']]
X.drop(labels='status', axis=1, inplace=True)<jupyter_output><empty_output><jupyter_text>Perform a train/test split. 30% test group size, with a random_state equal to 7.<jupyter_code>from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=7)<jupyter_output><empty_output><jupyter_text>Create a SVC classifier. Don't specify any parameters, just leave everything as default. Fit it against your training data and then score your testing data.<jupyter_code>from sklearn.svm import SVC

default_svc = SVC()
default_svc.fit(X_train, y_train)
score = default_svc.score(X_test, y_test)
print(score)<jupyter_output>0.813559322034<jupyter_text>Program a naive, best-parameter search by creating nested for-loops. The outer for-loop should iterate a variable C from 0.05 to 2, using 0.05 unit increments. The inner for-loop should increment a variable gamma from 0.001 to 0.1, using 0.001 unit increments. As you know, Python ranges won't allow for float intervals, so you'll have to do some research on NumPy ARanges, if you don't already know how to use them.Since the goal is to find the parameters that result in the model having the best accuracy score, you'll need a best_score = 0 variable that you initialize outside of the for-loops. Inside the inner for-loop, create an SVC model and pass in the C and gamma parameters its class constructor. Train and score the model appropriately. If the current best_score is less than the model's score, update the best_score being sure to print it out, along with the C and gamma values that resulted in it.<jupyter_code>best_score=score
for i in np.arange(start = 0.05, stop = 2.05, step = 0.05):
            for j in np.arange(start = 0.001, stop = 0.101, step = 0.001):
                model = SVC(C = i, gamma = j)
                model.fit(X_train, y_train)
                score = model.score(X_test, y_test)
                if score > best_score:
                    best_score = score
                    best_C = model.C
                    best_gamma = model.gamma
                    best_n_neighbors = iso.n_neighbors
                    best_n_components = iso.n_components
print("The highest score obtained:", best_score)
print("C value:", best_C)
print("gamma value:", best_gamma)
print("isomap n_neighbors:", best_n_neighbors)
print("isomap n_components:", best_n_components)<jupyter_output>C:\Users\sasha\Anaconda3\lib\site-packages\sklearn\utils\validation.py:526: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().
  y = column_or_1d(y, warn=True)<jupyter_text>Wait a second. Pull open the dataset's label file from: https://archive.ics.uci.edu/ml/datasets/ParkinsonsLook at the units on those columns: Hz, %, Abs, dB, etc. What happened to transforming your data? With all of those units interacting with one another, some pre-processing is surely in order.Right after you preform the train/test split but before you train your model, inject SciKit-Learn's pre-processing code. Unless you have a good idea which one is going to work best, you're going to have to try the various pre-processors one at a time, checking to see if they improve your predictive accuracy.Experiment with Normalizer(), MaxAbsScaler(), MinMaxScaler(), KernelCenterer(), and StandardScaler().<jupyter_code>from sklearn import preprocessing

#T = preprocessing.StandardScaler().fit_transform(X)
#T = preprocessing.MinMaxScaler().fit_transform(X)
#T = preprocessing.Normalizer().fit_transform(X)
T = preprocessing.scale(X)
#T = X # No Change<jupyter_output><empty_output><jupyter_text>The accuracy score keeps creeping upwards. Let's have one more go at it. Remember how in a previous lab we discovered that SVM's are a bit sensitive to outliers and that just throwing all of our unfiltered, dirty or noisy data at it, particularly in high-dimensionality space, can actually cause the accuracy score to suffer?Well, let's try to get rid of some useless features. Immediately after you do the pre-processing, run PCA on your dataset. The original dataset has 22 columns and 1 label column. So try experimenting with PCA n_component values between 4 and 14. Are you able to get a better accuracy?<jupyter_code>from sklearn.decomposition import PCA
pca = PCA(n_components = 14)
X_pca = pca.fit_transform(T)<jupyter_output><empty_output><jupyter_text>We where notIf you are not, then forget about PCA entirely. However if you are able to get a higher score, then be sure keep that accuracy score in mind, and comment out all the PCA code for now.In the same spot, run Isomap on the data. Manually experiment with every inclusive combination of n_neighbors between 2 and 5, and n_components between 4 and 6. Are you able to get a better accuracy?<jupyter_code>from sklearn.manifold import Isomap

iso = Isomap(n_neighbors=5, n_components=6)

iso.fit(X_train)
X_train = iso.transform(X_train)
X_test = iso.transform(X_test)<jupyter_output><empty_output><jupyter_text>If you are not, then forget about isomap entirely. However if you are able to get a higher score, then be sure keep that figure in mind.If either PCA or Isomap helped you out, then uncomment out the appropriate transformation code so that you have the highest accuracy possible.<jupyter_code>for k in range(2, 6):
    for l in range(4, 7):
        iso = Isomap(n_neighbors = k, n_components = l)
        X_iso = iso.fit_transform(T)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07570485902819436, 'fraction_numerical': 0.06022795440911818, 'mean_word_length': 2.822099954149473, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 1, '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': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '384292', 'license': 'MIT', 'n_tokens_mistral': 2922, 'n_tokens_neox': 2551, 'n_words': 913, 'path': 'LAb Data Modeling II/Module6 - Lab3.ipynb', 'repo_name': 'IsSev/-DAT210x', 'size': '8335'}