ValueError:无法将字符串转换为浮点值:将数据从sql server加载到Predict()时



我正在使用SKLEARN&Pandas加载数据集进行预测。在训练数据上,它就像一种魅力,问题来了。我正在将数据帧传递给预测函数(我直接从SQL server加载这些数据)。错误:

ValueError:无法将字符串转换为浮点值:'MESSAGE:向我发送电子邮件\r’

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import re
import csv
import pyodbc
server = "{10.66.74.80}"
db = "{SMS}"
con = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db)
query = "SELECT Prediction,Message from HC_followup where prediction in ('Sat','Dis_Sat')"
df = pd.read_sql(query, con)
df.head()
train_df,test_df=train_test_split(df,test_size=0.2,random_state=0)
train_df.loc[train_df['Prediction']=='Dis_Sat','Prediction']=0
train_df.loc[train_df['Prediction']=='Sat','Prediction']=1
X=train_df['Message']
X.head()
train_y=train_df['Prediction'].values
train_y=train_y.astype(np.int)
train_y[:]
from sklearn.feature_extraction.text import CountVectorizer
count_vec=CountVectorizer()
count_vec
count_vec.fit(X)
train_x=count_vec.transform(X).toarray()
train_x[:]
train_x.shape
from sklearn.linear_model import LogisticRegression
lr_clf=LogisticRegression()
lr_clf
lr_clf.fit(train_x,train_y)
server = "{W10HSVQXX1}"
db = "{test}"
con = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db)
query = "select Message from [dbo].[followup] where prediction ='Un-Known'"
df_test = pd.read_sql(query, con)
df_test.head()
cnt=CountVectorizer()
cnt
print(df_test.shape)
res=lr_clf.predict(count_vec.transform(df_test))
print(res)
if res==0:
print("Customer Is Dis_Sat")
if res==1:
print("Customer Is Sat")
print("Accuracy Percentage : ",lr_clf.score(train_x,train_y)*100,'%')

注意:模型已经过培训和安装。

接受任何建议/意见。我是这项技术的新手。谢谢

似乎是直接将文本数据输入到逻辑回归模型中。可能您在培训期间使用过CountVectorizer/TfidfVectorizer。使用相同的矢量器执行transform()。然后将转换后的文本数据输入到逻辑回归模型中。

请参阅我在预测线上的建议。

编辑:

from sklearn.linear_model import LogisticRegression 
lr_clf.fit(train_x,train_y) 
server = "{W10HSVQXX1}"
db = "{test}" 
con = pyodbc.connect('DRIVER={SQL Server};SERVER=' + server + ';DATABASE=' + db) 
query = "select Message from [dbo].[followup] where prediction ='Un-Known'" 
df_test = pd.read_sql(query, con) 
df_test.head() 
len(count_vec.get_feature_names()) 
res=lr_clf.predict(count_vec.transform(df_test))
print(res) 

相关内容

  • 没有找到相关文章

最新更新