简体   繁体   中英

How to improve and fix simple python scoring algorithm

I'm trying to get my scoring function to determine using the filled columns what the null value on the last column would be. This algorithm is based on a MySQL one shown below. I'm running into alot of syntax errors in relation to the text variables, as well as it seems very streched and not condensed.

import pandas as pd
import numpy as np

d = {'BP systolic': [139, 125, 117], 
     'BP diastolic': [83, 76, 87], 
     'Gender': ['Male', 'Female', 'Female'], 
     '1st degree relative with diabetes': ['Yes', 'Yes', 'No'],
     'Physically active': ['Yes', 'Yes', 'No'],
     'Age, years': [34, 48, 57],
     'history of hypertension': ['Yes', 'No', 'Yes'],
     'BMI, kg/m²': [40, 25, 17],
     'American Diabetes Association (ADA) Risk Calculator': [np.nan, np.nan, np.nan]}


df = pd.DataFrame(data=d)    
points = []
for i in range(0, len(df['BP systolic'])):
    p = 0
    
    r1 = df['BP systolic'][i]
    r2 = df['BP diastolic'][i]
    r3 = df['Gender'][i]
    r4 = df['1st degree relative with diabetes'][i]
    r5 = df['Physically active'][i]
    r6 = df['Age, years'][i]
    r7 = df['history of hypertension'][i]
    r8 = df['BMI, kg/m²'][i]

    if r1 > 140:
        p += 1
        points.append([r1, p])
    else:
        p += 0
        points.append([r1, p])
        
    if r2 > 90:
        p += 1
        points.append([r2, p])
    else:
        p += 0
        points.append([r2, p])
        
    if r3 = 'Male':
        p += 1
        points.append([r3, p])
    else:
        p += 0
        points.append([r3, p])
     
    if r4 = 'Yes':
        p += 1
        points.append([r4, p])
    else:
        p += 0
        points.append([r4, p])
        
    if r5 = 'No':
        p += 1
        points.append([r5, p])
    else:
        p += 0
        points.append([r5, p])
        
    if r6 < 40:
        p += 0
        points.append([r6, p])
    elif r6 > 40 < 50:
        p += 1
        points.append([r6, p])
    elif r6 > 50 < 60:
        p += 2
        points.append([r6, p])
    else:
        p += 3
        points.append([r6, p])
        
    if r7 = 'Yes':
        p += 1
        points.append([r7, p])
    else:
        p += 0
        points.append([r7, p])
        
    if r8 < 25:
        p += 0
        points.append([r8, p])
    elif r8 > 25 < 30:
        p += 1
        points.append([r8, p])
    elif r8 > 30 < 40:
        p += 2
        points.append([r8, p])
    else:
        p += 3
        points.append([r8, p])
pd.options.display.max_columns = None
pd.options.display.max_rows = None
print(df)
print(points)
CREATE TABLE `trial1`.`trial1` ( `Name` TEXT NULL , `Age, years` INT NULL , `Gender` TEXT NULL , `1st degree relative with diabetes` TEXT NULL , `history of hypertension` TEXT NULL , `BP systolic` INT NULL , `BP diastolic` INT NULL , `Physically active` TEXT NULL , `BMI, kg/m²` INT NULL , `American Diabetes Association (ADA) Risk Calculator` FLOAT NULL ) ENGINE = InnoDB;

INSERT INTO `trial1` (`Name`, `Age, years`, `Gender`, `1st degree relative with diabetes`, `history of hypertension`, `BP systolic`, `BP diastolic`, `Physically active`, `BMI, kg/m²`, `American Diabetes Association (ADA) Risk Calculator`) VALUES ('Person A', '34', 'Male', 'Yes', 'Yes', '139', '83', 'Yes', '40', NULL);
INSERT INTO `trial1` (`Name`, `Age, years`, `Gender`, `1st degree relative with diabetes`, `history of hypertension`, `BP systolic`, `BP diastolic`, `Physically active`, `BMI, kg/m²`, `American Diabetes Association (ADA) Risk Calculator`) VALUES ('Person B', '48', 'Female', 'Yes', 'No', '125', '76', 'Yes', '25', NULL);
INSERT INTO `trial1` (`Name`, `Age, years`, `Gender`, `1st degree relative with diabetes`, `history of hypertension`, `BP systolic`, `BP diastolic`, `Physically active`, `BMI, kg/m²`, `American Diabetes Association (ADA) Risk Calculator`) VALUES ('Person C', '57', 'Female', 'No', 'Yes', '117', '87', 'No', '17', NULL);
update trial1
set `American Diabetes Association (ADA) Risk Calculator` = ( 
  (`BP systolic` >= 140) + (`BP diastolic` >= 90) +
  (`Gender` = 'Male') +
  (`1st degree relative with diabetes` = 'Yes') +
  (`history of hypertension` = 'Yes') +
  (`Physically active` = 'No') +
  case
    when `Age, years` < 40 then 0 
    when `Age, years` BETWEEN 40 AND 49 then 1 
    when `Age, years` BETWEEN 50 AND 59 then 2
    when `Age, years` >= 60 then 3
  end +  
  case
    when `BMI, kg/m²` < 25 then 0
    when `BMI, kg/m²` BETWEEN 25 AND 30 then 1 
    when `BMI, kg/m²` BETWEEN 31 AND 39 then 2 
    when `BMI, kg/m²` >= 40 then 3
  end
) / 12 
where `American Diabetes Association (ADA) Risk Calculator` is null

You need to use 2 equal signs. if r3 == ['Male']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM