lal
This commit is contained in:
parent
9644582307
commit
481361b7e0
87
zavrazhnova_svetlana_lab_6/fraud_dataset.csv
Normal file
87
zavrazhnova_svetlana_lab_6/fraud_dataset.csv
Normal file
@ -0,0 +1,87 @@
|
||||
transaction_id,transaction_amount,location,merchant,age,gender,fraud_label
|
||||
1,1000.00,New York,ABC Corp,35,M,0
|
||||
2,500.00,Chicago,XYZ Inc,45,F,0
|
||||
3,2000.00,Los Angeles,ABC Corp,28,M,1
|
||||
4,1500.00,San Francisco,XYZ Inc,30,F,0
|
||||
5,800.00,Chicago,ABC Corp,50,F,0
|
||||
6,3000.00,New York,XYZ Inc,42,M,1
|
||||
7,1200.00,San Francisco,ABC Corp,55,F,0
|
||||
8,900.00,Los Angeles,XYZ Inc,37,M,0
|
||||
9,2500.00,Chicago,ABC Corp,33,F,1
|
||||
10,1800.00,New York,XYZ Inc,48,M,0
|
||||
11,750.00,San Francisco,ABC Corp,29,F,0
|
||||
12,2200.00,Chicago,XYZ Inc,51,M,0
|
||||
13,900.00,New York,ABC Corp,40,F,0
|
||||
14,1600.00,Los Angeles,XYZ Inc,26,M,0
|
||||
15,3000.00,San Francisco,ABC Corp,45,F,1
|
||||
16,1200.00,Chicago,XYZ Inc,34,M,0
|
||||
17,800.00,New York,ABC Corp,47,F,0
|
||||
18,1900.00,Los Angeles,XYZ Inc,32,M,0
|
||||
19,1100.00,San Francisco,ABC Corp,52,F,0
|
||||
20,4000.00,Chicago,XYZ Inc,38,M,1
|
||||
21,900.00,New York,ABC Corp,31,F,0
|
||||
22,1700.00,Los Angeles,XYZ Inc,49,M,0
|
||||
23,1000.00,San Francisco,ABC Corp,36,F,0
|
||||
24,2300.00,Chicago,XYZ Inc,27,M,1
|
||||
25,950.00,New York,ABC Corp,41,F,0
|
||||
26,1400.00,Los Angeles,XYZ Inc,54,M,0
|
||||
27,2800.00,San Francisco,ABC Corp,39,F,1
|
||||
28,1100.00,Chicago,XYZ Inc,44,M,0
|
||||
29,750.00,New York,ABC Corp,30,F,0
|
||||
30,2000.00,Los Angeles,XYZ Inc,46,M,0
|
||||
31,1250.00,San Francisco,ABC Corp,35,F,0
|
||||
32,2100.00,Chicago,XYZ Inc,43,M,0
|
||||
33,950.00,New York,ABC Corp,56,F,0
|
||||
34,1800.00,Los Angeles,XYZ Inc,29,M,0
|
||||
35,3200.00,San Francisco,ABC Corp,48,F,1
|
||||
36,1300.00,Chicago,XYZ Inc,37,M,0
|
||||
37,900.00,New York,ABC Corp,51,F,0
|
||||
38,2000.00,Los Angeles,XYZ Inc,33,M,0
|
||||
39,1050.00,San Francisco,ABC Corp,42,F,0
|
||||
40,2400.00,Chicago,XYZ Inc,26,M,0
|
||||
41,800.00,New York,ABC Corp,45,F,0
|
||||
42,1500.00,Los Angeles,XYZ Inc,31,M,0
|
||||
43,2800.00,San Francisco,ABC Corp,50,F,1
|
||||
44,1350.00,Chicago,XYZ Inc,28,M,0
|
||||
45,920.00,New York,ABC Corp,47,F,0
|
||||
46,2000.00,Los Angeles,XYZ Inc,36,M,0
|
||||
47,1125.00,San Francisco,ABC Corp,52,F,0
|
||||
48,1900.00,Chicago,XYZ Inc,38,M,1
|
||||
49,850.00,New York,ABC Corp,32,F,0
|
||||
50,1750.00,Los Angeles,XYZ Inc,49,M,0
|
||||
51,950.00,San Francisco,ABC Corp,27,F,0
|
||||
52,2300.00,Chicago,XYZ Inc,41,M,0
|
||||
53,850.00,New York,ABC Corp,54,F,0
|
||||
54,1600.00,Los Angeles,XYZ Inc,39,M,0
|
||||
55,3000.00,San Francisco,ABC Corp,46,F,1
|
||||
56,1250.00,Chicago,XYZ Inc,35,M,0
|
||||
57,800.00,New York,ABC Corp,56,F,0
|
||||
58,2200.00,Los Angeles,XYZ Inc,29,M,0
|
||||
59,1050.00,San Francisco,ABC Corp,48,F,0
|
||||
60,4000.00,Chicago,XYZ Inc,37,M,1
|
||||
61,950.00,New York,ABC Corp,30,F,0
|
||||
62,1700.00,Los Angeles,XYZ Inc,49,M,0
|
||||
63,1000.00,San Francisco,ABC Corp,36,F,0
|
||||
64,2800.00,Chicago,XYZ Inc,27,M,1
|
||||
65,900.00,New York,ABC Corp,41,F,0
|
||||
66,1400.00,Los Angeles,XYZ Inc,54,M,0
|
||||
67,3200.00,San Francisco,ABC Corp,39,F,1
|
||||
68,1100.00,Chicago,XYZ Inc,44,M,0
|
||||
69,750.00,New York,ABC Corp,30,F,0
|
||||
70,2000.00,Los Angeles,XYZ Inc,46,M,0
|
||||
71,1250.00,San Francisco,ABC Corp,35,F,0
|
||||
72,2100.00,Chicago,XYZ Inc,43,M,0
|
||||
73,950.00,New York,ABC Corp,56,F,0
|
||||
74,1800.00,Los Angeles,XYZ Inc,29,M,0
|
||||
75,3200.00,San Francisco,ABC Corp,48,F,1
|
||||
76,1300.00,Chicago,XYZ Inc,37,M,0
|
||||
77,900.00,New York,ABC Corp,51,F,0
|
||||
78,2000.00,Los Angeles,XYZ Inc,33,M,0
|
||||
79,1050.00,San Francisco,ABC Corp,42,F,0
|
||||
80,2400.00,Chicago,XYZ Inc,26,M,0
|
||||
81,800.00,New York,ABC Corp,45,F,0
|
||||
82,1500.00,Los Angeles,XYZ Inc,31,M,0
|
||||
83,2800.00,San Francisco,ABC Corp,50,F,1
|
||||
84,1350.00,Chicago,XYZ Inc,28,M,0
|
||||
85,920.00,New York,ABC Corp,47,F,0
|
||||
86,2000.00,Los Angeles,XYZ Inc,36,M,0
|
|
84
zavrazhnova_svetlana_lab_6/zavrazhnova_svetlana_lab_6.py
Normal file
84
zavrazhnova_svetlana_lab_6/zavrazhnova_svetlana_lab_6.py
Normal file
@ -0,0 +1,84 @@
|
||||
# import pandas as pd
|
||||
# from sklearn.neural_network import MLPClassifier
|
||||
# from sklearn.preprocessing import OneHotEncoder
|
||||
#
|
||||
# # Загрузка данных из файла
|
||||
# df = pd.read_csv('fraud_dataset.csv')
|
||||
#
|
||||
# # Разделение признаков и целевой переменной
|
||||
# # В данном случае целевая переменная - fraud_label
|
||||
# X = df[['transaction_amount', 'location', 'merchant', 'age', 'gender']]
|
||||
# y = df['fraud_label']
|
||||
#
|
||||
# X[['location', 'merchant', 'gender']] = X[['location', 'merchant', 'gender']].astype(str)
|
||||
#
|
||||
# # Преобразование категориальных признаков в числовые
|
||||
# enc = OneHotEncoder()
|
||||
# X_encoded = enc.fit_transform(X[['location', 'merchant', 'gender']]).toarray()
|
||||
#
|
||||
# # Объединение закодированных признаков с числовыми признаками
|
||||
# X_final = pd.concat([X['transaction_amount'], pd.DataFrame(X_encoded), X['age']], axis=1)
|
||||
#
|
||||
# # Создание модели MLPClassifier
|
||||
# model = MLPClassifier(hidden_layer_sizes=(100, 100)) # Можно настроить размеры и количество скрытых слоев
|
||||
#
|
||||
# # Обучение модели
|
||||
# model.fit(X_final, y)
|
||||
#
|
||||
# # Предсказание для новых данных
|
||||
# # Пример предсказания для новой транзакции
|
||||
# new_transaction = pd.DataFrame([[2000.00, 'New York', 'ABC Corp', 35, 'M']],
|
||||
# columns=['transaction_amount', 'location', 'merchant', 'age', 'gender'])
|
||||
# new_transaction_encoded = enc.transform(new_transaction[['location', 'merchant', 'gender']]).toarray()
|
||||
# new_transaction_final = pd.concat([new_transaction['transaction_amount'],
|
||||
# pd.DataFrame(new_transaction_encoded),
|
||||
# new_transaction['age']], axis=1)
|
||||
# prediction = model.predict(new_transaction_final)
|
||||
#
|
||||
# # Вывод предсказания
|
||||
# print(prediction)
|
||||
|
||||
import pandas as pd
|
||||
import numpy as np
|
||||
from matplotlib import pyplot as plt
|
||||
from sklearn import metrics
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.neural_network import MLPRegressor
|
||||
from sklearn.preprocessing import LabelEncoder
|
||||
|
||||
# Метод обучения нейронной сети
|
||||
def reg_neural_net():
|
||||
df = pd.read_csv('fraud_dataset.csv')
|
||||
x, y = [df.drop("fraud_label", axis=1).values,
|
||||
df["fraud_label"].values]
|
||||
encoder = LabelEncoder()
|
||||
df['location'] = encoder.fit_transform(df['location']) # Преобразование категориального столбца "location" в числовой формат
|
||||
|
||||
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.001, random_state=42)
|
||||
|
||||
mlp = MLPRegressor(hidden_layer_sizes=(100, 50), activation='tanh', solver='adam', random_state=15000)
|
||||
mlp.fit(x_train, y_train)
|
||||
y_predict = mlp.predict(x_test)
|
||||
err = pred_errors(y_predict, y_test)
|
||||
make_plots(y_test, y_predict, err[0], err[1], "Нейронная сеть")
|
||||
|
||||
|
||||
# Метод рассчёта ошибок
|
||||
def pred_errors(y_predict, y_test):
|
||||
mid_square = np.round(np.sqrt(metrics.mean_squared_error(y_test, y_predict)),3) # Рассчёт среднеквадратичной ошибки модели
|
||||
det_kp = np.round(metrics.r2_score(y_test, y_predict), 2) # Рассчёт коэфициента детерминации модели
|
||||
return mid_square, det_kp
|
||||
|
||||
|
||||
# Метод отрисовки графиков
|
||||
def make_plots(y_test, y_predict, mid_sqrt, det_kp, title):
|
||||
plt.plot(y_test, c="red", label="\"y\" исходная") # Создание графика исходной функции
|
||||
plt.plot(y_predict, c="green", label="\"y\" предсказанная \n"
|
||||
"Ср^2 = " + str(mid_sqrt) + "\n"
|
||||
"Кд = " + str(det_kp)) # Создание графика предсказанной функции
|
||||
plt.legend(loc='lower left')
|
||||
plt.title(title)
|
||||
plt.savefig('static/' + title + '.png')
|
||||
plt.close()
|
||||
|
||||
reg_neural_net()
|
Loading…
Reference in New Issue
Block a user