首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为coremltools编写拟合模型?

如何为coremltools编写拟合模型?
EN

Stack Overflow用户
提问于 2019-05-14 10:05:02
回答 1查看 96关注 0票数 0

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

代码语言:javascript
复制
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:期望一个‘合适的’模型来进行转换“。我该如何解决这个问题呢?

代码语言:javascript
复制
model = LinearSVC()

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

回答 1

Stack Overflow用户

发布于 2019-06-14 19:35:30

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56122055

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档