“”"
@Time : 19-9-19 下午2:50
@Author : lei
@Site :
@File : dermatology皮肤病.py
@Software: PyCharm
“”"
import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.feature_selection import VarianceThreshold
from sklearn.model_selection import GridSearchCV
from sklearn.neural_network import MLPClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.linear_model import SGDRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import cross_val_predict
from sklearn.metrics import confusion_matrix
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import roc_auc_score
class Dermatology(object):
# 初始化
def init(self, path, name):
self.data = pd.read_csv(path, names=name)
def deal_with_data(self):
# self.data.replace(to_replace="?", value=np.nan)
# self.data.dropna()
self.data.drop("Age(linear)", axis=1, inplace=True)
# var = VarianceThreshold(threshold=0.1)
# self.data = var.fit_transform(self.data)
x_train, x_test, y_train, y_test = train_test_split(self.data.iloc[:, 0:33], self.data.iloc[:, 33], test_size=0.25)
rf = RandomForestClassifier()
gs = GridSearchCV(rf, param_grid={"max_depth": [5, 8, 10], "n_estimators": [50, 80, 100, 120]}, n_jobs=5, cv=5)
gs.fit(x_train, y_train)
y_predict = gs.predict(x_test)
score = gs.score(x_test, y_test)
print("随机森林:", score)
print(gs.best_estimator_)
# print(classification_report(y_test, y_predict, labels=[1, 2, 3, 4, 5, 6], target_names=["psoriasis", "seboreic dermatitis", "lichen planus", "pityriasis rosea", "cronic dermatitis", "pityriasis rubra pilaris"]))
def logis_regression(self):
x_train, x_test, y_train, y_test = train_test_split(self.data.iloc[:, 0:33], self.data.iloc[:, 33], test_size=0.25)
mm = MinMaxScaler()
x_train = mm.fit_transform(x_train)
x_test = mm.transform(x_test)
lg = LogisticRegression(solver="sag")
# 交叉验证和栅格搜索
gs = GridSearchCV(lg, param_grid={"multi_class": ['ovr', 'multinomial'], "max_iter": [200, 500, 1000]}, cv=5)
gs.fit(x_train, y_train)
y_predict = gs.predict(x_test)
y_score = gs.score(x_test, y_test)
print("逻辑回归准确率为:", y_score)
print("最佳参数为:", gs.best_estimator_)
# print()
def neutral_network(self):
x_train, x_test, y_train, y_test = train_test_split(self.data.iloc[:, 0:33], self.data.iloc[:, 33], test_size=0.25)
mm = MinMaxScaler()
x_train = mm.fit_transform(x_train)
x_test = mm.transform(x_test)
mlp = MLPClassifier(solver="sgd")
gs = GridSearchCV(mlp, param_grid={"hidden_layer_sizes": [(32, 8, 4), (32, 25, 5), (25, 15, 5)], "max_iter": [2000, 3000, 4000]}, cv=5)
gs.fit(x_train, y_train)
y_predict = gs.predict(x_test)
y_score = gs.score(x_test, y_test)
print("神经网络预测结果:", y_score)
print("最佳模型为:", gs.best_estimator_)
# 主逻辑类
def run(self):
# print(self.data.head(1))
self.deal_with_data()
self.logis_regression()
self.neutral_network()
# print(self.data)
sgd = SGDRegressor()
def main():
path = ‘./dermatology.csv’
name = [“erythema”,
“scaling”,
“definite borders”,
“itchin”,
“koebner phenomenon”,
“polygonal papules”,
“follicular papules”,
“oral mucosal involvement”,
“knee and elbow involvement”,
“scalp involvement”,
“family history, (0 or 1)”,
“melanin incontinence”,
“eosinophils in the infiltrate”,
“PNL infiltrate”,
“fibrosis of the papillary dermis”,
“exocytosis”,
“acanthosis”,
“hyperkeratosis”,
“parakeratosis”,
“clubbing of the rete ridges”,
“elongation of the rete ridges”,
“thinning of the suprapapillary epidermis”,
“spongiform pustule”,
“munro microabcess”,
“focal hypergranulosis”,
“disappearance of the granular layer”,
“vacuolisation and damage of basal layer”,
“spongiosis”,
“saw-tooth appearance of retes”,
“follicular horn plug”,
“perifollicular parakeratosis”,
“inflammatory monoluclear inflitrate”,
“band-like infiltrate”,
“Age(linear)”,
“class”]
drug = Dermatology(path, name)
drug.run()
if name == “main”:
main()