I'm trying to make a text classifier
import pandas as pd
import pandas
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.multiclass import OneVsOneClassifier
from sklearn.svm import SVC
from sklearn import cross_validation
from sklearn.metrics import confusion_matrix
dataset = pd.read_csv('data.csv', encoding = 'utf-8')
data = dataset['text']
labels = dataset['label']
X_train, X_test, y_train, y_test = train_test_split (data, labels, test_size = 0.2, random_state = 0)
count_vector = CountVectorizer()
tfidf = TfidfTransformer()
classifier = OneVsOneClassifier(SVC(kernel = 'linear', random_state = 84))
train_counts = count_vector.fit_transform(X_train)
train_tfidf = tfidf.fit_transform(train_counts)
classifier.fit(train_tfidf, y_train)
test_counts = count_vector.transform(X_test)
test_tfidf = tfidf.transform(test_counts)
classifier.predict(test_tfidf)
fit_classifier(X_train, y_train)
predicted = predict(X_test)
print("confusion matrix")
print(confusion_matrix(X_test, predicted, labels = labels))
print("cross validation")
test_counts = count_vector.fit_transform(data)
test_tfidf = tfidf.fit_transform(test_counts)
scores = cross_validation.cross_val_score(classifier, test_tfidf, labels, cv = 10)
print(scores)
print("Accuracy: {} +/- {}".format(scores.mean(), scores.std() * 2))
But I have the following error and I can not understand.
Traceback (most recent call last):
File "classificacao.py", line 37, in
fit_classifier(X_train, y_train)
NameError: name 'fit_classifier' is not defined
But fit is not always defined by default?
you are calling a non existing function:
fit_classifier(X_train, y_train)
to fit your classifier you would use
classifier.fit(X_train, y_train)
instead.
You'll get the same error when trying to predict your test data.
You need to change
predicted = predict(X_test)
to
predicted = classifier.predict(X_test)
Your Confusionmatrix should get your labels, not your test data:
print(confusion_matrix(y_test, predicted, labels = labels))
Related
I am new to this KNN I want to ask a simple question I have written a code in python of KNN. when I used fingerprints.csv that contains decimals number my code gives me an error. I assume that it doesn't predict float values. so I used another CSV that has similar data but no decimal value my code worked well.
what changes I should make so my code will be able to predict floats.
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
import pickle
import glob
from sklearn.model_selection import GridSearchCV
from sklearn.metrics import f1_score
from sklearn.metrics import accuracy_score
from sklearn.multioutput import MultiOutputClassifier
# training/validation set
train_set = pd.read_csv("1.csv")
# test set
test_set = pd.read_csv("testing data.csv")
X = train_set.iloc[:,0:3].values #RSSI
Y = train_set.iloc[:,3:5].values #X,Y (OUTCOME)
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2, random_state=0)
#print(X_train.shape)
#print(X_test)
#print(Y_train.shape)
#print(Y_test)
sc = StandardScaler() #feature scalin
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
#print(Y_train)
aa =(X_train[:,0]+X_train[:,1]+X_train[:,2])/3
print(aa)
#import math
#print(math.sqrt(len(Y_test)))
knn = KNeighborsClassifier(n_neighbors=1, metric='euclidean')
classifier = MultiOutputClassifier(knn, n_jobs=-2)
classifier.fit(X_train, Y_train)
# Save the trained model as a pickle string.
saved_model = pickle.dumps (classifier)
# Load the pickled model
classifier_from_pickle = pickle.loads(saved_model)
# Use the loaded pickled model to make predictions
classifier_from_pickle.predict(X_test)
Y_pred = classifier.predict(X_test)
print(Y_pred)
a = Y_test[:,0] # actual labels
b = Y_pred[:,0] # predicted labels
acc = len([a[i] for i in range(0, len(a)) if a[i] == b[i]]) / len(a)
a = Y_test[:,1] # actual labels
b = Y_pred[:,1] # predicted labels
accu = len([a[i] for i in range(0, len(a)) if a[i] == b[i]]) / len(a)
accuracy=acc+accu
print("Accuracy: ",accuracy)
#Model Validation on Validation.csv
X = test_set.iloc[:,0:3].values #RSSI
#print(X)
X_test = sc.transform(X)
#print(X_test)
aa =(X_test[:,0]+X_test[:,1]+X_test[:,2])/3
print(aa)
# Use the loaded pickled model to make predictions on Validate Dataset
classifier_from_pickle.predict(X_test)
Y_pred = classifier.predict(X_test)
print(Y_pred)
the error
Traceback (most recent call last):
File "d:/knnn code/knn2.py", line 53, in <module>
classifier.fit(X_train, Y_train)
File "C:\Users\92316\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\multioutput.py", line 359, in fit
super().fit(X, Y, sample_weight)
File "C:\Users\92316\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\multioutput.py", line 156, in fit
check_classification_targets(y)
File "C:\Users\92316\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\utils\multiclass.py", line 169, in check_classification_targets
raise ValueError("Unknown label type: %r" % y_type)
ValueError: Unknown label type: 'continuous-multioutput'
thanks in advance for your time and help.
I have tried the following code and this error has been occuring to me
Link for DataSet is in link bellow
ValueError
---> line 18 ds1_model.fit(X, y)
ValueError: could not convert string to float: 'Iris-setosa'
import pandas as pd
from sklearn.metrics import mean_absolute_error
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
url = 'https://raw.githubusercontent.com/jbrownlee/Datasets/master/iris.csv'
ds1 = pd.read_csv(url)
ds1.columns = (['SepalLength' , 'SepalWidth' , 'PetalLength' , 'PetalWidth' , 'ClassLabel'])
ds1_filtered=ds1.dropna(axis=0)
y = ds1_filtered.ClassLabel
ds1_features = ['SepalLength' , 'SepalWidth' , 'PetalLength' , 'PetalWidth']
X = ds1_filtered[ds1_features]
ds1_model = DecisionTreeRegressor()
ds1_model.fit(X, y)
PredictedClassLabel = ds1_model.predict(X)
mean_absolute_error(y, PredictedClassLabel)
train_X, val_X, train_y, val_y = train_test_split(X, y, random_state = 0)
ds1_model = DecisionTreeRegressor()
ds1_model.fit(train_X, train_y)
predicitions = ds1_model.predict(val_X)
print(mean_absolute_error(val_y, predictions))
can you please help to suggest or explain how to fix this?
DataSet Link
As the name ClassLabel implies, the iris dataset is a classification and not a regression one; hence, neither DecisionTreeRegressor is the correct model to use nor mean_absolute_error is the correct metric.
You should use a DecisionTreeClassifier and accuracy_score instead:
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
iris = load_iris()
clf = DecisionTreeClassifier()
train_X, val_X, train_y, val_y = train_test_split(iris.data, iris.label, random_state = 0)
clf.fit(train_X, train_Y)
pred = clf.predict(val_X)
print(accuracy_score(val_y, pred))
The scikit-learn decision tree classification tutorial using the said dataset can give you more ideas.
I'm trying to do a score between two files. The two have the same data but not the same label. Labels from train data are corrects and the labels from test data not necessarily... and I would like to know the accuracy, recall and f-score.
import pandas
import numpy as np
import pandas as pd
from sklearn import metrics
from sklearn import cross_validation
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import precision_recall_fscore_support as score
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import precision_score, recall_score, confusion_matrix, classification_report, accuracy_score, f1_score
df_train = pd.read_csv('train.csv', sep = ',')
df_test = pd.read_csv('teste.csv', sep = ',')
vec_train = TfidfVectorizer()
X_train = vec_train.fit_transform(df_train['text'])
y_train = df_train['label']
vec_test = TfidfVectorizer()
X_test = vec_test.fit_transform(df_train['text'])
y_test = df_test['label']
clf = LogisticRegression(penalty='l2', multi_class = 'multinomial',solver ='newton-cg')
y_pred = clf.predict(X_test)
print ("Accuracy on training set:")
print (clf.score(X_train, y_train))
print ("Accuracy on testing set:")
print (clf.score(X_test, y_test))
print ("Classification Report:")
print (metrics.classification_report(y_test, y_pred))
A stupid example of the data:
TRAIN
text,label
dogs are cool,animal
flowers are beautifil,plants
pen is mine,objet
beyonce is an artist,person
TEST
text,label
dogs are cool,objet
flowers are beautifil,plants
pen is mine,person
beyonce is an artist,animal
Error:
Traceback (most recent call last):
File "accuracy.py", line 30, in
y_pred = clf.predict(X_test)
File "/usr/lib/python3/dist-packages/sklearn/linear_model/base.py", line 324, in predict
scores = self.decision_function(X)
File "/usr/lib/python3/dist-packages/sklearn/linear_model/base.py", line 298, in decision_function
"yet" % {'name': type(self).name})
sklearn.exceptions.NotFittedError: This LogisticRegression instance is not fitted yet
I just wanted to calculate the accuracy of the test
You are fitting a new TfidfVectorizer on test data. This will give wrong results. You should use the same object which you fitted on train data.
Do this:
vec_train = TfidfVectorizer()
X_train = vec_train.fit_transform(df_train['text'])
X_test = vec_train.transform(df_test['text'])
After that, as #MohammedKashif said, you need to first train your LogisticRegression model and then predict on test.
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
After that you can use the scoring code without any errors.
You have to first train your classifier object using the X_train before using the predict function over X_test. Something like this
clf = LogisticRegression(penalty='l2', multi_class = 'multinomial',solver ='newton-cg')
#Then train the classifier over training data
clf.fit(X_train, y_train)
#Then use predict function to make predictions
y_pred = clf.predict(X_test)
I'm trying to run my classifier but I get this error
import pandas
import numpy as np
import pandas as pd
from sklearn import svm
from sklearn.svm import SVC
from sklearn import cross_validation
from sklearn.metrics import confusion_matrix
from sklearn.multiclass import OneVsOneClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_fscore_support as score
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
dataset = pd.read_csv('all_topics_limpo.csv', encoding = 'utf-8')
data = pandas.get_dummies(dataset['verbatim_corrige'])
labels = dataset['label']
X_train, X_test, y_train, y_test = train_test_split(data, labels, test_size = 0.2, random_state = 0)
count_vector = CountVectorizer()
tfidf = TfidfTransformer()
classifier = OneVsOneClassifier(SVC(kernel = 'linear', random_state = 100))
#classifier = LogisticRegression()
train_counts = count_vector.fit_transform(X_train)
train_tfidf = tfidf.fit_transform(train_counts)
classifier.fit(X_train, y_train)
test_counts = count_vector.transform(X_test)
test_tfidf = tfidf.transform(test_counts)
predicted = classifier.predict(test_tfidf)
predicted = classifier.predict(X_test)
print("confusion matrix")
print(confusion_matrix(y_test, predicted, labels = labels))
print("F-score")
print(f1_score(y_test, predicted))
print(precision_score(y_test, predicted))
print(recall_score(y_test, predicted))
print("cross validation")
test_counts = count_vector.fit_transform(data)
test_tfidf = tfidf.fit_transform(test_counts)
scores = cross_validation.cross_val_score(classifier, test_tfidf, labels, cv = 10)
print(scores)
print("Accuracy: {} +/- {}".format(scores.mean(), scores.std() * 2))
My output error:
ValueError: cannot use sparse input in 'SVC' trained on dense data
I can not execute my code because of this problem and I am not understanding anything of what is happening.
all output error
Traceback (most recent call last):
File "classification.py", line 42, in
predicted = classifier.predict(test_tfidf)
File "/usr/lib/python3/dist-packages/sklearn/multiclass.py", line 584, in predict
Y = self.decision_function(X)
File "/usr/lib/python3/dist-packages/sklearn/multiclass.py", line 614, in decision_function
for est, Xi in zip(self.estimators_, Xs)]).T
File "/usr/lib/python3/dist-packages/sklearn/multiclass.py", line 614, in
for est, Xi in zip(self.estimators_, Xs)]).T
File "/usr/lib/python3/dist-packages/sklearn/svm/base.py", line 548, in predict
y = super(BaseSVC, self).predict(X)
File "/usr/lib/python3/dist-packages/sklearn/svm/base.py", line 308, in predict
X = self._validate_for_predict(X)
File "/usr/lib/python3/dist-packages/sklearn/svm/base.py", line 448, in _validate_for_predict
% type(self).name)
ValueError: cannot use sparse input in 'SVC' trained on dense data
You get this error because your training & test data are not of the same kind: while you train in your initial X_train set:
classifier.fit(X_train, y_train)
you are trying to get predictions from a dataset which has undergone count vectorization & tf-idf transormations first:
predicted = classifier.predict(test_tfidf)
It is puzzling why you choose to do so, why you nevertheless compute train_counts and train_tfidf (you don't seem to actually use them anywhere), and why you are also trying to redefine predicted as classifier.predict(X_test) immediately afterwards. Normally, changing your training line to
classifier.fit(train_tfidf, y_train)
and getting rid of your second predicted definition should work OK...
you can use this code :
test_tfidf = tfidf.transform(test_counts).toarray()
befor you want to predict your model and after :
predicted = classifier.predict(test_tfidf)
just do this simple code
nice job
I am running the [code] of multi-label classification1.how to fix the NameError that the "X_train" is not defined.the python code is given below.
import scipy
from scipy.io import arff
data, meta = scipy.io.arff.loadarff('./yeast/yeast-train.arff')
from sklearn.datasets import make_multilabel_classification
# this will generate a random multi-label dataset
X, y = make_multilabel_classification(sparse = True, n_labels = 20,
return_indicator = 'sparse', allow_unlabeled = False)
# using binary relevance
from skmultilearn.problem_transform import BinaryRelevance
from sklearn.naive_bayes import GaussianNB
# initialize binary relevance multi-label classifier
# with a gaussian naive bayes base classifier
classifier = BinaryRelevance(GaussianNB())
# train
classifier.fit(X_train, y_train)
# predict
predictions = classifier.predict(X_test)
from sklearn.metrics import accuracy_score
accuracy_score(y_test,predictions)
You forgot to split the dataset into train and test sets.
Import the library
from sklearn.model_selection import train_test_split
Add this line before classifier.fit()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
X_train does not exist, you have to split between train and test :
from sklearn.preprocessing import StandardScaler
s =StandardScaler()
X_train = s.fit_transform(X_train)
X_test = s.fit_transform(X_test)