<jupyter_start><jupyter_text>Import modules<jupyter_code>import numpy as np 
import pandas as pd 
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from keras.models import Sequential
from keras.layers import Dense<jupyter_output><empty_output><jupyter_text>Lấy dataset<jupyter_code>df=pd.read_csv("../data/Admission_Predict_Ver1.1.csv")
df=df.drop("Serial No.",axis=1)
df.head(10)
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 500 entries, 0 to 499
Data columns (total 8 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   GRE Score          500 non-null    int64  
 1   TOEFL Score        500 non-null    int64  
 2   University Rating  500 non-null    int64  
 3   SOP                500 non-null    float64
 4   LOR                500 non-null    float64
 5   CGPA               500 non-null    float64
 6   Research           500 non-null    int64  
 7   Chance of Admit    500 non-null    float64
dtypes: float64(4), int64(4)
memory usage: 31.4 KB<jupyter_text>Hiển thị dataset<jupyter_code>import seaborn as sns
corr = df.corr()
sns.heatmap(corr, 
            xticklabels=corr.columns.values,
            yticklabels=corr.columns.values)<jupyter_output><empty_output><jupyter_text>Tạo model![image-2.png](attachment:image-2.png)<jupyter_code>model = Sequential(name = 'Regression')
model.add(Dense(8, input_shape=(7,), activation='relu')) 
# Nếu input chỉ có 1 chiều thì bắt buộc phải dể trống giá trị thứ 2 của tuple (dimenson, )
# activation: 'relu', 'sigmoid', 'tanh', 'linear'.... xem thêm: https://faroit.com/keras-docs/2.0.2/layers/core/#activation
model.add(Dense(3, activation='relu'))
model.add(Dense(1, activation='relu'))
# Thiết lập thông số
model.compile(loss='mean_squared_error', optimizer='adam')
# compile: dùng để thiết lập hàm loss, optimizer
# loss: 'mean_squared_error', 'binary_crossentropy'... xem thêm: https://faroit.com/keras-docs/2.0.2/losses/
# optimizer: 'adam', 'sgd', 'rmsprop', 'adagrad'... xem thêm: https://faroit.com/keras-docs/2.0.2/optimizers/
model.summary()<jupyter_output>Model: "Regression"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_3 (Dense)              (None, 8)                 64        
_________________________________________________________________
dense_4 (Dense)              (None, 3)                 27        
_________________________________________________________________
dense_5 (Dense)              (None, 1)                 4         
=================================================================
Total params: 95
Trainable params: 95
Non-trainable params: 0
_________________________________________________________________<jupyter_text>Tách X, Y<jupyter_code>X=np.asarray(df.drop("Chance of Admit ",axis=1))
Y=np.asarray(df["Chance of Admit "])<jupyter_output><empty_output><jupyter_text>Train-test split, scale<jupyter_code>X_train, X_test, y_train, y_test = train_test_split(
     X,Y, test_size=0.2, random_state=1)
from sklearn.preprocessing import MinMaxScaler
scaler =  MinMaxScaler()
X_train=scaler.fit_transform(X_train)
X_test=scaler.fit_transform(X_test)<jupyter_output><empty_output><jupyter_text>Train<jupyter_code>history = model.fit(x=X_train, y=y_train, epochs=40, batch_size = 3) 
# Một Epoch được tính là khi đưa tất cả dữ liệu vào mạng neural network 1 lần.
# Batch - chia toàn bộ data thành nhiều gói dữ liệu nhỏ hơn để đưa vào mạng neuron. 
# Để hoành thành một epoch sẽ cần nhiều lần chạy (iteration) hơn<jupyter_output>Epoch 1/40
134/134 [==============================] - 1s 7ms/step - loss: 0.1191
Epoch 2/40
134/134 [==============================] - 1s 7ms/step - loss: 0.0344
Epoch 3/40
134/134 [==============================] - 1s 7ms/step - loss: 0.0225
Epoch 4/40
134/134 [==============================] - 1s 7ms/step - loss: 0.0147
Epoch 5/40
134/134 [==============================] - 1s 7ms/step - loss: 0.0102
Epoch 6/40
134/134 [==============================] - 1s 7ms/step - loss: 0.0076
Epoch 7/40
134/134 [==============================] - 1s 7ms/step - loss: 0.0064
Epoch 8/40
134/134 [==============================] - 1s 7ms/step - loss: 0.0057
Epoch 9/40
134/134 [==============================] - 1s 7ms/step - loss: 0.0052
Epoch 10/40
134/134 [==============================] - 1s 8ms/step - loss: 0.0049
Epoch 11/40
134/134 [==============================] - 1s 8ms/step - loss: 0.0048
Epoch 12/40
134/134 [==============================] - 1s 7ms/step - loss: 0.0046
Epoch 13/40
134/134 [====[...]<jupyter_text>Plot history<jupyter_code>plt.plot(history.history['loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>Đánh giá<jupyter_code>from sklearn.metrics import accuracy_score
prediction = model.predict(X_test)
plt.plot(y_test, color = 'green', label = 'Actual')
plt.plot(prediction, color = 'blue', label = 'Predicted')
plt.grid(alpha = 0.3)
plt.xlabel('Number of Candidate')
plt.ylabel('Score')
plt.title('Actual vs Predicted')
plt.legend()
plt.show()
from sklearn.metrics import r2_score
print("r_square score: ", r2_score(y_test,prediction))
# train_prediction = model.predict(X_train)
# print("r_square score (train dataset): ", r2_score(y_train,train_prediction))
# Tính loss:
model.evaluate(X_test, y_test)<jupyter_output>4/4 [==============================] - 0s 6ms/step - loss: 0.0038
{'fraction_non_alphanumeric': 0.21033931426541128, 'fraction_numerical': 0.05862497779356902, 'mean_word_length': 4.212962962962963, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '16395', 'license': 'MIT', 'n_tokens_mistral': 2246, 'n_tokens_neox': 1957, 'n_words': 428, 'path': 'Code/.ipynb_checkpoints/DNN - Regression-checkpoint.ipynb', 'repo_name': 'nguyenhuynhthien/DeepLearningFreeCource', 'size': '5629'}