未在 AzureML 中获得预期的输出
Not Getting the Expected Output in AzureML
背景:我正在从事一个项目,旨在使用 Azure ML 中的情感分析将产品评论分为正面评论和负面评论。我在将评论分类到不同部门时卡住了。
我基本上是从 csv 文件中读取单词并检查评论(v:句子列表)是否包含这些单词。如果在评论中发现其中一些词,那么我会记下句子编号并将其推入相应的列表(FinanceList、QualityList、LogisticsList)。最后,我将列表转换为字符串并将它们推送到数据框中。
我在 Azure ML 脚本中编写的打印语句的输出未被记录。
数据框中的值总是变成 0,但是当我 运行 本地代码时,我得到了预期的输出。
第一张图片的描述:数据框的列显示 0 个值。
第二张图片的描述:我已经突出显示了我在本地获得的预期输出,用于 AzureML 中使用的相同评论。
我已经检查过的东西:
- 已正确读取 csv 文件。
- 评论中包含我正在搜索的字词。
我无法理解哪里出错了。
'
import csv
import math
import pandas as pd
import numpy as np
def azureml_main( data, ud):
FinanceDept = []
LogisticsDept = []
QualityDept = []
#Reading from the csv files
with open('.\Script Bundle\quality1.csv', 'rb') as fin:
reader = csv.reader(fin)
QualityDept = list(reader)
with open('.\Script Bundle\finance1.csv', 'rb') as f:
reader = csv.reader(f)
FinanceDept = list(reader)
with open('.\Script Bundle\logistics1.csv', 'rb') as f:
reader = csv.reader(f)
LogisticDept = list(reader)
FinanceList = []
LogisticsList = []
QualityList = []
#Initializing the Lists
FinanceList.append(0)
LogisticsList.append(0)
QualityList.append(0)
rev = data['Data']
v = rev[0].split('.')
print FinanceDept
S = 0
for sentence in v:
S = S + 1
z = sentence.split(' ')
for c in z:
c = c.lower()
if c in FinanceDept and S not in FinanceList:
FinanceList.append(S)
if c in LogisticsDept and S not in LogisticsList:
LogisticsList.append(S)
if c in QualityDept and S not in QualityList:
QualityList.append(S)
#Compute User Reputation Score
Upvotes = int(ud['upvotes'].tolist()[0])
Downvotes = int(ud['downvotes'].tolist()[0])
TotalVotes = max(1,Upvotes+Downvotes)
q = data['Score']
print FinanceList
repScore = float(Upvotes)/TotalVotes
repScore = repScore*float( q[0] )
str1 = ','.join(str(e) for e in FinanceList)
str2 = ','.join(str(e) for e in QualityList)
str3 = ','.join(str(e) for e in LogisticsList)
x = ud['id']
#df = pd.DataFrame( [str(repScore), str1 , str2 , str3 ], columns=[Write the columns])
d = {'id': x[0], 'Score': float(repScore),'Logistics':str3,'Finance':str1,'Quality':str2}
df = pd.DataFrame(data=d, index=np.arange(1))
return df,`
你能检查文件路径是否正确吗,根据 https://azure.microsoft.com/en-us/documentation/articles/machine-learning-execute-python-scripts/,Python 代码无法访问它运行的机器上的大多数目录,当前目录及其子目录除外-目录。 "Script Bundle" 是子目录吗?您也可以尝试使用输入而不是从脚本中读取 csv 文件。执行 Python 脚本模块接受 3 个输入,前两个是数据帧,第三个是为 python 库文件保留的。例如,您可以将输入 1 用于实际数据,将输入 2 用于最初在 csv 中的单词。需要有一种机制将 3 个 csv 文件捆绑到一个输入 2 的数据框中。
@Anuj Shankar,
经过同事测试,我们可以从CSV
个文件中读取数据,得到了预期的结果。本经验请参考:
1) 输入数据 - 它有 apple.zip
文件,其中有两个 csv
文件与您相似,每个 csv 文件包含与公司相关的词袋。
2) Python 脚本:
# The script MUST contain a function named azureml_main
# which is the entry point for this module.
#
# The entry point function can contain up to two input arguments:
# Param<dataframe1>: a pandas.DataFrame
# Param<dataframe2>: a pandas.DataFrame
import csv
import numpy as np
import pandas as pd
def azureml_main(dataframe1 = None, dataframe2 = None):
# Execution logic goes here
#print('Input pandas.DataFrame #1:\r\n\r\n{0}'.format(dataframe1))
# If a zip file is connected to the third input port is connected,
# it is unzipped under ".\Script Bundle". This directory is added
# to sys.path. Therefore, if your zip file contains a Python file
# mymodule.py you can import it using:
# import mymodule
apple = {}
microsoft = {}
#Reading from the csv files
with open('.\Script Bundle\apple.csv', 'rb') as f:
reader = csv.reader(f)
apple = list_to_dict(list(reader)[0])
with open('.\Script Bundle\microsoft.csv', 'rb') as f:
reader = csv.reader(f)
microsoft = list_to_dict(list(reader)[0])
# print('hello world' + ' '.join(apple[0]))
applecount = 0
microsoftcount = 0
input = "i want to buy surface which runs on windows"
splitted_input = input.split(' ')
for word in splitted_input:
if word in apple:
applecount = applecount + 1
if word in microsoft:
microsoftcount = microsoftcount + 1
print("apple bag of words count - " + str(applecount))
print("microsoft bag of words count - " + str(microsoftcount))
mydata = [{'input words': len(splitted_input)}, {'applecount':applecount},
{'microsoftcount':microsoftcount}]
# Return value must be of a sequence of pandas.DataFrame
return pd.DataFrame(mydata),
def list_to_dict(li):
dct = {}
for item in li:
if dct.has_key(item):
dct[item] = dct[item] + 1
else:
dct[item] = 1
return dct
3) 输出——如果我考虑字符串 "i want to buy surface which runs on windows"。它有 2 个与微软相关的词和 0 个与苹果相关的词,如下图所示。
背景:我正在从事一个项目,旨在使用 Azure ML 中的情感分析将产品评论分为正面评论和负面评论。我在将评论分类到不同部门时卡住了。
我基本上是从 csv 文件中读取单词并检查评论(v:句子列表)是否包含这些单词。如果在评论中发现其中一些词,那么我会记下句子编号并将其推入相应的列表(FinanceList、QualityList、LogisticsList)。最后,我将列表转换为字符串并将它们推送到数据框中。
我在 Azure ML 脚本中编写的打印语句的输出未被记录。
数据框中的值总是变成 0,但是当我 运行 本地代码时,我得到了预期的输出。
第一张图片的描述:数据框的列显示 0 个值。
第二张图片的描述:我已经突出显示了我在本地获得的预期输出,用于 AzureML 中使用的相同评论。
我已经检查过的东西:
- 已正确读取 csv 文件。
- 评论中包含我正在搜索的字词。
我无法理解哪里出错了。
'
import csv
import math
import pandas as pd
import numpy as np
def azureml_main( data, ud):
FinanceDept = []
LogisticsDept = []
QualityDept = []
#Reading from the csv files
with open('.\Script Bundle\quality1.csv', 'rb') as fin:
reader = csv.reader(fin)
QualityDept = list(reader)
with open('.\Script Bundle\finance1.csv', 'rb') as f:
reader = csv.reader(f)
FinanceDept = list(reader)
with open('.\Script Bundle\logistics1.csv', 'rb') as f:
reader = csv.reader(f)
LogisticDept = list(reader)
FinanceList = []
LogisticsList = []
QualityList = []
#Initializing the Lists
FinanceList.append(0)
LogisticsList.append(0)
QualityList.append(0)
rev = data['Data']
v = rev[0].split('.')
print FinanceDept
S = 0
for sentence in v:
S = S + 1
z = sentence.split(' ')
for c in z:
c = c.lower()
if c in FinanceDept and S not in FinanceList:
FinanceList.append(S)
if c in LogisticsDept and S not in LogisticsList:
LogisticsList.append(S)
if c in QualityDept and S not in QualityList:
QualityList.append(S)
#Compute User Reputation Score
Upvotes = int(ud['upvotes'].tolist()[0])
Downvotes = int(ud['downvotes'].tolist()[0])
TotalVotes = max(1,Upvotes+Downvotes)
q = data['Score']
print FinanceList
repScore = float(Upvotes)/TotalVotes
repScore = repScore*float( q[0] )
str1 = ','.join(str(e) for e in FinanceList)
str2 = ','.join(str(e) for e in QualityList)
str3 = ','.join(str(e) for e in LogisticsList)
x = ud['id']
#df = pd.DataFrame( [str(repScore), str1 , str2 , str3 ], columns=[Write the columns])
d = {'id': x[0], 'Score': float(repScore),'Logistics':str3,'Finance':str1,'Quality':str2}
df = pd.DataFrame(data=d, index=np.arange(1))
return df,`
你能检查文件路径是否正确吗,根据 https://azure.microsoft.com/en-us/documentation/articles/machine-learning-execute-python-scripts/,Python 代码无法访问它运行的机器上的大多数目录,当前目录及其子目录除外-目录。 "Script Bundle" 是子目录吗?您也可以尝试使用输入而不是从脚本中读取 csv 文件。执行 Python 脚本模块接受 3 个输入,前两个是数据帧,第三个是为 python 库文件保留的。例如,您可以将输入 1 用于实际数据,将输入 2 用于最初在 csv 中的单词。需要有一种机制将 3 个 csv 文件捆绑到一个输入 2 的数据框中。
@Anuj Shankar,
经过同事测试,我们可以从CSV
个文件中读取数据,得到了预期的结果。本经验请参考:
1) 输入数据 - 它有 apple.zip
文件,其中有两个 csv
文件与您相似,每个 csv 文件包含与公司相关的词袋。
# The script MUST contain a function named azureml_main
# which is the entry point for this module.
#
# The entry point function can contain up to two input arguments:
# Param<dataframe1>: a pandas.DataFrame
# Param<dataframe2>: a pandas.DataFrame
import csv
import numpy as np
import pandas as pd
def azureml_main(dataframe1 = None, dataframe2 = None):
# Execution logic goes here
#print('Input pandas.DataFrame #1:\r\n\r\n{0}'.format(dataframe1))
# If a zip file is connected to the third input port is connected,
# it is unzipped under ".\Script Bundle". This directory is added
# to sys.path. Therefore, if your zip file contains a Python file
# mymodule.py you can import it using:
# import mymodule
apple = {}
microsoft = {}
#Reading from the csv files
with open('.\Script Bundle\apple.csv', 'rb') as f:
reader = csv.reader(f)
apple = list_to_dict(list(reader)[0])
with open('.\Script Bundle\microsoft.csv', 'rb') as f:
reader = csv.reader(f)
microsoft = list_to_dict(list(reader)[0])
# print('hello world' + ' '.join(apple[0]))
applecount = 0
microsoftcount = 0
input = "i want to buy surface which runs on windows"
splitted_input = input.split(' ')
for word in splitted_input:
if word in apple:
applecount = applecount + 1
if word in microsoft:
microsoftcount = microsoftcount + 1
print("apple bag of words count - " + str(applecount))
print("microsoft bag of words count - " + str(microsoftcount))
mydata = [{'input words': len(splitted_input)}, {'applecount':applecount},
{'microsoftcount':microsoftcount}]
# Return value must be of a sequence of pandas.DataFrame
return pd.DataFrame(mydata),
def list_to_dict(li):
dct = {}
for item in li:
if dct.has_key(item):
dct[item] = dct[item] + 1
else:
dct[item] = 1
return dct
3) 输出——如果我考虑字符串 "i want to buy surface which runs on windows"。它有 2 个与微软相关的词和 0 个与苹果相关的词,如下图所示。