如何为coremltools编译拟合模型?

时间:2019-05-14 02:05:02

标签: python scikit-learn coremltools

我已经建立了机器模型,需要使用coremltools将其上传到Xcode。我最初使用sklearn系综作为我的机器模型,但是coreml不支持它,因此我决定使用LinearSVC或LogisticRegression,它们在训练后具有最高的准确性。

import numpy as np
import pandas as pd

#load the dataset of the file 
#FYI:use quotation marks to escape comma or just not use the sentences
df = pd.read_csv('RhetoricalDevices1.csv', error_bad_lines=False, delimiter= ',', engine='python')

#print useful information about the data set
df.info()
df.head()

#check class distribution--number of each device uploaded
classes1 = df['Rhetorical Devices1']
classes2 = df['Rhetorical Devices2']

from sklearn.preprocessing import LabelEncoder
encoder1 = LabelEncoder()
encoder2 = LabelEncoder()

Y1 = encoder1.fit_transform(classes1.fillna('0'))
Y2 = encoder2.fit_transform(classes2.fillna('0'))
print(encoder1.inverse_transform([6]))

import nltk
from nltk.tokenize import word_tokenize

#creating a bag-of-words model
all_words = []

for sentences in processed:
    words = word_tokenize(sentences)
    for w in words:
        all_words.append(w)

all_words = nltk.FreqDist(all_words)


# use the 2000 most common words as features
word_features = list(all_words.keys())[:2000]

#define a find_feature function
def find_features(sentence):
    words = word_tokenize(sentence)
    features = {}
    for word in word_features:
        features[word] = (word in words)

    return features

#find features for all sentences
sentences = list(zip(processed, Y1))

#define a seed for reproducibility
seed = 1
np.random.seed = seed
np.random.shuffle(sentences)

#call find_features function for each sentence
featuresets = [(find_features(text), label) for (text, label) in sentences]

# split training and testing data sets using sklearn
from sklearn import model_selection

training, testing = model_selection.train_test_split(featuresets, test_size = 0.25, random_state = seed)




names = ['K Nearest Neighbors','Decision Tree','Random Forest','Logistic Regression','SGDClassifier','Multinomial','One Vs Rest Classifier']

classifiers = [
    KNeighborsClassifier(n_jobs = -1),
    DecisionTreeClassifier(class_weight = 'balanced'),
    RandomForestClassifier(),
    LogisticRegression(),
    SGDClassifier(max_iter = 100, class_weight ='balanced', n_jobs = -1),
    MultinomialNB(),
    #GaussianProcessClassifier(),
    LinearSVC()

]

models = list(zip(names, classifiers))

from nltk.classify.scikitlearn import SklearnClassifier

for name, model in models:
    nltk_model = SklearnClassifier(model)
    nltk_model.train(training)
    accuracy = nltk.classify.accuracy(nltk_model, testing)*100
    print("{} Accuracy: {}".format(name, accuracy))

当我尝试以下代码时,出现错误“ TypeError:需要转换为'适合'的模型”。我该如何解决?

model = LinearSVC()

coreml_model = coremltools.converters.sklearn.convert(model, 'Samples','Rhetorical Devices')

1 个答案:

答案 0 :(得分:0)

在将模型转换为CoreML之前,应使用训练数据在模型上调用fit()