import pandas as pd import numpy as np import matplotlib.pyplot as plt import sklearn.model_selection as sM_S import sklearn.preprocessing as sP import sklearn.tree as sT import sklearn.metrics as sM import sklearn.naive_bayes as sNB #1读取数据 data_train = pd.read_excel("train data.xlsx") # 1.1剔除无关列 data = data_train.iloc[:,3:-1] #2预处理 data = data.dropna() #3.数据预处理:空值处理,值映射(分段),归一化/标准化 X = data.iloc[:,0:-1] y = data.iloc[:,-1] mms = sP.MinMaxScaler() X = mms.fit_transform(X) #4.分割数据集和测试集 x_train,x_text,y_train,y_text = sM_S.train_test_split(X,y,test_size=0.33,random_state=42) #5.选择模型 model = sT.DecisionTreeClassifier(max_depth=7) #6.训练模型 model.fit(x_train,y_train) #7.评价模型,赛事要求用F1 y_predict = model.predict(x_text) score = sM.f1_score(y_predict,y_text,average="macro") print("预处理:{} 模型:{} 参数:{} 得分:{}".format("均值填充处理_归一化","决策树","max_depth=7",score)) # 8.应用模型--预测 trainData = pd.read_excel("train data.xlsx") trainData = pd.DataFrame(trainData) trainData = trainData.iloc[:, 3:-2] trainData = trainData.dropna() # 删除空值行 val_data = trainData val_data = mms.fit_transform(val_data) print("预测 train data.xlsx 的结果为:", model.predict(val_data)) 以上这段代码能运行吗?
时间: 2024-02-16 11:05:32 浏览: 78
使用pandas的box_plot去除异常值
这段代码存在语法错误,主要是因为第一行导入多个库时没有使用分号或换行符分割。以下是修正后的代码:
```python
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sklearn.model_selection as sM_S
import sklearn.preprocessing as sP
import sklearn.tree as sT
import sklearn.metrics as sM
import sklearn.naive_bayes as sNB
# 1.读取数据
data_train = pd.read_excel("train data.xlsx")
# 1.1剔除无关列
data = data_train.iloc[:, 3:-1]
# 2.预处理
data = data.dropna()
# 3.数据预处理:空值处理,值映射(分段),归一化/标准化
X = data.iloc[:, 0:-1]
y = data.iloc[:, -1]
mms = sP.MinMaxScaler()
X = mms.fit_transform(X)
# 4.分割数据集和测试集
x_train, x_test, y_train, y_test = sM_S.train_test_split(X, y, test_size=0.33, random_state=42)
# 5.选择模型
model = sT.DecisionTreeClassifier(max_depth=7)
# 6.训练模型
model.fit(x_train, y_train)
# 7.评价模型,赛事要求用F1
y_predict = model.predict(x_test)
score = sM.f1_score(y_predict, y_test, average="macro")
print("预处理:{} 模型:{} 参数:{} 得分:{}".format("均值填充处理_归一化", "决策树", "max_depth=7", score))
# 8.应用模型--预测
trainData = pd.read_excel("train data.xlsx")
trainData = pd.DataFrame(trainData)
trainData = trainData.iloc[:, 3:-2]
trainData = trainData.dropna() # 删除空值行
val_data = trainData
val_data = mms.fit_transform(val_data)
print("预测 train data.xlsx 的结果为:", model.predict(val_data))
```
请注意,这段代码的运行还需要保证 train data.xlsx 文件存在,并且其中的数据格式与代码中的数据格式一致。
阅读全文