【问题标题】:How to compile fitted model for coremltools?如何为 coremltools 编译拟合模型?
【发布时间】:2019-09-30 23:41:28
【问题描述】:

我已经制作了我的机器模型,需要使用 coremltools 将其上传到 Xcode。我最初使用 sklearn ensemble 作为我的机器模型,但 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: Expected a 'fitted' model for conversion”。我应该如何解决这个问题?

model = LinearSVC()

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

【问题讨论】:

    标签: python scikit-learn coremltools


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 2019-05-23
      • 2021-09-13
      • 1970-01-01
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多