import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn import metrics #定义评估回归性能函数 def evaluation_metrics() : print("Mean Absolute Error:",metrics.mean_absolute_error(y_test,y_test_pred)) print("Mean Squared Error:",metrics.mean_squared_error(y_test,y_test_pred)) print("Root Mean Squared Error:",np.sqrt(metrics.mean_squared_error(y_test,y_test_pred))) print("r2_score : ",str(metrics.r2_score(y_test,y_test_pred))) #定义绘制Expermental-Predicted图像的函数 def graph(model) : plt.grid(True) plt.plot([1,12],[1,12],color='darkgrey',lw=3) plt.scatter(y_train,y_train_pred,color='c') plt.scatter(y_test,y_test_pred,color='salmon') plt.xlabel("Expermental Values",fontsize=14,fontweight='bold') plt.ylabel("Predicted Values",fontsize=14,fontweight='bold') plt.xlim(1,12) plt.ylim(1,12) plt.title(str(model),fontweight='bold') plt.savefig(str(model)+ "_test.tif",dpi=500) plt.show()
时间: 2024-04-05 12:33:27 浏览: 62
这段代码定义了两个函数,分别是evaluation_metrics和graph。
evaluation_metrics函数用于评估回归性能,其中使用了sklearn库中的mean_absolute_error、mean_squared_error、r2_score等函数来计算预测值与实际值之间的差距,并输出结果。
graph函数用于绘制Expermental-Predicted图像,其中使用了matplotlib库来绘制散点图和参考直线。该函数接受一个参数model,用于指定模型的名称,绘制完成后将图像保存为tif格式并显示出来。
相关问题
import pandas as pd import numpy as np import matplotlib.pyplot as plt from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler from sklearn.linear_model import LogisticRegression from sklearn.tree import DecisionTreeClassifier from sklearn.neighbors import KNeighborsClassifier from sklearn.svm import SVC from sklearn.metrics import classification_report from sklearn.metrics import confusion_matrix
这段代码是在 Python 中导入了一些常用的机器学习库和模块,包括 pandas、numpy、matplotlib、sklearn 等。其中:
- pandas 是 Python 中常用的数据分析库,可以用来读取和处理数据;
- numpy 是 Python 中常用的科学计算库,提供了数组、矩阵等数据结构和各种数学函数;
- matplotlib 是 Python 中常用的数据可视化库,可以用来绘制各种图表;
- sklearn 是 Python 中常用的机器学习库,提供了许多常用的机器学习算法和工具,比如数据预处理、模型选择、模型评估等。
这段代码中还导入了不同的机器学习算法,包括逻辑回归、决策树、K近邻和支持向量机等。最后还导入了一些评估指标,比如分类报告和混淆矩阵。
优化这段代码# import modules 导入模块 from sklearn.model_selection import train_test_split from sklearn.decomposition import PCA import pandas as pd from sklearn import svm import numpy as np import math import matplotlib.pyplot as plt import matplotlib as mpl from matplotlib import colors from sklearn.model_selection import train_test_split from sklearn import datasets from matplotlib.colors import ListedColormap from sklearn.svm import SVC from sklearn.preprocessing import StandardScaler from sklearn.model_selection import StratifiedShuffleSplit,StratifiedKFold from sklearn.model_selection import GridSearchCV from sklearn.model_selection import GridSearchCV, LeaveOneOut, cross_val_predict from sklearn.model_selection import KFold from sklearn.linear_model import LogisticRegression from sklearn.naive_bayes import GaussianNB from sklearn.neighbors import KNeighborsClassifier from sklearn import svm from sklearn.tree import DecisionTreeClassifier from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import roc_auc_score from sklearn.metrics import roc_auc_score import math import datetime import multiprocessing as mp from sklearn.ensemble import StackingClassifier from sklearn.pipeline import make_pipeline from sklearn.svm import LinearSVC import random
你可以对这段代码进行如下优化:
```python
import pandas as pd
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib import colors
from matplotlib.colors import ListedColormap
from sklearn.model_selection import train_test_split, StratifiedShuffleSplit, StratifiedKFold, GridSearchCV, LeaveOneOut, cross_val_predict, KFold
from sklearn.decomposition import PCA
from sklearn.svm import SVC, LinearSVC
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, StackingClassifier
from sklearn.metrics import roc_auc_score
# 导入模块
# 设置随机种子
random.seed(42)
np.random.seed(42)
# 其他代码...
```
这样做的优点是将导入的模块进行整理和分类,提高了代码的可读性。同时,设置了随机种子,保证了代码的可复现性。你可以根据需要添加其他的代码或模块。
阅读全文