Python Primer
Getting Started
print
Mutable and slice
Python Objects
Each object has a list of attributes
Any attribute
attr
of an object obj can be accessed via the dot notation
Types and Operators
- Each object has a type:
str
,int
,float
, β¦
- reference
- Operator overloading: Operators such as
+
and*
can be defined for other data types as well
Functions and Methods
- Compute population mean and variance
Modules
- λͺ¨λ: κ΄λ ¨ ν¨μ, ν΄λμ€, λ³μ λ±μ κ·Έλ£Ήνν νμΌ(
.py
νμΌ)datetime
,matplotlib
,numpy
,os
,pandas
, β¦
Flow Control
while
,for
,if-else
, β¦
Iteration
Sets
- Python
sets
are unordered collections of unique objects{}
Lists
리μ€νΈ: μ€λ³΅(κ°μ κ° μ¬λ¬ λ² ν¬ν¨) κ°λ₯, μμ 보μ₯ κ°λ₯(μμλ€μ΄ μΆκ°λ μμλλ‘ μ μ₯λ¨, μΈλ±μ€λ‘ μ κ·Όν μ μμ), μμ κ°λ₯(μμμ μΆκ°, μμ , λ³κ²½ κ°λ₯)
Set: μ€λ³΅λ κ°μ νμ©νμ§ μκ³ , μμλ₯Ό 보μ₯νμ§ μκ³ (λ°λΌμ μΈλ±μ€λ‘ μ κ·Όν μ μμ), μμμ μΆκ° λ° μμ κ° κ°λ₯νλ νΉμ μμΉμ μμλ₯Ό λ³κ²½νλ κ²μ λΆκ°λ₯
Dictonary
- λμ
λ리:
key:value
μ μ‘°ν©μΌλ‘ μ΄λ£¨μ΄μ§, ν€λ₯Ό μ¬μ©νμ¬ κ°μ μ κ·Ό, ν€λ μ€λ³΅λ μ μμ§λ§ κ°μ μ€λ³΅λ μ μμ
Classes
- μλ‘μ΄ ν΄λμ€ λ§λλ μμ
Files
- κ΅μ¬μ μλ μμ μ§μ ν΄ λ³΄μΈμ
Numpy
Numpy
μ μΌκ°ν¨μ
Creating and shaping arrays
The fundamental data type in
numpy
is thendarray
.Note that
arange
isnumpy
βs version ofrange
, with the diβ΅erence thatarange
returns anndarray
object.The dimension of an
ndarray
can be obtained via itsshape
method, which returns a tuple.Arrays can be reshaped via the
reshape
method. This does not change the currentndarray
object.hstack
andvstack
: The arrays are joined horizontally and vertically, respectively.
Slicing
Arrays can be sliced similarly to Python lists.
If an array has several dimensions, a slice for each dimension needs to be specified.
ndarrays
are mutable
Array Operations
Basic mathematical operators and functions act element-wise on
ndarray
objects.Since version 3.5 of Python, it is possible to multiply two
ndarray
s using the@
operator (which implements thenp.matmul
method). For matrices, this is similar to using the dot method. For higher-dimensional arrays the two methods behave differently.numpy
allows arithmetic operations on arrays of different shapes (dimensions).
Random numbers
numpy
μλrandom
μ΄λΌλ sub-modules μ‘΄μ¬
Matplotlib
- μ°μ λ μμ
Pandas
pandas
:DataFrame
ν΄λμ€λ₯Ό ν¬ν¨νμ¬ λ°μ΄ν° ꡬμ±κ³Ό λΆμμ νμν λ€μν ν΄ μ 곡
Extracting Information
The
apply
method allows one to apply general functions to columns or rows of a DataFrame.The
loc
method allows for accessing elements (or ranges) in a data frame.count
: Counts number of non-NA cells.The
groupby
method of a DataFrame object is useful for summarizing and displaying the data in manipulated ways.mean
: Column/row mean.
Scikit-learn
Partitioning the Data
train_test_split
ν¨μλ‘ μλ£ λΆν κ°λ₯
Standardization
MinMaxScaler
,StandardScaler
νμ© κ°λ₯