import numpy as np from sklearn.datasets import load_iris from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error, r2_score from sklearn.model_selection import train_test_split import pandas as pd import matplotlib.pyplot as plt # Датасет Ирисы Фишера iris = load_iris() iris_pd=pd.DataFrame(data=np.c_[iris['data'], iris['target']], columns=iris['feature_names'] + ['target']) # Формирование выборок по признакам x1 = np.array(iris_pd['sepal length (cm)']) # первый признак - длина чашелистика x2 = np.array(iris_pd['sepal width (cm)']) # второй признак - ширина чашелистика x3 = np.array(iris_pd['petal length (cm)']) # третий признак - длина лепестка x4 = np.array(iris_pd['petal width (cm)']) # четвертый признак - ширина лепестка X = np.column_stack((x1, x2, x3)) # Разделение на обучающую и тестовую выборки X_train, X_test, y_train, y_test = train_test_split(X, x4, test_size=0.25, random_state=42) model = LinearRegression() model.fit(X_train, y_train) y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) rmse = np.sqrt(mse) r2 = r2_score(y_test, y_pred) print(f"Среднеквадратичное отклонение (СКO): {rmse:.2f}") print(f"Коэффициент детерминации (R²): {r2:.2f}") residuals = y_test - y_pred plt.scatter(y_pred, residuals) plt.axhline(y=0, color='red', linestyle='--') plt.xlabel('Предсказанные значения') plt.ylabel('Остаточные ошибки') plt.title('График остатков') plt.show()