0% found this document useful (0 votes)
4 views

Import Library Python

IMPORT LIBRARY PYTHON

Uploaded by

BudiYono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Import Library Python

IMPORT LIBRARY PYTHON

Uploaded by

BudiYono
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

IMPORT LIBRARY PYTHON

from flask import Flask, request, render_template


import numpy as np
import pandas as pd
import joblib
from keras.models import Sequential, load_model
from keras.layers import Dense
from sklearn.svm import SVR
from sklearn.preprocessing import StandardScaler
import os
from sklearn.svm import SVR
import joblib
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error, r2_score
import pickle
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('Agg')
import math
import os
import re
import time
import pickle
import emoji
import string
import subprocess
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from flask import Flask, render_template, url_for, redirect, request,
session, send_file
from nltk.corpus import stopwords
from nltk.stem import WordNetLemmatizer, PorterStemmer
from sklearn.cluster import KMeans
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import accuracy_score, precision_score, recall_score,
f1_score, confusion_matrix, \
classification_report
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from statistics import mean
from io import BytesIO
import base64
import numpy as np
from sklearn.datasets import make_regression
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error

Berikut penjelasan script:


Impor Library
1. Flask dan library terkait
 `Flask`, `request`, `render_template` Digunakan untuk membuat aplikasi web,
menangani permintaan HTTP, dan merender template HTML.
 `url_for`, `redirect`, `session`, `send_file`: Fitur tambahan dari Flask untuk menangani
URL, redirect, manajemen sesi, dan pengiriman file.
2. Manipulasi data dan visualisasi
 `numpy`, `pandas`: Library untuk operasi numerik dan manipulasi data.
 `seaborn`, `matplotlib.pyplot`, `matplotlib`: Library untuk membuat plot dan
visualisasi.
3. Machine Learning (Pembelajaran Mesin)
 `keras`: API tingkat tinggi untuk jaringan saraf, biasanya digunakan dengan backend
TensorFlow.
 `sklearn` (Scikit-learn): Berisi berbagai alat untuk pembelajaran mesin seperti regresi,
klasifikasi, clustering, dan preprocessing.
 `SVR`, `MLPRegressor`, `KMeans`, `KNeighborsClassifier`, `SVC`,
`MLPClassifier`: Berbagai model pembelajaran mesin.
 `StandardScaler`, `TfidfVectorizer`, `train_test_split`, `mean_squared_error`,
`accuracy_score`, dan sebagainya: Alat untuk penskalaan data, ekstraksi fitur,
pelatihan/penguji model, dan evaluasi kinerja.
4. Pemrosesan teks
 `nltk`: Toolkit untuk pemrosesan data bahasa manusia.
 `stopwords`, `WordNetLemmatizer`, `PorterStemmer`: Alat untuk normalisasi dan
stemming teks.
5. Utilitas lain
 `joblib`, `pickle`: Library untuk menyimpan dan memuat objek Python.
 `math`, `os`, `re`, `time`, `emoji`, `string`, `subprocess`: Library umum untuk operasi
matematika, interaksi sistem operasi, ekspresi reguler, pengukuran waktu, penanganan
emoji, manipulasi string, dan manajemen subprocess.
TRAINING ANN
def train_ann():
# Membaca DataFrame dari file CSV
df = pd.read_csv('dataset.csv', sep=",")
df.set_index('no', inplace=True)
df = df.loc[df['wh'] <= 17500]
# Membuat kolom x1, x2, x3, suhu, kelembapan, dan angin
df['x1'] = (df['x11'] + df['x12'] + df['x13'] + df['x14'] +df['x15'] +
df['x16'])
df['x2'] = (df['x21'] + df['x22'])
df['x3'] = (df['x31'] + df['x32'] + df['x33'] + df['x34'] +df['x35'] +
df['x36'] + df['x37'])
no = 0
rmse=[]

for sd in setdata:
dfx = df[sd]
print(dfx.head())
dfx.to_csv("datasets/dataset"+str(no)+".csv")
##############
# Mendeteksi dan menampilkan outliers
outliers, lower_bound, upper_bound = detect_and_plot_outliers(df,
'wh', 'static/outlier'+str(no)+'.png')
# Mengatasi outliers
dfx = handle_outliers(df, 'wh', method='remove')
print("DataFrame after handling outliers:\n", dfx)
##############
X = dfx.drop(columns=['wh'])
y = dfx['wh']

