RubieeTariq commited on
Commit
e369ba4
·
verified ·
1 Parent(s): a647f03

train and test it

Browse files
Files changed (1) hide show
  1. app.py +15 -41
app.py CHANGED
@@ -1,48 +1,22 @@
1
  import numpy as np
2
- from sklearn.datasets import load_iris
3
- from sklearn.model_selection import train_test_split
4
  from sklearn.linear_model import LogisticRegression
5
- from sklearn.metrics import accuracy_score
6
 
7
- # Load the Iris dataset
8
- iris = load_iris()
 
 
9
 
10
- # Split the dataset into features (X) and target (y)
11
- X = iris.data
12
- y = iris.target
13
-
14
- # Split the data into training and testing sets
15
- X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
16
-
17
- # Create a Logistic Regression model
18
  model = LogisticRegression()
 
19
 
20
- # Fit the model to the training data
21
- model.fit(X_train, y_train)
22
-
23
- # Make predictions on the test data
24
- y_pred = model.predict(X_test)
25
-
26
- # Calculate the accuracy of the model
27
- accuracy = accuracy_score(y_test, y_pred)
28
-
29
- print(f'Model Accuracy on Test Data: {accuracy*100:.2f}%')
30
-
31
- # Define a function to make predictions based on user input
32
- def predict_iris():
33
- while True:
34
- sepal_length = float(input("Enter Sepal Length: "))
35
- sepal_width = float(input("Enter Sepal Width: "))
36
- petal_length = float(input("Enter Petal Length: "))
37
- petal_width = float(input("Enter Petal Width: "))
38
-
39
- # Convert input to a numpy array
40
- input_data = np.array([sepal_length, sepal_width, petal_length, petal_width]).reshape(1, -1)
41
-
42
- # Make a prediction using the trained model
43
- prediction = model.predict(input_data)
44
-
45
- # Get the name of the predicted class
46
- prediction_name = iris.target_names[prediction[0]]
47
 
48
- print(f'Predicted Iris class: {prediction_name}') # Corrected line
 
 
 
 
 
 
1
  import numpy as np
 
 
2
  from sklearn.linear_model import LogisticRegression
3
+ import matplotlib.pyplot as plt
4
 
5
+ # Generate some sample binary data
6
+ np.random.seed(0)
7
+ X = np.random.randn(100, 1)
8
+ y = np.where(np.dot(X, np.array([0.5, 0.5])) > 0, 1, 0)
9
 
10
+ # Create logistic regression object and fit the model to the data
 
 
 
 
 
 
 
11
  model = LogisticRegression()
12
+ model.fit(X, y)
13
 
14
+ # Predict probabilities
15
+ predictions = model.predict_proba(X)[:,1]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Plot the results
18
+ plt.scatter(X, y)
19
+ plt.plot(X, predictions, color='red', alpha=0.5)
20
+ plt.xlabel('Feature')
21
+ plt.ylabel('Probability')
22
+ plt.show()