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 |
import matplotlib.pyplot as plt import numpy as np from sklearn.datasets import make_blobs # Create 3 clusters of data for classification X, pred_labels = make_blobs(n_samples=300, centers=3, cluster_std=0.8, random_state=42) # Define colors for the 3 predicted classes colors = ['red', 'green', 'blue'] # Plot each class in a different color for class_idx in range(3): plt.scatter(X[pred_labels == class_idx, 0], X[pred_labels == class_idx, 1], color=colors[class_idx], label=f'Class {class_idx}', alpha=0.6) plt.title('3-Class Clustered Data for AI Classification') plt.xlabel('X1') plt.ylabel('X2') plt.legend() plt.grid(True) plt.show() |
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 |
import numpy as np import matplotlib.pyplot as plt # Increase spread from the same centers def generate_cluster(center, num_points, label, spread=1.0): x = np.random.randn(num_points, 2) * spread + center # Increased spread y = np.full((num_points,), label) return x, y # Keep the same centers X1, y1 = generate_cluster(center=[-1, -1], num_points=50, label=0, spread=1.0) X2, y2 = generate_cluster(center=[2, 2], num_points=50, label=1, spread=1.0) X3, y3 = generate_cluster(center=[4, -2], num_points=50, label=2, spread=1.0) # Combine X = np.vstack([X1, X2, X3]) y = np.hstack([y1, y2, y3]) # Plot colors = ['red', 'green', 'blue'] for i in range(3): plt.scatter(X[y == i, 0], X[y == i, 1], color=colors[i], label=f'Class {i}', alpha=0.6) plt.title("Spread-Out Clusters (Same Centers)") plt.xlabel("X1") plt.ylabel("X2") plt.legend() plt.grid(True) plt.show() |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import numpy as np import matplotlib.pyplot as plt import tensorflow as tf from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler # ----------------------------- # Generate synthetic 2D data clustered in 3 classes # ----------------------------- def generate_cluster(center, num_points, label, spread=1.0): x = np.random.randn(num_points, 2) * spread + center # Create points with Gaussian noise around center y = np.full((num_points,), label) # Label all points in this cluster return x, y # Generate 3 clusters with different labels X1, y1 = generate_cluster(center=[-1, -1], num_points=50, label=0, spread=1.0) X2, y2 = generate_cluster(center=[2, 2], num_points=50, label=1, spread=1.0) X3, y3 = generate_cluster(center=[4, -2], num_points=50, label=2, spread=1.0) # Combine all clusters into one dataset X = np.vstack([X1, X2, X3]) y = np.hstack([y1, y2, y3]) # ----------------------------- # Preprocessing # ----------------------------- scaler = StandardScaler() # Normalize inputs to zero-mean and unit-variance X_scaled = scaler.fit_transform(X) # Apply scaling y_cat = tf.keras.utils.to_categorical(y, num_classes=3) # Convert labels to one-hot format # Split data into training and test sets (80% train, 20% test) X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_cat, test_size=0.2, random_state=42) # ----------------------------- # Build ANN Model # ----------------------------- model = tf.keras.Sequential([ tf.keras.layers.Input(shape=(2,)), # Input layer with 2 features (X1 and X2) tf.keras.layers.Dense(32, activation='relu'), # Hidden layer 1 tf.keras.layers.Dense(32, activation='relu'), # Hidden layer 2 tf.keras.layers.Dense(3, activation='softmax') # Output layer with 3 classes ]) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # ----------------------------- # Train Model and store history # ----------------------------- history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=100, verbose=1) # ----------------------------- # Plot Training & Validation Loss # ----------------------------- plt.plot(history.history['loss'], label='Train Loss') plt.plot(history.history['val_loss'], label='Val Loss') plt.title('Training & Validation Loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.legend() plt.grid(True) plt.show() # ----------------------------- # Predict example points from each cluster center # ----------------------------- sample_points = np.array([ [-1, -1], # Expected: Class 0 [2, 2], # Expected: Class 1 [4, -2] # Expected: Class 2 ], dtype=np.float32) # Scale points like training data sample_points_scaled = scaler.transform(sample_points) # Make predictions (probability distribution) predictions = model.predict(sample_points_scaled) # ----------------------------- # Display predictions # ----------------------------- for i, point in enumerate(sample_points): probs = np.round(predictions[i] * 100, 1) # Convert to percent and round pred_class = np.argmax(predictions[i]) # Class with highest probability print(f"Input point: {point}") print(f" → Predicted Class: {pred_class}") probs = predictions[i] * 100 # הכפל ל־אחוזים print(f" → Class Probabilities: Class 0: {probs[0]:.1f}%, Class 1: {probs[1]:.1f}%, Class 2: {probs[2]:.1f}%") |
ניבוי מחיר של בית
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 |
import pandas as pd import numpy as np import tensorflow as tf from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler import matplotlib.pyplot as plt # Load CSV df = pd.read_csv(r'D:\temp40\housing1.csv') # Split features and label X = df.drop(columns='Price') y = df['Price'].values.reshape(-1, 1) # Normalize scaler_X = StandardScaler() scaler_y = StandardScaler() X_scaled = scaler_X.fit_transform(X) y_scaled = scaler_y.fit_transform(y) # Train/test split X_train, X_test, y_train, y_test = train_test_split(X_scaled, y_scaled, test_size=0.2, random_state=42) # Build ANN model = tf.keras.Sequential([ tf.keras.layers.Input(shape=(X.shape[1],)), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(1) # output: price ]) model.compile(optimizer='adam', loss='mse') history = model.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=100, verbose=1) # Predict y_pred_scaled = model.predict(X_test) y_pred = scaler_y.inverse_transform(y_pred_scaled) y_true = scaler_y.inverse_transform(y_test) # Plot prediction plt.scatter(y_true, y_pred, alpha=0.6) plt.plot([min(y_true), max(y_true)], [min(y_true), max(y_true)], 'r--') plt.xlabel("True Price") plt.ylabel("Predicted Price") plt.title("ANN Housing Price Prediction") plt.grid(True) plt.show() # Plot training & validation loss plt.plot(history.history['loss'], label='Train Loss') plt.plot(history.history['val_loss'], label='Validation Loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.title('Training vs Validation Loss') plt.legend() plt.grid(True) plt.show() |