# Split the data


X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Standardize the data


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Train the ANN model


ann = MLPRegressor(hidden_layer_sizes=(1000,), max_iter=1000,
random_state=42)
ann.fit(X_train, y_train)

# Save the model


model_filename = 'models/ann_' + str(no) + '.pkl'
with open(model_filename, 'wb') as f:
pickle.dump(ann, f)

# Evaluate the model


y_pred = ann.predict(X_test)
rmse.append(math.sqrt(mean_squared_error(y_test, y_pred)))
r2 = r2_score(y_test, y_pred)

print(f'Model {no}: RMSE={rmse}, R^2={r2}')


# Plot true vs predicted values
plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_pred, alpha=0.7)
plt.xlabel('True Values ' + str(no))
plt.ylabel('Predictions ' + str(no))
plt.title('True vs Predicted Values ' + str(no))
plt.tight_layout()
plt.grid(True)
cm_img_path = 'static/images/ann_true_vs_pred_' + str(no) + '.png'
plt.savefig(cm_img_path)
plt.close()
no += 1
return render_template('train_ann.html', rmse=rmse)

train_size : .08
test_size : 0.2
menunjukkan jumlah data perbandingan antara data training dan data testing.

Hidden_layer : 1000
Max_iteration : 1000
Random state = 42
TRAIN SVM
def train_svm():
df = pd.read_csv('dataset.csv', sep=",")
df.set_index('no', inplace=True)
df = df.loc[df['wh'] <= 17500]

df['x1'] = (df['x11'] + df['x12'] + df['x13'] + df['x14'] + df['x15'] +


df['x16'])
df['x2'] = (df['x21'] + df['x22'])
df['x3'] = (df['x31'] + df['x32'] + df['x33'] + df['x34'] + df['x35'] +
df['x36'] + df['x37'])

no = 0
rmse = []

for sd in setdata:
dfx = df[sd]
print(dfx.head())
dfx.to_csv("datasets/dataset" + str(no) + ".csv")

outliers, lower_bound, upper_bound = detect_and_plot_outliers(df,


'wh', 'static/outlier' + str(no) + '.png')
dfx = handle_outliers(df, 'wh', method='remove')
print("DataFrame after handling outliers:\n", dfx)

X = dfx.drop(columns=['wh'])
y = dfx['wh']

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=42)

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

# Train the SVM model


svm_model = SVR(kernel='rbf', C=100, gamma=0.1, epsilon=.1)
svm_model.fit(X_train, y_train)

# Save the model


model_filename = 'models/svm_' + str(no) + '.pkl'
with open(model_filename, 'wb') as f:
pickle.dump(svm_model, f)

# Evaluate the model


y_pred = svm_model.predict(X_test)
rmse_value = math.sqrt(mean_squared_error(y_test, y_pred))
rmse.append(rmse_value)
r2 = r2_score(y_test, y_pred)

print(f'Model {no}: RMSE={rmse_value}, R^2={r2}')

plt.figure(figsize=(8, 6))
plt.scatter(y_test, y_pred, alpha=0.7)
plt.xlabel('True Values ' + str(no))
plt.ylabel('Predictions ' + str(no))
plt.title('True vs Predicted Values ' + str(no))
plt.tight_layout()
plt.grid(True)
cm_img_path = 'static/images/svm_true_vs_pred_' + str(no) + '.png'
plt.savefig(cm_img_path)
plt.close()
no += 1

return render_template('train_svm.html', mrse=rmse)

train_size : 0.8
test_size : 0.2
kernel : rbf
C : 100
Gamma : 0.1
Epsilon : 0.1
ANN GA
def train_ann_ga():
# Membaca DataFrame dari file CSV
df = pd.read_csv('dataset.csv', sep=",")
df.set_index('no', inplace=True)
df = df.loc[df['wh'] <= 17500]
# Membuat kolom x1, x2, x3, suhu, kelembapan, dan angin
df['x1'] = (df['x11'] + df['x12'] + df['x13'] + df['x14'] +df['x15'] +
df['x16'])
df['x2'] = (df['x21'] + df['x22'])
df['x3'] = (df['x31'] + df['x32'] + df['x33'] + df['x34'] +df['x35'] +
df['x36'] + df['x37'])

no = 0
rmse=[]

