-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN.py
82 lines (64 loc) · 2.6 KB
/
CNN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# -*- coding: utf-8 -*-
"""
Created on Sun May 21 01:01:49 2023
@author: Administrator
"""
# Importing required libraries
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv1D, Flatten, MaxPooling1D, Dense
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
from statsmodels.stats.stattools import durbin_watson
# Importing the dataset
dataset = pd.read_csv(r'D:\Extra\Sarmat\Dataset.csv')
# Replace NaN values with column means
dataset = dataset.fillna(dataset.mean())
# Define radius and mass
radius = 18.5
mass = 3.281
# Create the new column "force" based on the calculation
dataset['Force'] = mass * radius * dataset['Measured_RPM']
# Save the updated dataset to a new CSV file
dataset.to_csv(r'D:\Extra\Sarmat\0D.csv', index=False)
# Separating the features and labels
features = dataset.iloc[:, :-1].values
labels = dataset.iloc[:, -1].values
# Splitting the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.20, random_state=0)
# Scaling the numerical features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Reshape the input data for CNN
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
model = Sequential()
model.add(Conv1D(filters=32, kernel_size=3, activation='relu', input_shape=(X_train.shape[1], 1)))
model.add(MaxPooling1D(pool_size=2))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(1)) # Output layer with 1 neuron for regression
print(model.summary())
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error')
# Train the model
history = model.fit(X_train, y_train, batch_size=100, epochs=100, validation_data=(X_test, y_test))
# Evaluate the model
y_pred = model.predict(X_test)
# Calculate evaluation metrics
mae = mean_absolute_error(y_test, y_pred)
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
dw = durbin_watson(y_test - y_pred)
# Print the evaluation metrics
print("Mean Absolute Error:", mae)
print("Mean Squared Error:", mse)
print("R2 Score:", r2)
print("Durbin-Watson:", dw)