主要是用了LabelEncoder()将object类型转化为数值,一开始忘记LabelEncoder()的用法了,记录一下子。。 (在代码的26行附近)
# coding: utf-8
# In[1]:
from sklearn.externals import joblib
import pandas as pd
import numpy as np
from sklearn import preprocessing
from sklearn.ensemble import GradientBoostingRegressor
from sklearn import cross_validation, metrics
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import train_test_split
import warnings
warnings.filterwarnings("ignore")
import time
# In[2]:
data=pd.read_excel("./MCM_NFLIS_Data.xlsx")
encoder=["State","COUNTY","SubstanceName"]
print("encoding...")
col_encoder = preprocessing.LabelEncoder()
for feat in encoder:
col_encoder.fit(data[feat])
data[feat] = col_encoder.transform(data[feat])
data.to_csv("Out_to_number1.csv",index=False)
print("数值化 over...")
# In[3]:
label=data["TotalDrugReportsState"]
data=data.drop(["TotalDrugReportsState"], axis=1)
X_train, X_test, y_train, y_test = train_test_split(data, label,
test_size=0.2,
random_state=21)
# In[6]:
#构建模型
model = GradientBoostingRegressor()
model.fit(X_train,y_train)
print("fiting over..")
# In[12]:
y_pred = model.predict(X_test)
print("模型预测",y_pred)
print("真实数据:",y_test.values)
df=pd.DataFrame({"模型预测":y_pred,"真实数据":y_test})
# In[13]:
print(df)