def Land_cover_pred_plot(array_folder,raster_file, reference_file,ML_algo, plot = False): df_train , train_array = get_data_eval(array_folder,raster_file, reference_file) df_train = df_train.dropna() print(df_train) train_array = np.array(train_array, dtype=object) tile_df = pd.DataFrame() for i, array in enumerate(train_array[0]): # print(train_array[i], train_array_name[i]) tile_df[train_array[1][i]] = np.nan_to_num(array.ravel(), copy=False) # print(train_array[0][i], train_array[1][i]) X_train, X_test, y_train, y_test = train_test_split(df_train.drop('type' , axis = 1),df_train['type'],test_size = 0.1) print(X_train) ML_algo.fit(X_train,y_train) test_pred = ML_algo.predict(X_test) confusion_mat = confusion_matrix(y_test,test_pred) classification_repo = classification_report(y_test, test_pred) test_acc = accuracy_score(y_test, test_pred) print("Confusion Matri : \n", confusion_mat) print("Classification Report : \n", classification_repo) print("Accuracy on Test : ", test_acc) pred_array = ML_algo.predict(tile_df) mask_array = np.reshape(pred_array, train_array[0][0].shape) class_sum = [] for i,j in enumerate(df_train['type'].unique()): sum = (mask_array == j).sum() class_sum.append([j,sum]) print(class_sum) print(mask_array) if plot == True: arr_f = np.array(mask_array, dtype = float) arr_f = np.rot90(arr_f, axes=(-2,-1)) arr_f = np.flip(arr_f,0) plt.imshow(arr_f) plt.colorbar() return mask_array
时间: 2023-06-18 18:02:09 浏览: 58
该函数是一个用于地表覆盖预测和绘图的函数。它需要一个包含训练数据的文件夹路径,一个栅格文件和一个参考文件作为输入。它还需要一个机器学习算法和一个布尔值作为是否要绘制图表的标志。函数调用 get_data_eval 函数来获取训练数据,并使用 train_test_split 函数将其分成训练集和测试集。然后,使用机器学习算法来拟合训练数据,预测测试数据,并计算准确度、混淆矩阵和分类报告。最后,使用训练后的模型来预测栅格文件中的地表覆盖,并将结果绘制成图表(如果 plot 参数为 True)。函数返回预测结果的数组。
相关问题
def median_absolute_percentage_error(y_true,y_pred):
该函数计算真实值和预测值之间的中位绝对百分比误差(Median Absolute Percentage Error,简称MAPE)。
以下是该函数的Python代码实现:
```
import numpy as np
def median_absolute_percentage_error(y_true, y_pred):
"""
Computes median absolute percentage error between two arrays.
"""
y_true = np.array(y_true)
y_pred = np.array(y_pred)
return np.median(np.abs((y_true - y_pred) / y_true)) * 100
```
该函数首先将真实值和预测值转换为numpy数组,然后计算它们之间的绝对误差,并将其除以真实值。最后,它返回绝对误差的中位数乘以100,即中位绝对百分比误差。
loss = log_loss(y_test, y_pred)的底层代码是什么
`log_loss`函数的底层代码可以在sklearn的源代码中找到,具体实现在`metrics.py`文件中。下面是其中的关键部分:
```python
def log_loss(y_true, y_pred, eps=1e-15, normalize=True, sample_weight=None,
labels=None):
# 省略部分代码
if len(y_pred.shape) == 1:
# 将预测结果转化为二维数组
y_pred = y_pred.reshape(-1, 1)
if y_pred.shape[1] == 1:
# 将预测结果转化为概率值
y_pred = np.hstack([1 - y_pred, y_pred])
# 将真实标签转化为二进制编码
lb = LabelBinarizer()
lb.fit(y_true)
if len(lb.classes_) == 1:
lb.classes_ = np.array([0, 1])
y_true = lb.transform(y_true)
# 计算对数损失函数的值
loss = -(y_true * np.log(y_pred + eps)).sum(axis=1)
# 省略部分代码
return loss
```
这段代码首先将预测结果转化为概率值,然后将真实标签转化为二进制编码(例如,如果标签有3个类别,那么就将每个标签转化为一个长度为3的二进制向量,其中只有一个元素为1,其它元素为0),最后计算对数损失函数的值。