for sd in setdata:
dfx = df[sd]
print(dfx.head())
dfx.to_csv("datasets/dataset"+str(no)+".csv")
outliers, lower_bound, upper_bound = detect_and_plot_outliers(df,
'wh', 'static/outlier'+str(no)+'.png')
# Mengatasi outliers
dfx = handle_outliers(df, 'wh', method='remove')
print("DataFrame after handling outliers:\n", dfx)
X = dfx.drop(columns=['wh'])
y = dfx['wh']
#####GAAAAA
X, y = make_regression(n_samples=100, n_features=2, noise=0.1)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Inisialisasi dan optimasi dengan GA


ga = GA_MLPRegressor(X_train, X_test, y_train, y_test,
population_size=10, generations=20,
mutation_rate=0.1,model_filename='models/ann_' + str(no) + '_ga.pkl')
ga.optimize()

y_pred=ga.y_pred
rmse.append(ga.best_rmse)
r2 = r2_score(ga.y_test, ga.y_pred)

print(f'Model {no}: RMSE={ga.best_rmse}, R^2={r2}')


# Plot true vs predicted values
plt.figure(figsize=(8, 6))
plt.scatter(ga.y_test, ga.y_pred, alpha=0.7)
plt.xlabel('True Values ' + str(no))
plt.ylabel('Predictions ' + str(no))
plt.title('True vs Predicted Values ' + str(no))
plt.tight_layout()
plt.grid(True)
cm_img_path = 'static/images/ann_ga_true_vs_pred_' + str(no) +
'.png'
plt.savefig(cm_img_path)
plt.close()
no += 1
return render_template('train_ann_ga.html', rmse=rmse)

n_samples =100
n_features : 2
noise : 0.1
train_size : 0.8
test_size : 0.2
random_state : 42
population_size : 10
generations : 20
mutation_rate : 0.1
SVM GA
def train_svm_ga():
# Membaca DataFrame dari file CSV
df = pd.read_csv('dataset.csv', sep=",")
df.set_index('no', inplace=True)
df = df.loc[df['wh'] <= 17500]
# Membuat kolom x1, x2, x3, suhu, kelembapan, dan angin
df['x1'] = (df['x11'] + df['x12'] + df['x13'] + df['x14'] + df['x15'] +
df['x16'])
df['x2'] = (df['x21'] + df['x22'])
df['x3'] = (df['x31'] + df['x32'] + df['x33'] + df['x34'] + df['x35'] +
df['x36'] + df['x37'])
no = 0
rmse=[]

for sd in setdata:
dfx = df[sd]
print(dfx.head())
dfx.to_csv("datasets/dataset"+str(no)+".csv")
##############
# Mendeteksi dan menampilkan outliers
outliers, lower_bound, upper_bound = detect_and_plot_outliers(df,
'wh', 'static/outlier'+str(no)+'.png')
# Mengatasi outliers
dfx = handle_outliers(df, 'wh', method='remove')
print("DataFrame after handling outliers:\n", dfx)
##############
X = dfx.drop(columns=['wh'])
y = dfx['wh']
#####GAAAAA
X, y = make_regression(n_samples=100, n_features=2, noise=0.1)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)

# Inisialisasi dan optimasi dengan GA


ga = GA_SVR(X_train, X_test, y_train, y_test, population_size=10,
generations=20, mutation_rate=0.1,model_filename='models/svm_' + str(no) +
'_ga.pkl')
ga.optimize()

y_pred=ga.y_pred
rmse.append(ga.best_rmse)
r2 = r2_score(ga.y_test, ga.y_pred)

print(f'Model {no}: RMSE={ga.best_rmse}, R^2={r2}')


# Plot true vs predicted values
plt.figure(figsize=(8, 6))
plt.scatter(ga.y_test, ga.y_pred, alpha=0.7)
plt.xlabel('True Values ' + str(no))
plt.ylabel('Predictions ' + str(no))
plt.title('True vs Predicted Values ' + str(no))
plt.tight_layout()
plt.grid(True)
cm_img_path = 'static/images/svm_ga_true_vs_pred_' + str(no) +
'.png'
plt.savefig(cm_img_path)
plt.close()
no += 1
return render_template('train_svm_ga.html', rmse=rmse)
n_features : 2
noise : 0.1
train_size : 0.8
test_size : 0.2
random_state : 42
population_size : 10
generations : 20
mutation_rate : 0.1

You might also like