ml-lab/Notes/Numpy_Base.ipynb
2025-10-21 18:46:22 +05:00

453 lines
8.8 KiB
Text
Executable file

{
"cells": [
{
"cell_type": "markdown",
"id": "1de8b57d",
"metadata": {},
"source": [
"# Основы Numpy"
]
},
{
"cell_type": "markdown",
"id": "e442423a",
"metadata": {},
"source": [
"## Основная информация"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "54ad0b92",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "6f127250",
"metadata": {},
"source": [
"ndarray - основная единица в NumPy, представляет из себя n-мерный массив."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "5df38ce7",
"metadata": {},
"outputs": [],
"source": [
"data: np.ndarray = np.array(\n",
" [\n",
" [\n",
" [1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9],\n",
" ],\n",
" [\n",
" [1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9],\n",
" ],\n",
" [\n",
" [1, 2, 3],\n",
" [4, 5, 6],\n",
" [7, 8, 9],\n",
" ],\n",
" ],\n",
")"
]
},
{
"cell_type": "markdown",
"id": "5b68b84b",
"metadata": {},
"source": [
"Параметры и базовые функции:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "866a3fb8",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(3, 3, 3)\n",
"3\n"
]
}
],
"source": [
"# Размерность массива\n",
"shape = data.shape\n",
"print(shape)\n",
"\n",
"# Кол-во измерений\n",
"dimentions = data.ndim\n",
"print(dimentions)"
]
},
{
"cell_type": "markdown",
"id": "6e167e93",
"metadata": {},
"source": [
"## Измерения в Numpy"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "c9013d88",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"np.int64(60)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 0-D arrays - Scalars\n",
"scalar = np.array(42)\n",
"scalar2 = np.array(18)\n",
"\n",
"scalar + scalar2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7f705611",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"82\n",
"[10 24 48]\n"
]
}
],
"source": [
"# 1-D arrays - Vectors\n",
"vec1 = np.array([1, 2, 3])\n",
"vec2 = np.array((10, 12, 16))\n",
"\n",
"print(np.dot(vec1, vec2))\n",
"print(vec1 * vec2)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "b3a9d28b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10 12]\n",
"[13 15]\n",
"[4 8]\n"
]
},
{
"data": {
"text/plain": [
"array([61, 81])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 2-D arrays - Matrix\n",
"matrix = np.array(\n",
" [\n",
" [10, 12],\n",
" [13, 15],\n",
" [4, 8],\n",
" ]\n",
")\n",
"\n",
"vec = np.array(\n",
" [\n",
" 1, 3, 3,\n",
" ]\n",
")\n",
"\n",
"print(*matrix, sep=\"\\n\")\n",
"\n",
"# Умножение вектора на матрицу\n",
"vec @ matrix"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "ec7aafca",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[12 15 8]\n"
]
},
{
"data": {
"text/plain": [
"array([12, 45, 24])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vec2 = matrix[:, 1]\n",
"print(vec2)\n",
"\n",
"vec * vec2"
]
},
{
"cell_type": "markdown",
"id": "7732da7b",
"metadata": {},
"source": [
"## Минимальное кол-во измеренений"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "721daf56",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1, 2]])"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Сам допаковывает переданный массив в нужное кол-во измерений\n",
"\n",
"vec = np.array([1, 2], ndmin=2)\n",
"vec"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "eae8552a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[ 1 2 3]\n",
" [10 12 32]]\n"
]
}
],
"source": [
"array = [[1, 2, 3], [10, 12, 32]]\n",
"array = np.array(array, ndmin=2)\n",
"print(array)"
]
},
{
"cell_type": "markdown",
"id": "3fd29be4",
"metadata": {},
"source": [
"## Доступ по индексам"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "2d139f31",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([12, 14, 34])"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array[0, 1] + array[1, :]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "927cc500",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([10, 12])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"array[1, -3:-1]"
]
},
{
"cell_type": "markdown",
"id": "05a861bf",
"metadata": {},
"source": [
"## Типы данных\n",
"* Накладываются на весь массив\n",
"* Базовые типы:\n",
" * u - безнаковые числа\n",
" * i - целые числа\n",
" * f - дробные числа\n",
" * c - комплексные дробные числа\n",
" * m - timedelta\n",
" * M - datetime\n",
" * S - строки\n",
" * V - void, просто определенные куски памяти, выделенные под хранение\n",
" * U - строки unicode\n",
" * O - object, смешанный тип"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "43bcb180",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"int64 (3,) 1\n",
"float64 (3,) 1\n",
"int64 (3,) 1\n",
"object (3,) 1\n"
]
}
],
"source": [
"array = [1, 2, 3]\n",
"\n",
"numpy_array = np.array(array)\n",
"print(numpy_array.dtype, numpy_array.shape, numpy_array.ndim)\n",
"\n",
"\n",
"array = [1, 2, 3.]\n",
"\n",
"numpy_array = np.array(array)\n",
"print(numpy_array.dtype, numpy_array.shape, numpy_array.ndim)\n",
"\n",
"numpy_array = np.array(array, dtype=int)\n",
"print(numpy_array.dtype, numpy_array.shape, numpy_array.ndim)\n",
"\n",
"\n",
"\n",
"class Test:\n",
" def __init__(self): pass\n",
"\n",
"array = [Test(), True, 3]\n",
"\n",
"numpy_array = np.array(array, dtype=object)\n",
"print(numpy_array.dtype, numpy_array.shape, numpy_array.ndim)"
]
},
{
"cell_type": "markdown",
"id": "8c12e579",
"metadata": {},
"source": [
"Конвертация numpy массива в другой numpy массив"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e6f6728c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-1.2 2.5 3.1] float64\n",
"[-1 2 3] int16\n"
]
}
],
"source": [
"array: np.ndarray = np.array([-1.2, 2.5, 3.1])\n",
"print(array, array.dtype)\n",
"\n",
"new_array = np.ndarray = array.astype(\"i2\")\n",
"print(new_array, new_array.dtype)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}