23 Oct, 15:24
import tensorflow as tf
from tensorflow.keras import layers, models
# Load the MNIST dataset (images of digits)
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Normalize the data
x_train, x_test = x_train / 255.0, x_test / 255.0
# Build the model using CNN
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# Compile the model
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# Train the model
model.fit(x_train, y_train, epochs=5)
# Test the model
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"Model accuracy on test set: {test_acc}")
23 Oct, 15:24
import tensorflow as tf
from tensorflow.keras import layers, models
# تحميل مجموعة بيانات MNIST (صور الأرقام)
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# تطبيع البيانات
x_train, x_test = x_train / 255.0, x_test / 255.0
# بناء النموذج باستخدام CNN
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# تجميع النموذج
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# تدريب النموذج
model.fit(x_train, y_train, epochs=5)
# اختبار النموذج
test_loss, test_acc = model.evaluate(x_test, y_test)
print(f"دقة النموذج على مجموعة الاختبار: {test_acc}")
20 Oct, 17:50
20 Oct, 17:49
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
# Load the pre-trained model
model = load_model('model.h5')
# Load the image for classification
img = image.load_img('test_image.jpg', target_size=(150, 150))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
# Predict the class of the image
prediction = model.predict(img_array)
print(f"Model Prediction: {prediction}")
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import numpy as np
# Example sales data
sales_data = np.array([[1, 100], [2, 150], [3, 200], [4, 250]]) # month and sales
X = sales_data[:, 0].reshape(-1, 1) # input: month
y = sales_data[:, 1] # output: sales
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# Create Linear Regression model
model = LinearRegression()
model.fit(X_train, y_train)
# Predict sales for the fifth month
predicted_sales = model.predict([[5]])
print(f"Predicted sales for month 5: {predicted_sales}")
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
# Training data (regular and spam emails)
emails = ["Congratulations, you've won a prize!", "Meeting tomorrow at 10am", "Cheap products available now"]
labels = [1, 0, 1] # 1 means spam, 0 means regular email
# Convert texts to numerical data
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(emails)
# Train the model
model = MultinomialNB()
model.fit(X, labels)
# Test the model
test_email = vectorizer.transform(["Win a free vacation now!"])
prediction = model.predict(test_email)
print(f"Email Prediction: {'Spam' if prediction[0] == 1 else 'Regular email'}")
20 Oct, 17:49
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
# تحميل النموذج المدرب
model = load_model('model.h5')
# تحميل الصورة التي نريد تصنيفها
img = image.load_img('test_image.jpg', target_size=(150, 150))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
# توقع فئة الصورة
prediction = model.predict(img_array)
print(f"توقع النموذج: {prediction}")
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import numpy as np
# بيانات المبيعات (مثال)
sales_data = np.array([[1, 100], [2, 150], [3, 200], [4, 250]]) # الشهر والمبيعات
X = sales_data[:, 0].reshape(-1, 1) # المدخلات: الشهر
y = sales_data[:, 1] # المخرجات: المبيعات
# تقسيم البيانات
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# إنشاء نموذج الانحدار الخطي
model = LinearRegression()
model.fit(X_train, y_train)
# التنبؤ بالمبيعات للشهر الخامس
predicted_sales = model.predict([[5]])
print(f"توقع المبيعات للشهر الخامس: {predicted_sales}")
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
# بيانات التدريب (رسائل عادية ورسائل سبام)
emails = ["Congratulations, you've won a prize!", "Meeting tomorrow at 10am", "Cheap products available now"]
labels = [1, 0, 1] # 1 تعني سبام، 0 تعني رسالة عادية
# تحويل النصوص إلى بيانات رقمية
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(emails)
# تدريب النموذج
model = MultinomialNB()
model.fit(X, labels)
# اختبار النموذج
test_email = vectorizer.transform(["Win a free vacation now!"])
prediction = model.predict(test_email)
print(f"توقع الرسالة: {'سبام' if prediction[0] == 1 else 'رسالة عادية'}")
20 Oct, 14:29
18 Oct, 14:34
b = "Hello, World!"
print(b[-5:-2]) # النتيجة: "orl"
x = 'Welcome'
print(x[3:5])
upper()
بتحويل جميع الأحرف في النص إلى أحرف كبيرة.a = "Hello, World!"
print(a.upper()) # النتيجة: "HELLO, WORLD!"
lower()
بتحويل جميع الأحرف في النص إلى أحرف صغيرة.a = "Hello, World!"
print(a.lower()) # النتيجة: "hello, world!"
strip()
بإزالة أي مسافات زائدة من بداية أو نهاية النص.a = " Hello, World! "
print(a.strip()) # النتيجة: "Hello, World!"
replace()
باستبدال جزء من النص بآخر.a = "Hello, World!"
print(a.replace("H", "J")) # النتيجة: "Jello, World!"
split()
بتقسيم النص إلى قائمة من النصوص الفرعية بناءً على فاصل محدد (في هذا المثال هو الفاصلة).a = "Hello, World!"
print(a.split(",")) # النتيجة: ['Hello', ' World!']
a
مع المتغير b
في المتغير c
:a = "Hello"
b = "World"
c = a + b
print(c) # النتيجة: "HelloWorld"
a
و b
، قم بإضافة مسافة بينهما:a = "Hello"
b = "World"
c = a + " " + b
print(c) # النتيجة: "Hello World"
x
و y
في z
؟18 Oct, 14:34
+
operator.b = "Hello, World!"
print(b[:5]) # النتيجة: "Hello"
b = "Hello, World!"
print(b[2:]) # النتيجة: "llo, World!"
18 Oct, 09:53
17 Oct, 16:05
17 Oct, 12:32
print("مرحباً")
print('مرحباً')
print("إنه بخير")
print('يُدعى "جوني"')
=
):a = "مرحباً"
print(a)
"""
أو '''
).a = """هذا مثال
على سلسلة نصية متعددة الأسطر"""
print(a)
---
### السلاسل النصية كمصفوفات
في بايثون، السلاسل النصية هي مصفوفات من الأحرف. يمكنك الوصول إلى كل حرف باستخدام الأقواس المربعة `[]`.
#### التكرار عبر السلاسل النصية
بما أن السلاسل النصية هي مصفوفات، يمكنك التكرار عبرها باستخدام حلقة `for`.
#### طول السلسلة النصية
للحصول على طول سلسلة نصية، استخدم دالة `len()`.
---
### التحقق من وجود سلسلة فرعية
يمكنك التحقق مما إذا كانت سلسلة فرعية موجودة داخل سلسلة نصية باستخدام الكلمة المفتاحية `in`.
يمكنك أيضًا استخدامه داخل جملة `if`:
`
16 Oct, 16:29