Python Primer
Getting Started
print
Mutable and slice
Python Objects
Each object has a list of attributes
Any attribute
attrof 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
setsare unordered collections of unique objects{}
Lists
리μ€νΈ: μ€λ³΅(κ°μ κ° μ¬λ¬ λ² ν¬ν¨) κ°λ₯, μμ 보μ₯ κ°λ₯(μμλ€μ΄ μΆκ°λ μμλλ‘ μ μ₯λ¨, μΈλ±μ€λ‘ μ κ·Όν μ μμ), μμ κ°λ₯(μμμ μΆκ°, μμ , λ³κ²½ κ°λ₯)
Set: μ€λ³΅λ κ°μ νμ©νμ§ μκ³ , μμλ₯Ό 보μ₯νμ§ μκ³ (λ°λΌμ μΈλ±μ€λ‘ μ κ·Όν μ μμ), μμμ μΆκ° λ° μμ κ° κ°λ₯νλ νΉμ μμΉμ μμλ₯Ό λ³κ²½νλ κ²μ λΆκ°λ₯
Dictonary
- λμ
λ리:
key:valueμ μ‘°ν©μΌλ‘ μ΄λ£¨μ΄μ§, ν€λ₯Ό μ¬μ©νμ¬ κ°μ μ κ·Ό, ν€λ μ€λ³΅λ μ μμ§λ§ κ°μ μ€λ³΅λ μ μμ
Classes
- μλ‘μ΄ ν΄λμ€ λ§λλ μμ
Files
- κ΅μ¬μ μλ μμ μ§μ ν΄ λ³΄μΈμ
Numpy
Numpyμ μΌκ°ν¨μ
Creating and shaping arrays
The fundamental data type in
numpyis thendarray.Note that
arangeisnumpyβs version ofrange, with the diβ΅erence thatarangereturns anndarrayobject.The dimension of an
ndarraycan be obtained via itsshapemethod, which returns a tuple.Arrays can be reshaped via the
reshapemethod. This does not change the currentndarrayobject.hstackandvstack: 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.
ndarraysare mutable
Array Operations
Basic mathematical operators and functions act element-wise on
ndarrayobjects.Since version 3.5 of Python, it is possible to multiply two
ndarrays using the@operator (which implements thenp.matmulmethod). For matrices, this is similar to using the dot method. For higher-dimensional arrays the two methods behave differently.numpyallows arithmetic operations on arrays of different shapes (dimensions).
Random numbers
numpyμλrandomμ΄λΌλ sub-modules μ‘΄μ¬
Matplotlib
- μ°μ λ μμ
Pandas
pandas:DataFrameν΄λμ€λ₯Ό ν¬ν¨νμ¬ λ°μ΄ν° ꡬμ±κ³Ό λΆμμ νμν λ€μν ν΄ μ 곡
Extracting Information
The
applymethod allows one to apply general functions to columns or rows of a DataFrame.The
locmethod allows for accessing elements (or ranges) in a data frame.count: Counts number of non-NA cells.The
groupbymethod 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νμ© κ°λ₯