首页 > 解决方案 > ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0. 当从不和谐消息中获取预测数据时

问题描述

当我执行以下代码时,我收到错误:

ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 21459 is different from 1)

不和谐代码:

import discord
import joblib
from sklearn.preprocessing import OrdinalEncoder
import numpy as np
intents = discord.Intents().all()
client = discord.Client(intents=intents)

loaded_model = joblib.load("finalized_model.sav")

@client.event
async def on_ready():
    print("im ready")

@client.event
async def on_message(message):
    enc1 = OrdinalEncoder()
    text = message.content
    array = np.array(text)
    array = array.reshape(1, -1)
    enc1.fit(array)
    await message.channel.send(loaded_model.predict(enc1.transform(array)))

client.run("the bot token")

保存训练模型的程序:

from sklearn.linear_model import LinearRegression
import pandas as pd
from joblib import parallel_backend
from sklearn.preprocessing import OrdinalEncoder
import joblib
import numpy as np
with parallel_backend('threading', n_jobs=1):
    enc1 = OrdinalEncoder()
    enc2 = OrdinalEncoder()
    firstLine = True
    model = LinearRegression()
    df = pd.read_csv("Emotion_final.csv")
    X = np.array(df["Text"]).reshape(1, -1)
    y = np.array(df["Emotion"]).reshape(1, -1)
    enc1.fit(X)
    enc2.fit(y)
    model.fit(enc1.transform(X), enc2.transform(y))
    filename = 'finalized_model.sav'
    joblib.dump(model, filename)

标签: pandasnumpyscikit-learndiscord.py

解决方案


推荐阅读