-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
73 lines (62 loc) · 2.48 KB
/
main.py
File metadata and controls
73 lines (62 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score
# 1. Rastgele bir tohum ayarlayarak sonuçların tekrarlanabilir olmasını sağlıyoruz
np.random.seed(42)
# 2. Yapay veri seti oluşturma
class_0 = np.random.multivariate_normal(mean=[2, 2],
cov=[[1, 0.5], [0.5, 1]],
size=100)
class_1 = np.random.multivariate_normal(mean=[4, 4],
cov=[[1, -0.5], [-0.5, 1]],
size=100)
data = np.vstack((class_0, class_1))
labels = np.hstack((np.zeros(100), np.ones(100)))
# 3. Veri setini DataFrame'e dönüştürme
df = pd.DataFrame(data, columns=['x1', 'x2'])
df['label'] = labels
print(df.head())
# 4. Veri görselleştirme
plt.figure(figsize=(8,6))
sns.scatterplot(x='x1', y='x2', hue='label', data=df, palette='viridis')
plt.title('Yapay Veri Seti Dağılımı')
plt.show()
# 5. Veri setini eğitim ve test olarak bölme
X = df[['x1', 'x2']]
y = df['label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
print(f"Eğitim seti boyutu: {X_train.shape}")
print(f"Test seti boyutu: {X_test.shape}")
# 6. Lojistik regresyon modelini oluşturma ve eğitme
model = LogisticRegression()
model.fit(X_train, y_train)
# 7. Modelin değerlendirilmesi
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
print(f"Doğruluk Oranı: {accuracy:.2f}")
print("\nSınıflandırma Raporu:")
print(classification_report(y_test, y_pred))
cm = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(6,4))
sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
plt.xlabel('Tahmin Edilen Etiket')
plt.ylabel('Gerçek Etiket')
plt.title('Confusion Matrix')
plt.show()
# 8. Karar sınırını görselleştirme
h = 0.02
x_min, x_max = X['x1'].min() - 1, X['x1'].max() + 1
y_min, y_max = X['x2'].min() - 1, X['x2'].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.figure(figsize=(8,6))
plt.contourf(xx, yy, Z, alpha=0.3, cmap='viridis')
sns.scatterplot(x='x1', y='x2', hue='label', data=df, palette='viridis', edgecolor='k')
plt.title('Lojistik Regresyon Karar Sınırı')
plt.show()