58 lines
2.2 KiB
Python
58 lines
2.2 KiB
Python
|
import pandas as pd
|
||
|
import matplotlib.pyplot as plt
|
||
|
from sklearn.model_selection import train_test_split
|
||
|
from sklearn.neural_network import MLPClassifier
|
||
|
from sklearn.preprocessing import StandardScaler
|
||
|
from sklearn.metrics import accuracy_score
|
||
|
import seaborn as sns
|
||
|
|
||
|
# Загрузка датасета
|
||
|
data = pd.read_csv('train_bikes.csv', sep=',').dropna()
|
||
|
|
||
|
data['is_holiday'] = data['holiday'].apply(lambda x: 1 if x == 1 else 0)
|
||
|
data['is_workingday'] = data['workingday'].apply(lambda x: 1 if x == 1 else 0)
|
||
|
|
||
|
# Выбор признаков
|
||
|
features = ['weather', 'temp', 'humidity', 'is_holiday', 'is_workingday']
|
||
|
target = 'count'
|
||
|
|
||
|
X = data[features]
|
||
|
y = data[target]
|
||
|
|
||
|
# Разбиваем на тестовую и тренировочную выборки
|
||
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
||
|
|
||
|
# Стандартизация признаков
|
||
|
scaler = StandardScaler()
|
||
|
X_train = scaler.fit_transform(X_train)
|
||
|
X_test = scaler.transform(X_test)
|
||
|
|
||
|
# Создание и обучение нейронной сети MLPClassifier
|
||
|
model = MLPClassifier(
|
||
|
hidden_layer_sizes=(500,),
|
||
|
activation='relu',
|
||
|
solver='adam',
|
||
|
alpha=0.01,
|
||
|
max_iter=100,
|
||
|
random_state=42,
|
||
|
verbose=True,
|
||
|
)
|
||
|
model.fit(X_train, y_train)
|
||
|
|
||
|
# Предсказание значений
|
||
|
y_pred = model.predict(X_test)
|
||
|
|
||
|
# Оценка модели
|
||
|
accuracy = accuracy_score(y_test, y_pred)
|
||
|
print(f'Оценка точности: {accuracy*100:.2f}%')
|
||
|
|
||
|
# Визуализация результатов (гистограмма)
|
||
|
plt.figure(figsize=(10, 6))
|
||
|
sns.scatterplot(x=y_test, y=y_pred, hue=X_test[:, 0], palette='viridis', alpha=0.7, s=80)
|
||
|
plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], linestyle='--', color='gray', linewidth=2)
|
||
|
plt.xlabel('Фактическое количество арендованных велосипедов')
|
||
|
plt.ylabel('Предсказанное количество арендованных велосипедов')
|
||
|
plt.title('Фактическое vs Предсказанное количество арендованных велосипедов')
|
||
|
plt.legend(title='Погодные условия', loc='upper left')
|
||
|
plt.show()
|