大家都知道BMI指数吧。
但是不同的人会有不同的BMI
我给大家程序。
import tkinter as tk from tkinter import messagebox from joblib import load import pandas as pd import numpy as np # 加载BMI数据 data = pd.read_csv('bmi.csv') # 加载分类器 clf = load('clf.pkl') # 计算BMI def calculate_bmi(): height = float(entry_height.get()) weight = float(entry_weight.get()) bmi = weight / (height * height) return bmi # 计算建议的体重 def calculate_suggested_weight(): height = float(entry_height.get()) desired_bmi = 24.0 suggested_weight = desired_bmi * (height * height) return suggested_weight # 预测BMI的范围 def predict_bmi_range(): sex = variable_sex.get() age = int(entry_age.get()) height = float(entry_height.get()) weight = float(entry_weight.get()) bmi = weight / (height * height) X = np.array([[sex, age, height, weight]]) bmi_range = clf.predict(X) return bmi_range[0] # 显示结果 def show_result(): bmi = calculate_bmi() bmi_range = predict_bmi_range() suggested_weight = calculate_suggested_weight() if bmi < 18.5: if bmi_range == 'adult_man' or bmi_range == 'adult_woman': messagebox.showinfo("结果", "您的BMI指数为:{},体重过轻。建议增加锻炼,增强身体素质。".format(bmi)) elif bmi_range == 'child_man' or bmi_range == 'child_woman': messagebox.showinfo("结果", "您的BMI指数为:{},体重过轻。建议增加营养,保证健康成长。".format(bmi)) elif 18.5 <= bmi < 24.0: if bmi_range == 'adult_man' or bmi_range == 'adult_woman': messagebox.showinfo("结果", "您的BMI指数为:{},体重正常。保持良好的生活习惯,有助于保持健康的身体。".format(bmi)) elif bmi_range == 'child_man' or bmi_range == 'child_woman': messagebox.showinfo("结果", "您的BMI指数为:{},体重正常。保持良好的生活习惯,有助于健康成长。".format(bmi)) elif 24.0 <= bmi < 28.0: if bmi_range == 'adult_man' or bmi_range == 'adult_woman': messagebox.showinfo("结果", "您的BMI指数为:{},体重过重。建议控制饮食,增加锻炼,以保持健康的身体。".format(bmi)) elif bmi_range == 'child_man' or bmi_range == 'child_woman': messagebox.showinfo("结果", "您的BMI指数为:{},体重过重。建议控制饮食,增加锻炼,以保持健康的身体。".format(bmi)) else: if bmi_range == 'adult_man' or bmi_range == 'adult_woman': messagebox.showinfo("结果", "您的BMI指数为:{},肥胖。建议控制饮食,增加锻炼,以保持健康的身体。".format(bmi)) elif bmi_range == 'child_man' or bmi_range == 'child_woman': messagebox.showinfo("结果", "您的BMI指数为:{},肥胖。建议控制饮食,增加锻炼,以保持健康的身体。".format(bmi)) # 创建主窗口 win = tk.Tk() win.title("BMI计算与建议") # 创建变量 variable_sex = tk.StringVar() # 创建标签和输入框 label_height = tk.Label(win, text="请输入身高(米):") label_height.pack() entry_height = tk.Entry(win) entry_height.pack() label_weight = tk.Label(win, text="请输入体重(千克):") label_weight.pack() entry_weight = tk.Entry(win) entry_weight.pack() label_sex = tk.Label(win, text="请选择性别:") label_sex.pack() radio_man = tk.Radiobutton(win, text="男", variable=variable_sex, value="man") radio_man.pack() radio_woman = tk.Radiobutton(win, text="女", variable=variable_sex, value="woman") radio_woman.pack() label_age = tk.Label(win, text="请输入年龄:") label_age.pack() entry_age = tk.Entry(win) entry_age.pack() # 创建按钮 button_calculate = tk.Button(win, text="计算BMI", command=show_result) button_calculate.pack() # 运行主循环 win.mainloop()
这是python文件
还有几个附加文件
sex,age,height,weight,bmi man,25,1.75,70,23.5 man,30,1.8,80,24.5 man,20,1.7,60,22.8 woman,25,1.65,65,22.5 woman,20,1.6,55,21.5 man,25,1.7,70,23.5 woman,30,1.7,75,23.5 man,22,1.72,65,22.9 woman,28,1.68,60,21.7 man,18,1.65,55,20.5 woman,19,1.55,45,19.5 man,15,1.5,40,17.5 woman,20,1.6,50,21.5 man,28,1.78,75,24.0 woman,24,1.62,55,21.0 man,17,1.67,58,20.0 woman,18,1.58,48,18.5
名字叫做bmi.csv
训练数据
# 导入所需库 from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import accuracy_score # 加载BMI数据 data = pd.read_csv('bmi.csv') # 划分特征和标签 X = data[['height', 'weight']] y = data['bmi'] # 划分训练集和测试集 X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # 数据标准化 scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) # 使用KNN分类器训练模型 clf = KNeighborsClassifier(n_neighbors=3) clf.fit(X_train, y_train) # 预测测试集 y_pred = clf.predict(X_test) # 计算准确率 accuracy = accuracy_score(y_test, y_pred) print('Accuracy:', accuracy) # 保存模型 import joblib joblib.dump(clf, 'clf.pkl')
clf.pkl
训练分别程序
上次我的那个爬虫文章审核因为不明原因不通过所以:我没办法了,我就多加了几个gobots.txt
呜呼
csv文件为训练文件可以变为自己的训练数据。