SVM模型 + 导出MLCore 最终方案
import pandas as pd
import numpy as np
# 读取 xlsx 文件
df = pd.read_excel('/Users/blibli/Documents/答辩数据/原始数据/T6发版本后修改/18939_20230116161751_处理中.xlsx')
df.fillna(0, inplace=True)
df = df.sample(n=8000)
# 连续数据标准化处理
df['VideoMusicIndex'] = (df['VideoMusicIndex'] - df['VideoMusicIndex'].mean()) / df['VideoMusicIndex'].std()
df['WordingCount_format100'] = (df['WordingCount_format100'] - df['WordingCount_format100'].mean()) / df[
'WordingCount_format100'].std()
df['videoBitrate_format10000'] = (df['videoBitrate_format10000'] - df['videoBitrate_format10000'].mean()) / df[
'videoBitrate_format10000'].std()
df['width_height_1080'] = (df['width_height_1080'] - df['width_height_1080'].mean()) / df[
'width_height_1080'].std()
df_onehot = pd.get_dummies(df, columns=['EnterScene', 'VideoSource', 'LbsFlag', 'MusicType', 'professional_type',
'sex_info', 'videoFrameRate_format', 'font_level'])
# 使用 pop 方法将 'postRet_format' 列从 DataFrame 中移除
postRet_format_column = df_onehot.pop('postRet_format')
# 使用 assign 方法将 'postRet_format' 列添加到 DataFrame 的末尾
df_onehot = df_onehot.assign(postRet_format=postRet_format_column)
df_onehot = df_onehot.head(13000) # 12000 精确度96%
from sklearn.model_selection import train_test_split
# print(df_onehot)
# print(df_onehot.columns)
print(df_onehot.shape[1])
# 将数据分成特征和目标
X = df_onehot.iloc[:, :(df_onehot.shape[1] - 1)] # 填41时表示:选择前 41 列作为特征
y = df_onehot.iloc[:, (df_onehot.shape[1] - 1)] # 选择第 42 作为目标
# print('y' + y)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.feature_selection import RFE
from sklearn.svm import SVC
# 将数据分成训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# 创建SVM模型
model = SVC(kernel='linear')
print('will begin')
# 使用RFE筛选出前10的特征量
# Recursive Feature Elimination(RFE)算法是数据竞赛中最为流行的特征筛选方案之一
selector = RFE(model, n_features_to_select=10)
X_new = selector.fit_transform(X, y)
print(X_new)
# 在训练数据上训练模型
model.fit(X_train, y_train)
print('will test')
# 在测试数据上测试模型
y_pred = model.predict(X_test)
# 计算模型的准确率
accuracy = accuracy_score(y_test, y_pred)
print('准确率:', accuracy)
print('params !')
print(model.get_params())
print('hello')
import coremltools
print('begin mlmodel')
input_features = np.array(X.columns)
print(input_features)
output_feature = "post"
input_model = coremltools.converters.sklearn.convert(model, input_features, output_feature)
# Set model metadata
input_model.author = 'binbinwang'
input_model.license = 'BSD'
input_model.short_description = 'Predict whether users will actually publish in the publishing interface.'
print('begin input_description')
# Set feature descriptions manually
# input_model.input_description['tv'] = 'Ads price spent on tv'
# input_model.input_description['radio'] = 'Ads price spent on radio'
# input_model.input_description['newspaper'] = 'Ads price spent on newspaper'
# Set the output descriptions
input_model.output_description['post'] = 'Users will trigger publishing behavior'
input_model.save("FinderPostModel.mlmodel")
print('Predict success')