%pylab inline
import numpy as np
import pandas as pd
import random
import time
import scipy
import sklearn.feature_extraction
import pickle
from sklearn.cross_validation import StratifiedKFold
from sklearn.svm import LinearSVC
from sklearn.externals import joblib
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.metrics import confusion_matrix
bedsizes = {'None':0,
'Rest All':1}
invbedsizes = {v: k for k, v in bedsizes.items()}
model = joblib.load('model_bed_size.pkl')
vocab = pickle.load(open('dictionary', 'rb'))
var=pd.read_csv('Train_variables.csv')
dtest = pd.read_csv('/home/ubuntu/test_null_new.csv', usecols= ("Bed_size","title","short_description","long_description","primary_shelf.all_paths_str","attributes.all_shelves.0","attributes.all_shelves.1","attributes.all_shelves.2","attributes.all_shelves.3","attributes.all_shelves.4","attributes.type.0","attributes.type.1","attributes.type.2","item_id","last_updated_at"),encoding='ISO-8859-1')
lentest = len(dtest)
vocab=vocab["Vocabulary"].to_dict()
Xall = []
i=1
for col in var['Variable']:
vectorizer = CountVectorizer(min_df=1, vocabulary=(vocab[i]), token_pattern = '\\b\\w+\\b')
Xall.append(vectorizer.transform(dtest[col].astype(str)))
j=i
i=j+1
print (col, 'Done', shape(Xall[-1]))
Xspall = scipy.sparse.hstack(Xall)
X_test_final = scipy.sparse.csr_matrix(Xspall)
print (shape(X_test_final))
ypred = model.decision_function(X_test_final)
ypredc = model.classes_[np.argmax(ypred, axis = 0)]
ypredcon = (np.max(ypred, axis = 1) + 2.)/8.
ypredcon[ypredcon < 0.] = 0 .
ypredcon[ypredcon > 1.] = 1.
dfinal = pd.DataFrame()
dfinal['item_id '] = dtest['item_id']
dfinal['Predictions'] = ypredc
dfinal['Predictions'].replace(invbedsizes, inplace = True)
dfinal['confidence_score'] = ypredcon
Der obige Code sagte einen Indexfehler geben, dass Index 14328 außerhalb der Grenzen für die Achse ist 0 und Größe 2. Der Fehler wird bei kommenden Linie ypredc = model.classes_ [np.argmax (ypred, axis = 0)]
Kann mir jemand dabei helfen?Index Fehlerindex 14238 ist außerhalb der Grenzen für die Achse 0 mit der Größe 2
Gibt es eine dauerhafte Lösung dafür? Ich habe das versucht, als ich mehrere Klassen hatte, aber jetzt, da ich dies für 2 Klassen versuche, gibt es diesen Fehler. –
Der Fix ist zu verstehen, welche Dimensionen jedes der Arrays hat, und verwenden Sie sie entsprechend. Mit anderen Worten, versuche nicht zu schummeln. Verstehen Sie das Problem und machen Sie es richtig. – hpaulj
Eigentlich für (X_test_final) ist das Array 2D aber für (ypred) kommt es als 1D, also kann es das Problem sein? –