创建一个加载模型、获取数据向量并进行预测的函数
我正在尝试在 Boston Housing 数据集上训练回归模型,并将模型保存到磁盘,然后创建 Title 函数。
下面的代码正在工作
import pandas as pd
import numpy as np
import os
import pickle
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from joblib import dump, load
boston = load_boston()
boston_df = pd.DataFrame(boston.data, columns = boston.feature_names)
y = pd.DataFrame(boston.target)
x_train, x_test, y_train, y_test = train_test_split(boston_df, y, test_size = 0.2, random_state=17)
model = LinearRegression()
model.fit(x_train, y_train)
y_predict = model.predict(x_test)
mse = mean_squared_error(y_test, y_predict)
with open('model.pkl', 'wb') as file:
pickle.dump(model, file)
del model
with open('model.pkl', 'rb') as file:
lin_model = pickle.load(file)
但是当我添加它时它应该工作
def predicting(x):
x = pd.DataFrame(x)
with open('model.pkl', 'rb') as file:
lin_model = pickle.load(file)
pred = lin_model.predict(x)
print(pred)
但我不知道“x”需要什么结构才能使这个功能工作!
x = [1,2,4,5,6,3,5,6,7,3,5,6,4]
predicting(x)
像上面一样,它说 ValueError: matmul: 输入操作数 1 在其核心维度 0 中不匹配,具有 gufunc 签名 (n?,k),(k,m?)->(n?,m?) (尺寸 13 与 1) 不同
请帮忙?
I'm trying to train a regression model on the Boston Housing dataset and save the model to disk and then make the Title function.
Below code's is working
import pandas as pd
import numpy as np
import os
import pickle
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
from joblib import dump, load
boston = load_boston()
boston_df = pd.DataFrame(boston.data, columns = boston.feature_names)
y = pd.DataFrame(boston.target)
x_train, x_test, y_train, y_test = train_test_split(boston_df, y, test_size = 0.2, random_state=17)
model = LinearRegression()
model.fit(x_train, y_train)
y_predict = model.predict(x_test)
mse = mean_squared_error(y_test, y_predict)
with open('model.pkl', 'wb') as file:
pickle.dump(model, file)
del model
with open('model.pkl', 'rb') as file:
lin_model = pickle.load(file)
But when I add it's supossed to work
def predicting(x):
x = pd.DataFrame(x)
with open('model.pkl', 'rb') as file:
lin_model = pickle.load(file)
pred = lin_model.predict(x)
print(pred)
But I don't know what structure does 'x' need for this function to work!
x = [1,2,4,5,6,3,5,6,7,3,5,6,4]
predicting(x)
Like above it says ValueError: matmul: Input operand 1 has a mismatch in its core dimension 0, with gufunc signature (n?,k),(k,m?)->(n?,m?) (size 13 is different from 1)
Please help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有最终的印刷品,但我看到了它并认为它很漂亮,但这就是我需要的! :)
Without the final print but I saw it and thought that was pretty, but here's what I needed! :)