2-Amaliy mashg`ulot.
1. Python muhitida Pandas, Keras, Tensorflow, kutubxonalaridan foydalangan holda jadval ko’rinishida berilgan ma’lumotlarni klassifikatsiya qilish.
2. Logistik regressiya tushunchasi. Xatoliklarni baholash. Klassifikatsiyalashni amalga oshirish.
3. Eng soda perseptron yordamida hisoblashlarni amalga oshirish.
1. Python muhitida Pandas, Keras, Tensorflow, kutubxonalaridan foydalangan holda jadval ko’rinishida berilgan ma’lumotlarni klassifikatsiya qilish.
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical
# Load the Iris dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
column_names = ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'class']
iris_data = pd.read_csv(url, names=column_names)
# Split the dataset into features and labels
X = iris_data.iloc[:, 0:4].values
y = iris_data.iloc[:, 4].values
# Encode the categorical labels
encoder = LabelEncoder()
y = encoder.fit_transform(y)
y = to_categorical(y)
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Create a Keras model
model = Sequential()
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(3, activation='softmax'))
# Compile the model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train the model
model.fit(X_train, y_train, epochs=150, batch_size=10, verbose=0)
# Evaluate the model
_, accuracy = model.evaluate(X_test, y_test, verbose=0)
print(f"Accuracy: {accuracy * 100:.2f}%")
2. Logistik regressiy tushunchasi. Xatoliklarni baholash. Klassifikatsiyalashni amalga oshirish
import numpy as np
X = np.array([[2.7810836,2.550537003],
[1.465489372,2.362125076],
[3.396561688,4.400293529],
[1.38807019,1.850220317],
[3.06407232,3.005305973],
[7.627531214,2.759262235],
[5.332441248,2.088626775],
[6.922596716,1.77106367 ],
[8.675418651,-0.242068655],
[7.673756466,3.508563011]])
y = np.array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1])
def sigmoid(x):
return 1.0 / (1.0 + np.exp(-x))
def predict(X, weights):
X = np.insert(X, 0, 1, axis=1)
y_hat = sigmoid(np.dot(X, weights))
y_hat[y_hat >= 0.5] = 1
y_hat[y_hat < 0.5] = 0
return y_hat
def train(X, y, alpha=0.1, epochs=100):
# To'plamni maxsus holatga keltirish
X = np.insert(X, 0, 1, axis=1)
# Boshlanish vazni
weights = np.zeros(X.shape[1])
# Gradientni hisoblash
for epoch in range(epochs):
y_hat = sigmoid(np.dot(X, weights))
error = y - y_hat
gradient = alpha * np.dot(X.T, error)
weights += gradient
return weights
# Modelni o'qitish
weights = train(X, y)
# Yangi ma'lumotlar uchun natijalarni hisoblash
new_data = np.array([[3.123456789, 4.567890123], [5.678901234, 2.345678901]])
predictions = predict(new_data, weights)
print(predictions)
3. Eng soda perseptron yordamida hisoblashlarni amalga oshirish.
Do'stlaringiz bilan baham: |