6/21/2021 Get Started with Python
Memulai Python!
Dapat menuliskan keterangan kode menggunakan Markdown
In [3]: print("Hello world!") # mode skrip
Hello world!
In [4]: height = 1.84
In [5]: tall = True
In [6]: height1 = 1.84
In [7]: height2 = 1.79
In [8]: height3 = 1.82
In [9]: height4 = 1.90
Masalah :
Terlalu banyak data masukan untuk tipe data yang sama
Tidak nyaman
Solusi: Python List
In [10]: [1.84, 1.79, 1.82, 1.90, 1.80]
Out[10]: [1.84, 1.79, 1.82, 1.9, 1.8]
In [11]: height = [1.84, 1.79, 1.82, 1.90, 1.80]
In [12]: height
Out[12]: [1.84, 1.79, 1.82, 1.9, 1.8]
In [14]: famz = ["Abe", 1.84, "Beb", 1.79, "Cory", 1.82, "Dad", 1.90]
In [15]: famz
Out[15]: ['Abe', 1.84, 'Beb', 1.79, 'Cory', 1.82, 'Dad', 1.9]
In [16]: weight = [66.5, 60.3, 64.7, 89.5, 69.8]
In [17]: weight
Out[17]: [66.5, 60.3, 64.7, 89.5, 69.8]
localhost:8888/notebooks/2021 Python for Data Science - ITB/Get Started with Python.ipynb# 1/5
6/21/2021 Get Started with Python
In [18]: weight / height ** 2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-18-cb550bf380fe> in <module>()
----> 1 weight / height ** 2
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
Solusi: NumPy
Library dasar untuk perhitungan saintifik (scientific computing) dengan Python (https://numpy.org/
(https://numpy.org/))
Alternatif untuk Python List: Numpy Array untuk n-dimensi
Mudah digunakan dan bersifat open source
Jika library belum terpasang, tuliskan perintah instalasi: pip install numpy
Kemudian impor: import numpy as np
In [19]: import numpy as np
In [20]: np_height = np.array(height)
In [21]: np_height
Out[21]: array([1.84, 1.79, 1.82, 1.9 , 1.8 ])
In [22]: np_weight = np.array(weight)
In [23]: np_weight
Out[23]: array([66.5, 60.3, 64.7, 89.5, 69.8])
In [24]: bmi = np_weight / np_height ** 2
In [25]: bmi
Out[25]: array([19.64201323, 18.81963734, 19.53266514, 24.79224377, 21.54320988])
Untuk melihat fungsi lain pada NumPy, gunakan perintah np.< TAB >
In [ ]: np.
In [27]: np_height = np.array([1.84, 1.79, 1.82, 1.9, 1.8])
In [28]: np_weight = np.array([66.5, 60.3, 64.7, 89.5, 69.8])
In [29]: type(np_height)
Out[29]: numpy.ndarray
In [30]: type(np_weight)
Out[30]: numpy.ndarray
In [31]: np_2d = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
localhost:8888/notebooks/2021 Python for Data Science - ITB/Get Started with Python.ipynb# 2/5
6/21/2021 Get Started with Python
In [32]: np_2d
Out[32]: array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10]])
In [33]: np_2d.shape
Out[33]: (2, 5)
SciPy
SciPy (dibaca “Sigh Pie”) merupakan library yang bersifat open source dan tersedia di https://www.scipy.org/
(https://www.scipy.org/)
SciPy dibangun untuk untuk bekerja dengan NumPy array dan menyediakan kumpulan algoritma numerik,
termasuk pemrosesan sinyal, optimasi, statistika, dan library Matplotlib untuk visualisasi data.
Jika library belum terpasang, tuliskan perintah instalasi: pip install scipy
Pandas
Pandas (Panel Data) merupakan library popular di Python yang digunakan untuk data structure dan data
analysis
Bersifat open source dan tersedia di https://pandas.pydata.org/ (https://pandas.pydata.org/)
Pandas sangat berkaitan dengan NumPy
Jika library belum terpasang, tuliskan perintah instalasi: pip install pandas
Kemudian impor: import pandas as pd
In [37]: # series
np.array([1, 2, 3, 4, 5])
Out[37]: array([1, 2, 3, 4, 5])
In [38]: # DataFrame
np.array([[1, 2], [3, 4]])
Out[38]: array([[1, 2],
[3, 4]])
In [39]: import pandas as pd
In [51]: Tab = pd.read_csv("Tab.csv")
In [52]: Tab
Out[52]:
Unnamed: 0 Negara Populasi Area Ibukota
0 IN Indonesia 250 123456 Jakarta
1 MA Malaysia 25 3456 KL
2 SI Singapura 15 456 Singapura
3 JP Jepang 60 5678 Tokyo
4 TH Thailand 45 678 Bangkok
In [54]: Tab["Negara"]
Out[54]: 0 Indonesia
1 Malaysia
2 Singapura
3 Jepang
4 Thailand
Name: Negara, dtype: object
localhost:8888/notebooks/2021 Python for Data Science - ITB/Get Started with Python.ipynb# 3/5
6/21/2021 Get Started with Python
In [55]: Tab.Ibukota
Out[55]: 0 Jakarta
1 KL
2 Singapura
3 Tokyo
4 Bangkok
Name: Ibukota, dtype: object
Matplotlib
Matplotlib adalah library Python untuk visualisasi data dengan dua dimensi
Bersifat open source dan tersedia di https://matplotlib.org/ (https://matplotlib.org/)
Matplotlib berkaitan dengan NumPy dan Pandas
Jika library belum terpasang, tuliskan perintah instalasi: pip install matplotlib
Kemudian impor: import matplotlib.pyplot as plt
In [56]: import matplotlib.pyplot as plt
In [64]: year = [1980, 1990, 2000, 2010, 2020]
In [60]: price = [2.5, 7.6, 9.7, 15.8, 22.9]
In [69]: plt.plot(year, price)
plt.show()
In [67]: plt.scatter(year,price)
Out[67]: <matplotlib.collections.PathCollection at 0x1ec21c92f98>
localhost:8888/notebooks/2021 Python for Data Science - ITB/Get Started with Python.ipynb# 4/5
6/21/2021 Get Started with Python
In [70]: plt.bar(year,price)
Out[70]: <BarContainer object of 5 artists>
In [ ]:
localhost:8888/notebooks/2021 Python for Data Science - ITB/Get Started with Python.ipynb# 5/5