Я разработал 3 модели ML в spyder, это линейная регрессия, полиномиальная регрессия и случайная регрессия леса. В sypder все они работали хорошо. Однако когда я развернул в Django для создания веб-приложения, Random Forest поднял «ValueError: Несоответствие типов буфера, ожидалось SIZE_t, но получилось« long long »». (Я пытался удалить randomforest, и другие две модели работали хорошо).
Сначала проверьте это: - ValueError Изображение CMD
Модель разработана в Sypder
"""****************** Import Lib ******************"""
import numpy as np
import pandas as pd
import seaborn as sns
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
"""****************** Loading dataset ******************"""
boston = load_boston()
dataset = pd.DataFrame(boston.data, columns=boston.feature_names)
dataset['target'] = boston.target
"""****************** Data Preprocessing ******************"""
""" Data Analysis """
# Check Null
dataset.isnull().sum()
# Calculate X and y
X = dataset.iloc[:,:-1].values
y = dataset.iloc[:,-1].values.reshape(-1,1)
# train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=25)
""" Visualizing Data """
corr = dataset.corr()
sns.heatmap(corr, annot=True, cmap='Blues')
sns.pairplot(dataset)
"""****************** Regression Models ******************"""
""" Linear Regression """
from sklearn.linear_model import LinearRegression
regressor_linear = LinearRegression()
regressor_linear.fit(X_train, y_train)
cv_linear = cross_val_score(estimator = regressor_linear, X=X_train, y=y_train, cv=10)
""" Polynomial Regression """
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 2)
X_poly = poly_reg.fit_transform(X_train)
regressor_poly2 = LinearRegression()
regressor_poly2.fit(X_poly, y_train)
cv_poly2 = cross_val_score(estimator=regressor_poly2, X=X_poly, y=y_train, cv=10)
""" Random Forest Regression """
from sklearn.ensemble import RandomForestRegressor
regressor_rf = RandomForestRegressor(n_estimators=500, random_state=0, n_jobs=-1)
regressor_rf.fit(X_train, y_train.ravel())
cv_rf = cross_val_score(estimator=regressor_rf, X=X_train, y=y_train.ravel(), cv=10)
"""****************** Measuring the Error ******************"""
models=[
('Linear Regression', cv_linear.mean()),
('Polynomial Regression (2)', cv_poly2.mean()),
('Random Forest Regression', cv_rf.mean())
]
cv_scores = pd.DataFrame(data=models, columns=['Model','CV Score'])
"""****************** Dump ******************"""
from sklearn.externals import joblib
joblib.dump(regressor_linear,'regressor_linear_jb')
joblib.dump(regressor_poly2,'regressor_poly2_jb')
joblib.dump(regressor_rf,'regressor_rf_jb')
Django Код реализации
from django.shortcuts import render
from django.http import HttpResponse
import json
from django.http import JsonResponse
import pandas as pd
import numpy as np
from sklearn.externals import joblib
from sklearn.preprocessing import PolynomialFeatures
# Create your views here.
# ML Code
regressor_linear = joblib.load('./models/regressor_linear_jb')
regressor_poly2 = joblib.load('./models/regressor_poly2_jb')
regressor_rf = joblib.load('./models/regressor_rf_jb')
# ML Code End
def predict(request):
temp_data = [
0.16902,
0,
25.65,
0,
0.581,
5.986,
88.4,
1.9929,
2,
188,
19.1,
385.02,
14.81,
]
temp_df = pd.DataFrame(temp_data).transpose()
predict = {}
# Linear Regression
predict['Linear Regressor'] = round(regressor_linear.predict(temp_df)[0, 0], 2)
# Polynomial Regression.
regressor_poly = PolynomialFeatures(degree=2)
temp_df_poly = regressor_poly.fit_transform(temp_df)
predict['Polynomial Regressor'] = round(regressor_poly2.predict(temp_df_poly)[0, 0], 2)
# Random Forest Regression
predict['Random Forest Regressor'] = round(regressor_rf.predict(temp_df)[0],2)
return JsonResponse(predict)