我正在使用framworks django编码Web应用程序,问题是将线性再学习模型保存为.pkl时出现了问题。 是否需要创建一个空的.pkl文件或它会自动生成?以及如何读取此类.pkl文件的内容?
# Libraries
# Importing Dataset
data = pd.read_csv('ml_code/ml_process/test.csv')
data.fillna(0, inplace=True)
def handle_non_numerical_data(df):
columns = df.columns.values
for column in columns:
text_digit_vals = {}
def convert_to_int(val):
return text_digit_vals[val]
if df[column].dtype != np.int64 and df[column].dtype !=
np.float64:
column_contents = df[column].values.tolist()
unique_elements = set(column_contents)
x = 0
for unique in unique_elements:
if unique not in text_digit_vals:
text_digit_vals[unique] = x
x = x + 1
df[column] = list(map(convert_to_int, df[column]))
return df
data = handle_non_numerical_data(data)
data = data.as_matrix()
#X matrice des var. explicatives
X = data[:,0:9]
#y vecteur de la var. à prédire
y = data[:,9]
X2_train, X2_test, y2_train, y2_test = train_test_split(X, y,
test_size=0.3, random_state=0)
lreg = LinearRegression()
lreg.fit(X2_train, y2_train)
print('Accuracy of linear regression on training set:
{:.2f}'.format(lreg.score(X2_train, y2_train)))
print('Accuracy of linear regression on test set:
{:.2f}'.format(lreg.score(X2_test, y2_test)))
y_pred2 = lreg.predict(X2_test)
print("Predicted Sales: %.3f" % (y_pred2[0]))
```
Saving the Logistic Regression Model
linear_regression_model = pickle.dumps(lreg)
Saving the model to a file
joblib.dump(linear_regression_model,'ml_code/linear_regression_model.pkl')
I get this error when I run the application
with open(filename, 'rb') as f:
FileNotFoundError: [Errno 2] No such file or directory:
'ml_code/linear_regression_model.pkl'