Linier Regresion ML
Linier Regresion ML
Buka anaconda
2. Buka Jupyter notebook
3. Buka file …. New …. notebook
4. Buka home Jupyter notebook
5. Upload datasheet.csv
6. Ketikan program berikut pada terminal kemudian Run
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df.iloc[:, 2] memilih kolom kedua tetapi df.iloc[:, :2] atau secara eksplisit df.iloc[:, 0:2] memilih kolom
hingga (tidak termasuk) kolom kedua. Ini sama dengan irisan Python. Saat Anda menggunakan indeks
negatif, tidak ada perubahan. Kalau dibilang df.iloc[:, -1] artinya kolom terakhir, tapi df.iloc[:, :-1] artinya
sampai kolom terakhir.
y_pred
y_test
print(regressor.coef_)
print(regressor.intercept_)
16. R2 Score
from sklearn.metrics import r2_score
score = r2_score(y_test, y_pred)
print("The accuracy of our model is {}%".format(round(score, 2) *100))
#%%
# General dependencies
import pandas as pd # for data handling
import matplotlib.pyplot as plt # for linear plot
import seaborn as sns # for scatter plot
from sklearn.model_selection import train_test_split
import datetime
# Read sensor data
sensor = pd.read_csv("data.csv", sep = ';', index_col = 0, parse_dates =
False)
nama file, separator, dimulai index col 0 sebagai index dataframe
# Model
lr = LinearRegression()
# Fit
lr.fit(X_train, Y_train)
# Get MLR coefficients
print('Intercept: \n', lr.intercept_)
print('Coefficients: \n', lr.coef_)
# Predict
df_test["MLR_Pred"] = lr.intercept_ + lr.coef_[0]*df_test["Sensor_O3"] +
lr.coef_[1]*df_test["Temp"] + lr.coef_[2]*df_test["RelHum"]
# Plot linear
df_test[["RefSt", "MLR_Pred"]].plot()
plt.xticks(rotation = 20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'MLR_Pred', data = df_test, fit_reg = True,
line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["MLR_Pred"])
# K-Nearest Neighbor
from sklearn.neighbors import KNeighborsRegressor
# Model
knn = KNeighborsRegressor(n_neighbors = 19)
# Fit
knn.fit(X_train, Y_train)
# Predict
df_test["KNN_Pred"] = knn.predict(X_test)
print(df_test)
# Plot linear
df_test[["RefSt", "KNN_Pred"]].plot()
plt.xticks(rotation=20)
# Plot regression
sns.lmplot(x = 'RefSt', y = 'KNN_Pred', data = df_test, fit_reg = True,
line_kws = {'color': 'orange'})
# Loss
loss_functions(y_true = df_test["RefSt"], y_pred = df_test["KNN_Pred"])
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('LOG1.csv')
dates =list(dataset['Date'])
princes=list(dataset['Temp'])
plt.plot (dates,princes)
Olah datalogger
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.dates as mdates
import re
def correct_datetime_format(datetime_str):
if datetime_str == 'NaT':
return datetime_str
# Memperbaiki jam, menit, dan detik satu digit
datetime_str = re.sub(r'(\d{4}/\d{1,2}/\d{1,2})\s+(\d{1,2}):(\d{1,2}):(\d{1,2})',
lambda x: f"{x.group(1)}
{int(x.group(2)):02}:{int(x.group(3)):02}:{int(x.group(4)):02}",
datetime_str)
return datetime_str
df1_clean = df1.drop(0).reset_index(drop=True)
df1_clean['DateTime'] = df1_clean['DateTime'].astype(str).apply(correct_datetime_format)
df1_clean['Soil'] = pd.to_numeric(df1_clean['Soil'], errors='coerce')
df1_clean['Temperature'] = pd.to_numeric(df1_clean['Temperature'], errors='coerce')
df1_clean.head()