import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.pipeline import make_pipeline
from sklearn.neural_network import MLPRegressor
from sklearn.svm import SVR
from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.wrappers.scikit_learn import KerasRegressor
from scikeras.wrappers import BaseWrapper
def load_and_process_data(data):
X = data[['MaxTemp','MinTemp','ApparentTemp','AvgWind', 'AvgHumidity', 'AvgPressure']]
y = data['Rain']
return X, y
def create_pipeline(model):
pipeline = make_pipeline(
StandardScaler(),
model
)
return pipeline
def plot_predictions(y_true, y_pred):
df = pd.DataFrame({'Gerçek Değerler': y_true, 'Tahmin Edilen Değerler': y_pred})
sns.set_style("whitegrid") # seaborn stilini kullan
fig, ax = plt.subplots(figsize=(12, 6))
df.plot(ax=ax)
ax.set_xlabel('Tarih')
ax.set_ylabel('Yağış Miktarı')
plt.show()
def mlp_regressor(X_train, y_train, X_test, y_test):
model = MLPRegressor(hidden_layer_sizes=(10, 10), activation='relu', solver='adam', max_iter=1000, random_state=42)
pipeline_mlp = create_pipeline(model)
pipeline_mlp.fit(X_train, y_train)
y_pred_mlp = pd.Series(pipeline_mlp.predict(X_test), index=y_test.index)
mse_mlp = mean_squared_error(y_test, y_pred_mlp)
print("MSE:", mse_mlp)
plot_predictions(y_test, y_pred_mlp)
def svm_regressor(X_train, y_train, X_test, y_test):
model = SVR(kernel='rbf', C=1, epsilon=0.1)
pipeline_svm = create_pipeline(model)
pipeline_svm.fit(X_train, y_train)
y_pred_svm = pd.Series(pipeline_svm.predict(X_test), index=y_test.index)
mse_svm = mean_squared_error(y_test, y_pred_svm)
print("MSE:", mse_svm)
plot_predictions(y_test, y_pred_svm)
def kalman_filter_regressor(X_train, y_train, X_test, y_test):
# Kalman Filtresi için gerekli matrisleri tanımla
dt = 1.0 / 5
F = np.array([[1, dt, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]])
H = np.array([1, 0, 0, 0, 0]).reshape(1, 5)
Q = np.array([[0.05, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.05, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.05, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.05, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.05]])
R = np.array([0.5]).reshape(1, 1)
x = np.zeros((5, 1))
P = np.eye(5)
def main():
X, y = load_and_process_data(data)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print("MLP Regressor Model")
mlp_regressor(X_train, y_train, X_test, y_test)
print("SVM Model")
svm_regressor(X_train, y_train, X_test, y_test)
if __name__ == '__main__':
main()
我不知道如何编辑 plot_predictions 函数。此外,当我使用值或其他东西时,较低的 mse 值会使图形变得更糟。所以它具有误导性。随着 mse 值的降低,我希望实际值和预测值在图表上重叠。 如果实际值和估计值之间有这样的差异,为什么mse会这么低?