Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

08/07/22 21.27 UTSPembelajaranMesin.

ipynb - Colaboratory

UTS Pembelajaran Mesin

Nama, Nim, Kelas : Muhamad Khaerul Rijal - 1187050063 - C

DATA GAJI

mendata gaji karyawan sesusai tahun bekerja

Data Preparation

Membahas mengenai load data, pemilihan data, pembersihan data, kontuksi data, pelabelan data, ine

import numpy as np
import·matplotlib.pyplot·as·plt
import·pandas·as·pd
import·tensorflow·as·tf

url·=·"https://raw.githubusercontent.com/khaerul99/machine/main/Salary_Data.csv"
df = pd.read_csv(url, sep=',')
df.head(10)

YearsExperience Salary

0 1.1 39343.0

1 1.3 46205.0

2 1.5 37731.0

3 2.0 43525.0

4 2.2 39891.0

5 2.9 56642.0

6 3.0 60150.0

7 3.2 54445.0

8 3.2 64445.0

9 3.7 57189.0

df.info()

<class 'pandas.core.frame.DataFrame'>
https://colab.research.google.com/drive/16JqKSW1n1_tMrzAGy6diM3RKw8_tO3Ab?hl=id#scrollTo=pb0dyUljFYwj&printMode=true 1/6
08/07/22 21.27 UTSPembelajaranMesin.ipynb - Colaboratory
RangeIndex: 30 entries, 0 to 29
Data columns (total 2 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 YearsExperience 30 non-null float64
1 Salary 30 non-null float64
dtypes: float64(2)
memory usage: 608.0 bytes

x = df[df.columns[:1]]

y = df['Salary']

https://colab.research.google.com/drive/16JqKSW1n1_tMrzAGy6diM3RKw8_tO3Ab?hl=id#scrollTo=pb0dyUljFYwj&printMode=true 2/6
08/07/22 21.27 UTSPembelajaranMesin.ipynb - Colaboratory

YearsExperience

0 1.1

1 1.3

2 1.5

3 2.0

4 2.2

5 2.9

6 3.0

y 7 3.2

8 3.2
0 39343.0
19 46205.0 3.7
2 37731.0
310 43525.0 3.9
4 39891.0
511 56642.0 4.0
6 60150.0
12 4.0
7 54445.0
813 64445.0 4.1
9 57189.0
10
14 63218.0 4.5
11 55794.0
15
12 56957.0 4.9
13 57081.0
16
14 61111.0 5.1
15 67938.0
17 5.3
16 66029.0
17
18 83088.0 5.9
18 81363.0
19
19 93940.0 6.0
20 91738.0
20
21 98273.0 6.8
22 101302.0
21
23 113812.0 7.1
24
22 109431.0 7.9
25 105582.0
26
23 116969.0 8.2
27 112635.0
24
28 122391.0 8.7
29 121872.0
25
Name: 9.0 float64
Salary, dtype:
26 9.5
from sklearn.preprocessing import StandardScaler
27 9.6

28 10.3
scaler = StandardScaler()
29
scaler.fit(x) 10.5

StandardScaler()

https://colab.research.google.com/drive/16JqKSW1n1_tMrzAGy6diM3RKw8_tO3Ab?hl=id#scrollTo=pb0dyUljFYwj&printMode=true 3/6
08/07/22 21.27 UTSPembelajaranMesin.ipynb - Colaboratory

x = scaler.transform(x)

array([[-1.51005294],
[-1.43837321],
[-1.36669348],
[-1.18749416],
[-1.11581443],
[-0.86493538],
[-0.82909552],
[-0.75741579],
[-0.75741579],
[-0.57821647],
[-0.50653674],
[-0.47069688],
[-0.47069688],
[-0.43485702],
[-0.29149756],
[-0.1481381 ],
[-0.07645838],
[-0.00477865],
[ 0.21026054],
[ 0.2461004 ],
[ 0.53281931],
[ 0.6403389 ],
[ 0.92705781],
[ 1.03457741],
[ 1.21377673],
[ 1.32129632],
[ 1.50049564],
[ 1.5363355 ],
[ 1.78721455],
[ 1.85889428]])

Modeling data

Membahas mengenai proses fiting model, set parameter dll.

from sklearn.model_selection import train_test_split

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.4,  random_state=

from sklearn.linear_model import LinearRegression
regressor =  LinearRegression()
regressor.fit(x_train, y_train)

LinearRegression()

Evaluasi

https://colab.research.google.com/drive/16JqKSW1n1_tMrzAGy6diM3RKw8_tO3Ab?hl=id#scrollTo=pb0dyUljFYwj&printMode=true 4/6
08/07/22 21.27 UTSPembelajaranMesin.ipynb - Colaboratory

Membahas mengenai hasil model

def viz_linear():
  plt.scatter(x, y, color='red')
  plt.plot(x, lin_reg.predict(x), color='blue')
  plt.title('SALARY_DATA')
  plt.xlabel('Tahun')
  plt.ylabel('Gaji')
  plt.show()
  return
  viz_linear()

viz_test = plt
viz_test.scatter(x_test, y_test, color='red')
viz_test.plot(x_train, regressor.predict(x_train), color='blue')
viz_test.title('SALARY_DATA')
viz_test.xlabel('Tahun')
viz_test.ylabel('Gaji')
viz_test.show()

https://colab.research.google.com/drive/16JqKSW1n1_tMrzAGy6diM3RKw8_tO3Ab?hl=id#scrollTo=pb0dyUljFYwj&printMode=true 5/6
08/07/22 21.27 UTSPembelajaranMesin.ipynb - Colaboratory

check 0 d selesai pada 21.25

https://colab.research.google.com/drive/16JqKSW1n1_tMrzAGy6diM3RKw8_tO3Ab?hl=id#scrollTo=pb0dyUljFYwj&printMode=true 6/6

You might also like