IIS_2023_1/madyshev_egor_lab_6/main.py

50 lines
1.8 KiB
Python

import numpy as np
import pandas as pb
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, Perceptron, LogisticRegression, Lasso, Ridge
from sklearn.neural_network import MLPClassifier, MLPRegressor
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, MinMaxScaler
from sklearn.tree import DecisionTreeRegressor, DecisionTreeClassifier
from sklearn.preprocessing import PolynomialFeatures
df = pb.read_csv("StudentsPerformance.csv", sep=",", encoding="windows-1251")
df1 = df
print("Данные без подготовки:")
with pb.option_context('display.max_rows', None, 'display.max_columns', None, 'display.width', 1000):
print(df[:5])
def prepareStringData(columnName):
uniq = df[columnName].unique()
mp = {}
for i in uniq:
mp[i] = len(mp)
df[columnName] = df[columnName].map(mp)
print()
print("Данные после подготовки:")
prepareStringData("gender")
prepareStringData("race/ethnicity")
prepareStringData("parental level of education")
prepareStringData("lunch")
prepareStringData("test preparation course")
with pb.option_context('display.max_rows', None, 'display.max_columns', None, 'display.width', 1000):
print(df[:5])
X = df[["gender", "race/ethnicity", "lunch", "parental level of education", "reading score", "writing score", "math score"]]
y = df["test preparation course"]
X_train, X_Test, y_train, y_test = train_test_split(X, y, test_size=0.26, random_state=42)
mlpr = MLPRegressor()
mlpc = MLPClassifier()
mlpr.fit(X_train, y_train)
mlpc.fit(X_train, y_train)
print("MLPRegressor:", mlpr.score(X_Test, y_test))
print("MLPClassifier:", mlpc.score(X_Test, y_test))