Machine Learning & Deep Learning practice code.3

AI Engineering Course Log·2023년 7월 12일
0

road to AI Engineering

목록 보기
57/83
  • get the dataset
boston = pd.read_csv('boston_house.csv')
boston
  • check the first rows
boston.head()
  • check the statistic
boston.describe()
  • see the values of selected columns
boston['MEDV']
  • check the column's distribution using histplot()
sns.histplot(data=boston, x='MEDV')
  • visualize the graph using plot(scatter)
    kind='scatter', x='RM', y='MEDV'
boston.plot(kind='scatter', x='RM', y='MEDV')
  • print four columns' values
boston[['MEDV', 'RM', 'AGE', 'CHAS']]
  • identify relationships between columns
    plt.figure(figsize=(10,10))
sns.pairplot(boston[['MEDV', 'RM', 'AGE', 'CHAS']])
plt.show()
  • see the rows correlation
boston_corr = boston[['MEDV', 'RM', 'AGE', 'CHAs']].corr()
boston_corr
  • check the correlations between columns using seaborn's heatmap
    annot=True, cmap='Blues'
    plt.figure(figsize=(10,10)), sns.set(font_scale=0.6)
plt.figure(figsize=(10,10))
sns.set(font_scale=0.6)
sns.heatmap(boston_corr, annot=True, cmap='Blues')
plt.show()
  • finding the most relatable column to MEDV using corr(). except last value, absolute value, ascending sort
boston_corr['MEDV'][:-1].abs().sort_values(ascending=False)
  • separate data X and answers y
X = boston.drop('MEDV', axis=1)
X
y = bosotn['MEDV']
y
  • change the form of DataFrame, Series to numpy array
X = X.values
y = y.values
print(X[:2])
print(y[:2])
  • scaling with MinMaxScaler
from sklearn.preprocessing import MinMaxScaler
    1. define the function mmx
  1. label encoding X with fit_transform function and re-save it to X
  2. check the result
mmx = MinMaxScaler()
X = mmx.fit_transform(X)
print(X)
X.shape
  • modeling Decision Tree
dt = DecisionTreeRegressor()
dt.fit(X, y)
dt.score(X, y)
  • modeling RandomForest
from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor()
rf.fit(X, y)
rf.score(X, y)
  • print sample data
print(X[149])
print(y[149])
  • insert the sample data into the model and predict using rf.predict
pred = rf.predict([X[149]])
print(pred)
  • get libraries for deep learning
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
  • make a Sequential model --> and save to model variable
    input layer: (13, )
    hidden layer: 6 unit, activation='relu'
    output layer: 1 unit
model = Sequential()
model.add(Dense(6, activation='relu', input_shape=(13,)))
model.add(Dense(1))
  • model compile
    loss='mse'
    optimizer='adam'
    metrics=['mse','mae']
model.compile(loss='mse', optimizer='adam', metrics=['mse', 'mae'])
  • model learning: fit
    X, y, epochs=10, batch_size=8
    save the learned result: history
history = model.fit(X, y, epochs=10, batch_size=8)

-improve the accuracy. increase epochs -> 50

history = model.fit(X, y, epochs=50, batch_size=8)
  • evaluate the performance of the deep learning
plt.plot(history.history['loss'], 'r')
plt.plot(history.history['mse'], 'b')
plt.title('Loss and Accuracy')
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legent(["Loss", "MSE"])
plt.show()

0개의 댓글