AI ۋە ماشىنا ئۆگىنىشنىڭ ئاساسى بولغان Python كۈتۈپخانىلىرىنىڭ تولۇق كود مىسالى، نامۇنا ۋە ئىشلىتىش ئۇسۇللىرى — تەپسىلىي ئۇيغۇرچە چۈشەندۈرمە بىلەن
import numpy as np # ══ 1. ndarray قۇرۇش ══════════════════════════════════ a = np.array([1, 2, 3, 4, 5]) # 1D ئامبال b = np.array([[1,2,3],[4,5,6]]) # 2D ماترىتسا c = np.zeros((3,4)) # 3×4 نۆل ماترىتسا d = np.ones((2,3)) # 2×3 بىرلىك ماترىتسا e = np.eye(4) # 4×4 كىملىك ماترىتسا f = np.random.randn(3,4) # نورمال تاساددۇپىي g = np.arange(0, 10, 0.5) # 0 دىن 10 غا 0.5 قەدەم h = np.linspace(0, 1, 100) # 0-1 ئارىسىدا 100 نومۇر # ══ 2. شەكىل ۋە تۈر ══════════════════════════════════ print(b.shape) # (2, 3) print(b.dtype) # int64 print(b.ndim) # 2 print(b.size) # 6 (ئومۇمىي ئېلېمېنت) b_f = b.astype(np.float32) # تۈر ئالماشتۇرۇش b_r = b.reshape(3, 2) # شەكىل ئۆزگەرتىش b_t = b.T # ئالماشتۇرۇش (transpose) b_f2 = b.flatten() # 1D غا تارىلىش # ══ 3. سانلىق مەلۇمات ئالىش (Indexing / Slicing) ═════ x = np.array([[10,20,30],[40,50,60],[70,80,90]]) print(x[1, 2]) # 60 — 2-قۇر، 3-ئىستون print(x[0, :]) # [10 20 30] — 1-قۇر print(x[:, 1]) # [20 50 80] — 2-ئىستون print(x[0:2, 1:]) # [[20 30][50 60]] print(x[x > 50]) # [60 70 80 90] — شەرت ئاساسلىق # ══ 4. ماتېماتىك مەشغۇلاتلار ════════════════════════ a = np.array([1,2,3]) b = np.array([4,5,6]) a + b # [5 7 9] — ئېلېمېنت ئاساسلىق a * b # [4 10 18] a ** 2 # [1 4 9] np.dot(a, b) # 32 — نوقتا كۆپەيتىش np.cross(a, b) # چاتىشما كۆپەيتىش # ماترىتسا كۆپەيتىش A = np.random.rand(3,4) B = np.random.rand(4,2) C = np.matmul(A, B) # ياكى A @ B → (3,2) # ══ 5. ستاتىستىكا ════════════════════════════════════ data = np.random.randn(1000) print(data.mean()) # ئوتتۇرا print(data.std()) # ئۆلچەملىك بۇرۇش print(data.min(), data.max()) print(np.percentile(data, [25, 50, 75])) # كۋارتىل print(np.corrcoef(data[:500], data[500:])) # مۇناسىبەت # ══ 6. Broadcasting ══════════════════════════════════ matrix = np.ones((3,4)) row = np.array([1,2,3,4]) result = matrix + row # (3,4) + (4,) → (3,4) Broadcasting # ══ 7. AI دا ئىشلىتىش ════════════════════════════════ def sigmoid(x): return 1 / (1 + np.exp(-x)) def relu(x): return np.maximum(0, x) def softmax(x): e_x = np.exp(x - x.max()) return e_x / e_x.sum() def cosine_similarity(a, b): return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
import pandas as pd import numpy as np # ══ 1. DataFrame قۇرۇش ═══════════════════════════════ df = pd.DataFrame({ “ئىسمى”: [“ئىدراك”, “Dilnoza”, “Ablat”, “Mayram”], “ياشى”: [28, 25, 32, 22], “شەھەر”: [“ئۈرۈمچى”, “قەشقەر”, “ئۈرۈمچى”, “خوتەن”], “مائاش”: [8500, 7200, 9100, 6800], }) # ══ 2. ئاساسلىق كۆزىتىش ═════════════════════════════ print(df.head(3)) # تۇنجى 3 قۇر print(df.tail(2)) # ئاخىرقى 2 قۇر print(df.info()) # تۈر، بوش ئورۇن print(df.describe()) # ستاتىستىكا خۇلاسىسى print(df.shape) # (4, 4) print(df.columns) # ئىستون ئىسملىرى print(df.dtypes) # سانلىق مەلۇمات تۈرلىرى # ══ 3. سانلىق مەلۇمات تاللاش ═══════════════════════ df[“ياشى”] # ئىستون df[[“ئىسمى”, “مائاش”]] # كۆپ ئىستون df.iloc[0] # ئىندېكس بويىچە قۇر df.loc[1, “ئىسمى”] # قۇر+ئىستون df[df[“ياشى”] > 25] # شەرتلىك تاللاش df[df[“شەھەر”] == “ئۈرۈمچى”] # ئۈرۈمچى كىشىلەر df.query(“مائاش > 8000 and ياشى < 30”) # query ئۇسۇلى # ══ 4. سانلىق مەلۇمات تازىلاش ══════════════════════ df2 = df.copy() df2[“مائاش”].fillna(df2[“مائاش”].mean(), inplace=True) # NaN تولدۇرۇش df2.dropna(inplace=True) # NaN قۇرلارنى ئۆچۈر df2.drop_duplicates(inplace=True) # تەكرارلارنى ئۆچۈر df2.rename(columns={“ياشى”: “age”}, inplace=True) # ئىسىم ئۆزگەرت # ══ 5. يىغىش ۋە گۇرۇپپىلاش ══════════════════════════ df.groupby(“شەھەر”)[“مائاش”].mean() # شەھەر بويىچە ئوتتۇرا مائاش df.groupby(“شەھەر”).agg({ # كۆپ ئۆلچەم “مائاش”: [“mean”, “max”, “count”], “ياشى”: “mean” }) df.pivot_table(values=“مائاش”, index=“شەھەر”, aggfunc=“mean”) # ══ 6. يېڭى ئىستون قوشۇش ════════════════════════════ df[“مائاش_نورمال”] = (df[“مائاش”] - df[“مائاش”].min()) / (df[“مائاش”].max() - df[“مائاش”].min()) df[“كاتېگورى”] = pd.cut(df[“مائاش”], bins=[0,7000,9000,float(“inf”)], labels=[“تۆۋەن”, “ئوتتۇرا”, “يۇقىرى”] ) # ══ 7. ھۆججەت ئوقۇش/يېزىش ═══════════════════════════ df = pd.read_csv(“data.csv”, encoding=“utf-8”) df = pd.read_excel(“data.xlsx”, sheet_name=“Sheet1”) df = pd.read_json(“data.json”) df.to_csv(“output.csv”, index=False) df.to_excel(“output.xlsx”, index=False)
import matplotlib.pyplot as plt import seaborn as sns import numpy as np # ══ 1. ئاساسلىق سىزىق گرافىك ════════════════════════ x = np.linspace(0, 2*np.pi, 100) plt.figure(figsize=(10, 4)) plt.plot(x, np.sin(x), label=“sin(x)”, color=“blue”, linewidth=2) plt.plot(x, np.cos(x), label=“cos(x)”, color=“red”, linestyle=”–”) plt.xlabel(“x قىممىتى”); plt.ylabel(“y قىممىتى”) plt.title(“sin ۋە cos گرافىكى”) plt.legend(); plt.grid(True) plt.savefig(“sin_cos.png”, dpi=150) plt.show() # ══ 2. Subplot — كۆپ گرافىك ══════════════════════════ fig, axes = plt.subplots(2, 2, figsize=(12, 8)) data = np.random.randn(500) axes[0,0].hist(data, bins=30, color=“steelblue”) axes[0,0].set_title(“تارقىلىش گرافىكى”) axes[0,1].scatter(np.random.rand(100), np.random.rand(100), alpha=0.6, c=“coral”) axes[0,1].set_title(“نوقتا گرافىكى”) cats = [“ئۈرۈمچى”, “قەشقەر”, “خوتەن”] values = [4500, 2800, 1900] axes[1,0].bar(cats, values, color=[”#3B6FE8”, ”#EE4C2C”, ”#16A34A”]) axes[1,0].set_title(“تىك بالداق گرافىكى”) axes[1,1].boxplot([np.random.randn(100) for _ in range(4)]) axes[1,1].set_title(“صندوق گرافىكى”) plt.tight_layout() plt.show() # ══ 3. Seaborn — گۈزەل گرافىكلار ════════════════════ sns.set_theme(style=“whitegrid”) df = sns.load_dataset(“tips”) # مۇناسىۋەت ماترىتساسى plt.figure(figsize=(8,6)) sns.heatmap(df.corr(numeric_only=True), annot=True, cmap=“coolwarm”) plt.title(“مۇناسىۋەت ماترىتساسى”) plt.show() sns.pairplot(df, hue=“sex”) # ئومۇمىي مۇناسىۋەت sns.violinplot(x=“day”, y=“total_bill”, data=df)
from sklearn import datasets, preprocessing, metrics from sklearn.model_selection import train_test_split, cross_val_score, GridSearchCV from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler, LabelEncoder, OneHotEncoder from sklearn.linear_model import LogisticRegression, LinearRegression from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier from sklearn.svm import SVC from sklearn.neighbors import KNeighborsClassifier from sklearn.tree import DecisionTreeClassifier from sklearn.cluster import KMeans import numpy as np # ══ 1. سانلىق مەلۇمات تەييارلاش ═════════════════════ iris = datasets.load_iris() X, y = iris.data, iris.target X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=42, stratify=y ) # ══ 2. Pipeline قۇرۇش ════════════════════════════════ pipe = Pipeline([ (“scaler”, StandardScaler()), (“model”, RandomForestClassifier(n_estimators=100, random_state=42)) ]) pipe.fit(X_train, y_train) y_pred = pipe.predict(X_test) # ══ 3. باھالاش ═══════════════════════════════════════ print(f”Accuracy: {metrics.accuracy_score(y_test, y_pred):.4f}”) print(f”F1-Score: {metrics.f1_score(y_test, y_pred, average=‘weighted’):.4f}”) print(metrics.classification_report(y_test, y_pred, target_names=iris.target_names)) # Confusion Matrix cm = metrics.confusion_matrix(y_test, y_pred) # ══ 4. Cross-Validation ══════════════════════════════ cv_scores = cross_val_score(pipe, X, y, cv=5, scoring=“accuracy”) print(f”CV Mean: {cv_scores.mean():.4f} ± {cv_scores.std():.4f}”) # ══ 5. GridSearchCV — ئەڭ ياخشى پارامېتر ════════════ param_grid = { “model__n_estimators”: [50, 100, 200], “model__max_depth”: [3, 5, None], } grid = GridSearchCV(pipe, param_grid, cv=5, n_jobs=-1, verbose=1) grid.fit(X_train, y_train) print(f”ئەڭ ياخشى: {grid.best_params_}”) # ══ 6. مودېل ساقلاش ══════════════════════════════════ import joblib joblib.dump(grid.best_estimator_, “best_model.pkl”) loaded = joblib.load(“best_model.pkl”) # ══ 7. K-Means توپلاشتۇرۇش ══════════════════════════ km = KMeans(n_clusters=3, random_state=42, n_init=10) km.fit(X) print(f”توپ بەلگىلەر: {km.labels_[:10]}”) print(f”توپ مەركەزلىرى:\n{km.cluster_centers_}”)
import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import Dataset, DataLoader import torch.nn.functional as F # ══ 1. Tensor ئاساسلىرى ══════════════════════════════ x = torch.tensor([[1.0, 2.0],[3.0, 4.0]]) y = torch.zeros(3, 4) z = torch.randn(3, 4) # GPU غا يوللاش device = “cuda” if torch.cuda.is_available() else “mps” if torch.backends.mps.is_available() else “cpu” x = x.to(device) print(f”ئىشلىتىلىدىغان: {device}”) # ══ 2. Autograd (ئاپتوماتىك گرادىيېنت) ══════════════ w = torch.tensor(2.0, requires_grad=True) b = torch.tensor(1.0, requires_grad=True) x = torch.tensor(3.0) y = w * x + b # y = 7.0 y.backward() # گرادىيېنت ھىسابلاش print(w.grad) # dy/dw = 3.0 print(b.grad) # dy/db = 1.0 # ══ 3. نېيرون تارماق ئانىقلاش ════════════════════════ class TextClassifier(nn.Module): def **init**(self, input_dim, hidden_dim, num_classes): super().**init**() self.layers = nn.Sequential( nn.Linear(input_dim, hidden_dim), nn.BatchNorm1d(hidden_dim), nn.ReLU(), nn.Dropout(0.3), nn.Linear(hidden_dim, hidden_dim // 2), nn.ReLU(), nn.Linear(hidden_dim // 2, num_classes) ) ``` def forward(self, x): return self.layers(x) ``` model = TextClassifier(768, 256, 3).to(device) print(model) # ══ 4. تەربىيىلەش تىزىمى ════════════════════════════ criterion = nn.CrossEntropyLoss() optimizer = optim.AdamW(model.parameters(), lr=1e-3, weight_decay=0.01) scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=50) def train_epoch(model, loader, optimizer, criterion): model.train() total_loss, correct = 0, 0 ``` for X_batch, y_batch in loader: X_batch, y_batch = X_batch.to(device), y_batch.to(device) optimizer.zero_grad() outputs = model(X_batch) loss = criterion(outputs, y_batch) loss.backward() torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0) # Gradient clipping optimizer.step() total_loss += loss.item() correct += (outputs.argmax(1) == y_batch).sum().item() return total_loss / len(loader), correct ``` def eval_epoch(model, loader, criterion): model.eval() total_loss, correct = 0, 0 with torch.no_grad(): for X_batch, y_batch in loader: X_batch, y_batch = X_batch.to(device), y_batch.to(device) outputs = model(X_batch) total_loss += criterion(outputs, y_batch).item() correct += (outputs.argmax(1) == y_batch).sum().item() return total_loss / len(loader), correct # ── تەربىيىلەش دەۋرى ──────────────────────────────── EPOCHS = 50 for epoch in range(EPOCHS): tr_loss, tr_acc = train_epoch(model, train_loader, optimizer, criterion) va_loss, va_acc = eval_epoch(model, val_loader, criterion) scheduler.step() if epoch % 10 == 0: print(f”[{epoch:3d}] Loss: {tr_loss:.4f} | Val Loss: {va_loss:.4f}”) # ── مودېل ساقلاش / يوللاش ────────────────────────── torch.save(model.state_dict(), “model.pt”) model.load_state_dict(torch.load(“model.pt”, map_location=device))
import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers, callbacks print(tf.**version**) print(f”GPU: {tf.config.list_physical_devices(‘GPU’)}”) # ══ 1. ئاددىي Keras مودېلى ═══════════════════════════ model = keras.Sequential([ layers.Dense(256, activation=“relu”, input_shape=(784,)), layers.BatchNormalization(), layers.Dropout(0.3), layers.Dense(128, activation=“relu”), layers.Dropout(0.2), layers.Dense(10, activation=“softmax”) ]) model.compile( optimizer = keras.optimizers.AdamW(learning_rate=1e-3), loss = “sparse_categorical_crossentropy”, metrics = [“accuracy”] ) model.summary() # ══ 2. Functional API (مۇرەككەپ ئارخىتىكتۇرا) ═══════ inputs = keras.Input(shape=(784,)) x = layers.Dense(256, activation=“relu”)(inputs) x = layers.BatchNormalization()(x) skip = layers.Dense(64)(inputs) # Skip connection x = layers.Dense(64, activation=“relu”)(x) merged = layers.Add()([x, skip]) outputs = layers.Dense(10, activation=“softmax”)(merged) func_model = keras.Model(inputs, outputs) # ══ 3. Callbacks — تەربىيىلەش كونترولى ══════════════ cb_list = [ callbacks.EarlyStopping( # ئاشىرا ئۆگىنىشتا توختا monitor = “val_loss”, patience = 10, restore_best_weights = True ), callbacks.ModelCheckpoint( # ئەڭ ياخشى مودېلنى ساقلا “best_model.keras”, save_best_only = True, monitor = “val_accuracy” ), callbacks.ReduceLROnPlateau( # ئۆگىنىش سۈرئىتىنى ئازايت monitor = “val_loss”, factor = 0.5, patience = 5 ), callbacks.TensorBoard( # كۆزىتىش تاختىسى log_dir = ”./logs” ), ] history = model.fit( X_train, y_train, epochs = 100, batch_size = 32, validation_split = 0.2, callbacks = cb_list, ) # ══ 4. Transfer Learning ═════════════════════════════ base = keras.applications.EfficientNetB0( weights = “imagenet”, include_top = False, input_shape = (224, 224, 3) ) base.trainable = False # ئاساسلىق مودېلنى بەكىت x = layers.GlobalAveragePooling2D()(base.output) x = layers.Dense(256, activation=“relu”)(x) out = layers.Dense(5, activation=“softmax”)(x) transfer_model = keras.Model(base.input, out)
# ── ئۇيغۇرچە تىل تۈرلەش تولۇق مىسالى ───────────────── import numpy as np import torch import torch.nn as nn from transformers import AutoTokenizer, AutoModel from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report # ── 1. سانلىق مەلۇمات تەييارلاش ──────────────────────── texts = [ “بۇ مەھسۇلات ناھايىتى ياخشى”, # مۇسبەت “خىزمەت ناچار ئىدى”, # مەنپى “ئوتتۇراچە بىر تەجرىبە”, # بىتەرەپ ] labels = [2, 0, 1] # 0=مەنپى، 1=بىتەرەپ، 2=مۇسبەت X_train, X_test, y_train, y_test = train_test_split( texts, labels, test_size=0.2, random_state=42 ) # ── 2. Tokenizer + Embedding ───────────────────────────── tokenizer = AutoTokenizer.from_pretrained(“bert-base-multilingual-cased”) bert = AutoModel.from_pretrained(“bert-base-multilingual-cased”) def encode_texts(texts, max_len=128): encoded = tokenizer( texts, padding=True, truncation=True, max_length=max_len, return_tensors=“pt” ) with torch.no_grad(): outputs = bert(**encoded) return outputs.last_hidden_state[:, 0, :] # CLS token X_train_emb = encode_texts(X_train) # (N, 768) X_test_emb = encode_texts(X_test) # ── 3. Sklearn بىلەن تۈرلەش ───────────────────────────── from sklearn.linear_model import LogisticRegression clf = LogisticRegression(max_iter=1000) clf.fit(X_train_emb.numpy(), y_train) y_pred = clf.predict(X_test_emb.numpy()) print(classification_report(y_test, y_pred, target_names=[“مەنپى”, “بىتەرەپ”, “مۇسبەت”])) # ── 4. يايدۇرۇش ───────────────────────────────────────── import joblib joblib.dump(clf, “sentiment_clf.pkl”) def predict_sentiment(text: str) -> str: emb = encode_texts([text]) pred = clf.predict(emb.numpy())[0] return [“مەنپى”, “بىتەرەپ”, “مۇسبەت”][pred] print(predict_sentiment(“بۇ كىتاب ناھايىتى پايدىلىق ئىدى”)) # → “مۇسبەت”
# ── ئاساسلىق AI توپلىمى ───────────────────────────────── pip install numpy pandas matplotlib seaborn scikit-learn # ── PyTorch (CUDA 12.1) ────────────────────────────────── pip install torch torchvision torchaudio –index-url https://download.pytorch.org/whl/cu121 # ── PyTorch (CPU فەقەت / Mac) ──────────────────────────── pip install torch torchvision torchaudio # ── TensorFlow ─────────────────────────────────────────── pip install tensorflow # Linux/Windows GPU pip install tensorflow-macos # Mac pip install tensorflow-metal # Mac GPU # ── Hugging Face ───────────────────────────────────────── pip install transformers datasets accelerate peft trl # ── يۆتكەلمە ئاپپارات تەكشۈرۈش ───────────────────────── python -c “import torch; print(torch.cuda.is_available())” python -c “import torch; print(torch.backends.mps.is_available())” python -c “import tensorflow as tf; print(tf.config.list_physical_devices())”
| جەھەت | 🔥 PyTorch | 🌊 TensorFlow/Keras | تەۋسىيە |
|---|---|---|---|
| ئىشلىتىش ئاسانلىقى | Python ئۇسلۇبىدا | Keras ئارقىلىق ئاسان | ئىككىسىمۇ ياخشى |
| تەتقىقات | ئامماۋى — 70%+ | كەمىيىدى | PyTorch |
| Production | TorchServe، ONNX | TFServing، TFLite | TensorFlow |
| موبىل يايدۇرۇش | TorchScript | TFLite، TF.js | TensorFlow |
| Dynamic Graph | ✅ ئاساسلىق | ✅ tf.function | PyTorch |
| Debugging | ئاسان (Python) | قىيىنرەق | PyTorch |
| Hugging Face | ✅ ئاساسلىق | ✅ قوللانغان | PyTorch |
| GPU ئىشلىتىش | .to(device) | ئاپتوماتىك | TensorFlow |
| ئۇسۇل | ئۇيغۇرچە | مىسال |
|---|---|---|
| np.array() | ئامبال قۇرۇش | np.array([1,2,3]) → ndarray |
| np.zeros/ones() | نۆل/بىرلىك | np.zeros((3,4)) → 3×4 نۆل |
| np.reshape() | شەكىل ئۆزگەرتىش | (12,) → (3,4) |
| np.dot() / @ | ماترىتسا كۆپەيتىش | A @ B → ماترىتسا نەتىجىسى |
| np.mean/std() | ئوتتۇرا/بۇرۇش | axis=0 قۇر، axis=1 ئىستون |
| np.argmax() | ئەڭ چوڭ ئىندىكس | Softmax چىقىمىنى تۈرلەش |
| np.concatenate() | بىرلەشتۈرۈش | axis=0 قۇر، axis=1 ئىستون |
| np.random.randn() | نورمال تاساددۇپىي | Weight تەشەببۇسلاش ئۈچۈن |