I'm very new to machine learning & python in general and I'm trying to apply a Decision Tree Classifier to my dataset that I'm working on.

I would like to use this model to predict the outcome after training it with certain cellular features. The training data consists of a results column, describing either a living/dead cell as 1 and 0 respectively. The additional columns are the cellular features I'm used for training.

However, I am unsure about how to apply my finalized model and introduce it to new data. What I would like to do is have it predict the "Result" tab (the 0 and 1 values) by giving it the values for 'ASA', 'ASC', 'ASMR', 'IMIH', 'IMIA', 'TCH' in the new dataset.

I would also like it to convert these predictions and possibly have it add these to a .csv file for later use, but I'm not sure how to do that.

This is the code I've been using, I'm having trouble with the segment ("Load test dataset") near the end and I think I'm doing that bit wrong, but I added the full code just as clarification.

import pandas
from pandas.plotting import scatter_matrix
import matplotlib.pyplot as plt
from sklearn import model_selection
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
import numpy

file = '/Users/Aida/Desktop/AlivevsDeadTest_Improved.csv'
names = ['Result', 'ASA', 'ASC', 'ASMR', 'IMIH', 'IMIA', 'TCH']
dataset = pandas.read_csv(file, names=names)

# Peek at the data

# Statistical summary

# Split-out validation dataset
array = dataset.values
X = array[1:,1:10]
Y = array[1:,0]
validation_size = 0.20
seed = 7
X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, Y, test_size=validation_size, random_state=seed)

# Test options and evaluation metric
seed = 7
scoring = 'accuracy'

# Spot Check Algorithms
models = []
models.append(('LR', LogisticRegression()))
models.append(('LDA', LinearDiscriminantAnalysis()))
models.append(('KNN', KNeighborsClassifier()))
models.append(('CART', DecisionTreeClassifier()))
models.append(('NB', GaussianNB()))
models.append(('SVM', SVC()))

# Evaluate each model in turn
results = []
names = []
for name, model in models:
    kfold = model_selection.KFold(n_splits=10, random_state=seed)
    cv_results = model_selection.cross_val_score(model, X_train, Y_train, cv=kfold, scoring=scoring)
    msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())

# Compare Algorithms
fig = plt.figure()
fig.suptitle('Algorithm Comparison')
ax = fig.add_subplot(111)

# Make predictions on validation dataset
cart = DecisionTreeClassifier()
cart.fit(X_train, Y_train)
predictions = cart.predict(X_validation)
print(accuracy_score(Y_validation, predictions))
print(confusion_matrix(Y_validation, predictions))
print(classification_report(Y_validation, predictions))

# Finalize model
import pickle
cart_model = DecisionTreeClassifier()
cart_model.fit(X_train, Y_train)

# Save model to disk
filename = 'Final_Model.sav'
pickle.dump(cart_model, open(filename, 'wb'))

# Load model from disk and use it to make new predictions
loaded_model = pickle.load(open(filename, 'rb'))
result = loaded_model.score(X_validation, Y_validation)

# Load test dataset
final_predict = numpy.loadtxt("AlivevsDead_Final.csv", delimiter=";")
X_train = final_predict
pred = cart_model.predict(X_train)

When I run this script it gives me an error, here's kind of what it looks like:

Traceback (most recent call last):
  File "C:/Users/Aida/Desktop/tennistesting.py", line 89, in <module>
    final_predict = numpy.loadtxt("AlivevsDead_Final.csv", delimiter=";")
ValueError: could not convert string to float: 'Result,ASA,ASC,ASMR,IMIH,IMIA,TCH'

From what I understand, machine learning consists of 3 steps, which include training, validation and finally applying it to a new dataset to perform predictions. I just don't know how to introduce this new dataset and have the model perform predictions on it.

When I run the model, I asked it to display a small segment of the dataset as clarification, this can be found below.

 Result  ASA          ASC     ...              IMIH         IMIA          TCH
0  Result  ASA          ASC     ...              IMIH         IMIA          TCH
1       1   84  1.275275533     ...       0.650034902  0.000235479  4.126984127
2       1  218  1.020682416     ...       0.339955874  0.000535448  8.125748503
3       1  207  1.453129647     ...       0.575357024   0.00061345  5.629370629
4       1  106  1.088015726     ...       0.729552852  0.000135923  7.162162162

I'm sorry if this was a silly question! I'm just very new to all of this and was hoping if anyone could possibly help me out with understanding how it's done properly.

Thanks in advance!


You are loading it incorrectly as it's a CSV file (delimiter is $,$ not $;$ by default) is what I can conclude..(may be wrong)

Try Using pandas Library..

Or exactly,

import pandas as pd
df_test = pd.read_csv(path to file)

Also you should use to_feather from pandas itself to save the files..

It's a bit faster that way...


It seems like the headings of your DataFrame,


is also the first line of your DataFrame, see where the 0th index is when you display the small segment of the dataset as clarification.

So the model thinks you first set of data is:


instead of:

1   84  1.275275533     ...       0.650034902  0.000235479  4.126984127

To stop this, remove the names=names from

dataset = pandas.read_csv(file, names=names)

because it looks like they are already the headings of the data in the csv file and pandas.read_csv will pick them up automatically.


Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.