-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_neural_network_project_1.py
53 lines (39 loc) · 1.66 KB
/
simple_neural_network_project_1.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
# -*- coding: utf-8 -*-
"""simple neural network project 1.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1CZ_UB0_kjyY50bNs3D2BSBqcS56xR3hg
"""
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from keras.models import Sequential
from keras.layers import Dense
# Load the dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-red.csv"
data = pd.read_csv(url, sep=';')
# Display the first few rows of the dataset
print(data.head())
# Check for missing values
print(data.isnull().sum())
# Split features and target variable
X = data.drop('quality', axis=1)
y = data['quality']
# Split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize the features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Now X_train_scaled and X_test_scaled contain the preprocessed features
model = Sequential()
model.add(Dense(64, input_shape=(X_train.shape[1],), activation='relu'))
model.add(Dense(32, activation='relu'))
model.add(Dense(1)) # For regression, as it's predicting wine quality (a regression task)
# Compile the model
model.compile(optimizer='adam', loss='mean_squared_error') # Using Mean Squared Error as the loss function for regression
# Train the model
model.fit(X_train_scaled, y_train, epochs=50, batch_size=32, validation_split=0.1)
# Evaluate the model on test data
loss = model.evaluate(X_test_scaled, y_test)
print("Test Loss:", loss)