IIS_2023_1/lipatov_ilya_lab_6/lab6.py
2023-12-01 21:29:29 +04:00

35 lines
945 B
Python

from sklearn.metrics import mean_absolute_percentage_error
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
import pandas as pd
def MEDV_to_number(MEDV):
if MEDV < (minMEDV + meanMEDV) / 2:
return 1
if MEDV < (maxMEDV + meanMEDV) / 2:
return 2
return 3
data = pd.read_csv('boston.csv')
minMEDV = data['MEDV'].min()
maxMEDV = data['MEDV'].max()
meanMEDV = data['MEDV'].mean()
data['MEDV'] = data['MEDV'].apply(MEDV_to_number)
X = (data[['RM', 'LSTAT', 'CRIM']])
y = data['MEDV']
X_train, X_test, Y_train, Y_test = train_test_split(X, y, test_size=0.1, random_state=42)
clf = MLPClassifier(hidden_layer_sizes=(100, 100), activation='relu', solver='lbfgs', max_iter=1000)
clf.fit(X_train, Y_train)
y_predict = clf.predict(X_test)
score = clf.score(X_test, Y_test)
print(f'Score: {score}')
print(f'MAPE: {mean_absolute_percentage_error(Y_test, y_predict)}')