Advertisement
Guest User

Untitled

a guest
Jun 24th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. # The script MUST contain a function named azureml_main
  2. # which is the entry point for this module.
  3.  
  4. # imports up here can be used to
  5. import pandas as pd
  6.  
  7. # The entry point function can contain up to two input arguments:
  8. #   Param<dataframe1>: a pandas.DataFrame
  9. #   Param<dataframe2>: a pandas.DataFrame
  10. def azureml_main(dataframe1):
  11.  
  12.     # Execution logic goes here
  13.     print('Input pandas.DataFrame #1:\r\n\r\n{0}'.format(dataframe1))
  14.  
  15.     # If a zip file is connected to the third input port is connected,
  16.     # it is unzipped under ".\Script Bundle". This directory is added
  17.     # to sys.path. Therefore, if your zip file contains a Python file
  18.     # mymodule.py you can import it using:
  19.     # import mymodule
  20.  
  21.     products = {}
  22.     for i in range(len(dataframe1['Product'])):
  23.         name = dataframe1['Product'][i]
  24.         if name in products:
  25.             products[name] += dataframe1['Income'][i]
  26.         else:
  27.             products[name] = dataframe1['Income'][i]
  28.     data = {}
  29.     data['Product'] = list(products.keys())
  30.     result = pd.DataFrame(data)
  31.     result['Income'] = list(products.values())
  32.  
  33.     periods = {}
  34.     for i in range(1996 * 12 + 7, 1998 * 12 + 6):
  35.         periods[i] = {}
  36.     for i in range(len(dataframe1['Product'])):
  37.         date = dataframe1['Date'][i]
  38.         arr = date.split('.')
  39.         m = int(arr[1])
  40.         y = int(arr[2])
  41.         name = dataframe1['Product'][i]
  42.         income = dataframe1['Income'][i]
  43.         month = y * 12 + m
  44.         if name in periods[month]:
  45.             periods[month][name] += income
  46.         else:
  47.             periods[month][name] = income
  48.  
  49.     xyz = []
  50.     for name in products:
  51.         mean = 0.0
  52.         for month in periods:
  53.             if name in periods[month]:
  54.                 mean += periods[month][name]
  55.         mean /= len(periods)
  56.         sigma = 0.0
  57.         for month in periods:
  58.             if name in periods[month]:
  59.                 sigma += (periods[month][name] - mean) ** 2
  60.             else:
  61.                 sigma += mean ** 2
  62.         sigma /= len(periods)
  63.         sigma = sigma ** 0.5
  64.  
  65.         var = 1.0 * sigma / mean
  66.         if 100 * var <= 85:
  67.             xyz.append('X')
  68.             continue
  69.         if 100 * var <= 100:
  70.             xyz.append('Y')
  71.             continue
  72.         else:
  73.             xyz.append('Z')
  74.             continue
  75.  
  76.     result['ABC'] = pd.Series(len(products), index=result.index)
  77.     result['XYZ'] = pd.Series(len(products), index=result.index)
  78.     for i in range(len(products)):
  79.         result['XYZ'][i] = xyz[i]
  80.     result.sort_values(by=['Income'], ascending=False, inplace=True)
  81.     sumOfIncomes = result['Income'].sum()
  82.     result = result.reset_index(drop=True)
  83.     accumulate = 0
  84.     s = result['Income']
  85.     for i in range(len(products)):
  86.         accumulate += s[i]
  87.         if (accumulate <= 0.7 * sumOfIncomes):
  88.             result['ABC'][i] = 'A'
  89.         elif (accumulate <= 0.9 * sumOfIncomes):
  90.             result['ABC'][i] = 'B'
  91.         else:
  92.             result['ABC'][i] = 'C'
  93.  
  94.     # Return value must be of a sequence of pandas.DataFrame
  95.     return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement