使用自归一化神经网络识别HTRU1中的脉冲星的代码
时间: 2023-04-02 11:01:13 浏览: 165
以下是使用自归一化神经网络识别HTRU1中的脉冲星的代码:
```python
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import Adam
# 读取数据
data = pd.read_csv('HTRU_1.csv', header=None)
# 将数据分为特征和标签
X = data.iloc[:, :-1].values
y = data.iloc[:, -1].values
# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=.2, random_state=42)
# 构建模型
model = Sequential()
model.add(Dense(64, input_dim=X_train.shape[1], activation='relu'))
model.add(Dropout(.5))
model.add(Dense(32, activation='relu'))
model.add(Dropout(.5))
model.add(Dense(1, activation='sigmoid'))
# 编译模型
model.compile(loss='binary_crossentropy', optimizer=Adam(lr=.001), metrics=['accuracy'])
# 训练模型
model.fit(X_train, y_train, epochs=50, batch_size=32, validation_data=(X_test, y_test))
# 评估模型
loss, accuracy = model.evaluate(X_test, y_test)
print('Test loss:', loss)
print('Test accuracy:', accuracy)
```
希望能对您有所帮助。
阅读全文