3 posts

·Tech·Python

About Python

Full page →
Python

monty_python.png The name comes from a comedy group called Monty Python. It is independent of the operating system and can operate as long as you have the appropriate interpreter. In other words, it is a language that interprets the source code at the time of execution without a separate translation process and allows the computer to process it. Python is an object-oriented language that focuses on unit modules rather than execution order, and dynamic typing language determines the type of data that the program should use at the time the program is executed.

Setting up the operating environment

Jupyter Notebook + Collab operating environment settings Jupyter Notebook is an interactive Python shell based on the IPython kernel. When installing, the code below is

miniconda

I used .

conda install jupyter
jupyter notebook
#http://localhost:8888/tree

Colab is a cloud-based jupyter notebook developed by Google. Building an Anaconda virtual environment Anaconda is a tool that allows you to easily install and manage Python libraries. In particular, it gathers together several packages in the fields of mathematics and science to support the use of Python in the fields of data science and machine learning. In addition, it makes it easier to build a virtual environment. When installing Anaconda

Jupyter

,

spyder

,

NumPy

,

SciPy

,

Numba

,

pandas

,

DASK

,

Bokeh

,

HoloViews

,

Datashader

,

matplotlib

,

scikit-learn

,

H2O.ai

,

TensorFlow

,

Conda

It provides various libraries such as:

conda create -n {ENV_NAME} python={PYTHON_VERSION}
conda activate {ENV_NAME}
conda deactivate

If the virtual environment is activated, the library is installed in that path, and Jupyter Notebook can also be run under the virtual environment.

pip install --upgrade pip
pip install -r requirements-window.txt
jupyter notebook

Basic grammar

A variable is a place to store values in programming, it has a memory address, and the value is assigned to it. (von Neumann architecture)

  • Variable names can be declared using letters, numbers, and underscores (_). They are case-sensitive and do not use reserved words. The data type is

integer

,

float

,

string

,

boolean

There is.

Main packages

NumPy A library for scientific calculations. Used for matrix and array processing and operations or random number generation. The basic code is as follows, please refer to the link for more details.

import numpy as np
x = [[1,2,3],[3,4,5]]
np.array(x)          # [[1 2 3]
#  [3 4 5]]
np.arange(10)        # [0 1 2 3 4 5 6 7 8 9]
np.zeros(2,2)        # [[0. 0.]
#  [0. 0.]]
np.full((2,3),5)     # [[5 5 5]
#  [5 5 5]]
np.eye(2)            # [[1. 0.]
#  [0. 1.]]
# 배열 간 연산도 가능합니다
a = np.array([1,2,3])
b = np.array([4,5,6])
c = a+b                     # [5 7 9]
# c = np.add(a,b)
s = np.sum(a)               # 6

Pandas A data processing and analysis library. It is conveniently used to create data objects with rows and columns or to process large amounts of data. There are three types of data structures: Series: 1-dimensional, DataFrame: 2-dimensional, and Panel: 3-dimensional. The basic code is as follows, please refer to the link for more details.

import pandas as pd

  • Import the pandas library.

pd.DataFrame()

  • This is a function that creates a data frame. Enter data to create a table-type data structure consisting of rows and columns.

df.head()

  • Returns the first 5 rows of a dataframe. You can adjust the number of rows to return by specifying a number in parentheses.

df.shape

  • Returns the number of rows and columns in a data frame. It is returned in the form of (number of rows, number of columns).

df.columns

  • Returns the column names of the dataframe.

df.info()

  • Outputs a summary of the basic information of the data frame. You can check the data type of the column, whether there are missing values, etc.

df.describe()

  • Print descriptive statistics information for the data frame. Provides count, average, standard deviation, minimum and maximum values ​​for columns.

df.isnull()

  • Returns a Boolean (True/False) value indicating whether each element in the data frame is a missing value.

df.dropna()

  • Returns a data frame with rows with missing values removed.

df.fillna(value)

  • Fills missing values with the specified value. In value, specify the value to be filled.

df.groupby(column)

  • Groups data based on specified columns. You can perform aggregation and analysis by group.

df.sort_values(column)

  • Sorts data based on a specified column. By default, they are sorted in ascending order; to sort in descending order, set ascending=False.

df.merge(df2)

  • Merge two dataframes. Combine data based on common columns.

df.pivot_table(values, index, columns)

  • Create a pivot table. You can summarize and aggregate data by specifying rows and columns.

df.apply(function)

  • Applies the specified function to the dataframe to perform transformations on each column or row. Sklearn

Provides various machine learning algorithms and data preprocessing functions. sklearn allows you to efficiently perform tasks such as dataset segmentation, feature scaling, model training, and evaluation through an easy-to-use and consistent API. It contains various algorithms such as classification, regression, clustering, and dimensionality reduction, making it an essential tool for data science and machine learning.

train_test_split

  • This function divides the dataset into training and testing.

StandardScaler

  • A scaler that standardizes data (mean 0, standard deviation 1).

LinearRegression

  • A class for training and predicting linear regression models.

DecisionTreeClassifier

  • This is a class that learns and predicts decision tree-based classification models.

RandomForestRegressor

  • This is a class that trains and predicts random forest regression models.

KMeans

  • A class that assigns clusters using the K-means clustering algorithm.

accuracy_score

  • This is a function that evaluates the accuracy of the classification model.

mean_squared_error

  • A function that calculates the mean squared error of a regression model.

GridSearchCV

  • This class finds the optimal parameters of the model through grid search.

CountVectorizer

  • A class that vectorizes words based on their frequency in a set of documents.

Python programming basics

Comments

No comments yet. Be the first!