nuppy 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nuppy-0.1.0/PKG-INFO +86 -0
- nuppy-0.1.0/README.md +78 -0
- nuppy-0.1.0/nuppy.egg-info/PKG-INFO +86 -0
- nuppy-0.1.0/nuppy.egg-info/SOURCES.txt +19 -0
- nuppy-0.1.0/nuppy.egg-info/dependency_links.txt +1 -0
- nuppy-0.1.0/nuppy.egg-info/top_level.txt +1 -0
- nuppy-0.1.0/nupy/__init__.py +12 -0
- nuppy-0.1.0/nupy/activation_functions.py +131 -0
- nuppy-0.1.0/nupy/artificial_neural_network.py +98 -0
- nuppy-0.1.0/nupy/decision_tree.py +64 -0
- nuppy-0.1.0/nupy/hierarchical_clustering.py +81 -0
- nuppy-0.1.0/nupy/kmeans.py +80 -0
- nuppy-0.1.0/nupy/knn.py +68 -0
- nuppy-0.1.0/nupy/logistic_regression.py +59 -0
- nuppy-0.1.0/nupy/multiple_linear_regression.py +53 -0
- nuppy-0.1.0/nupy/naive_bayes.py +64 -0
- nuppy-0.1.0/nupy/simple_linear_regression.py +47 -0
- nuppy-0.1.0/nupy/support_vector_machine.py +63 -0
- nuppy-0.1.0/nupy/tensorflow_tensors.py +75 -0
- nuppy-0.1.0/pyproject.toml +13 -0
- nuppy-0.1.0/setup.cfg +4 -0
nuppy-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nuppy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A collection of my Machine Learning practical codes
|
|
5
|
+
Author-email: a-123 <a12964856@gmail.com>
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
|
|
9
|
+
\# nupy
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
A collection of my Machine Learning practical codes written in Python.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
\## About
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
`nupy` is a personal Python library created by \*\*Rutuja Phalke\*\*.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
It contains 12 Machine Learning and Deep Learning practical codes. The library stores the complete practical code and allows users to display the code using simple Python functions.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
\## Included Codes
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
1\. Simple Linear Regression
|
|
34
|
+
|
|
35
|
+
2\. Multiple Linear Regression
|
|
36
|
+
|
|
37
|
+
3\. Support Vector Machine (SVM)
|
|
38
|
+
|
|
39
|
+
4\. Naive Bayes Classifier
|
|
40
|
+
|
|
41
|
+
5\. Logistic Regression
|
|
42
|
+
|
|
43
|
+
6\. K-Nearest Neighbors (KNN)
|
|
44
|
+
|
|
45
|
+
7\. Decision Tree Classifier
|
|
46
|
+
|
|
47
|
+
8\. K-Means Clustering
|
|
48
|
+
|
|
49
|
+
9\. Hierarchical Clustering
|
|
50
|
+
|
|
51
|
+
10\. Activation Functions
|
|
52
|
+
|
|
53
|
+
11\. TensorFlow Tensors and Matrix Operations
|
|
54
|
+
|
|
55
|
+
12\. Artificial Neural Network (ANN)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
\## Example
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
|
|
65
|
+
from nupy import simple\_linear\_regression
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
print(simple\_linear\_regression())
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
The function displays the complete saved practical code as text. It does not execute the Machine Learning code.
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
\## Author
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
\*\*Rutuja Phalke\*\*
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
nuppy-0.1.0/README.md
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
\# nupy
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
A collection of my Machine Learning practical codes written in Python.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
\## About
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
`nupy` is a personal Python library created by \*\*Rutuja Phalke\*\*.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
It contains 12 Machine Learning and Deep Learning practical codes. The library stores the complete practical code and allows users to display the code using simple Python functions.
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
\## Included Codes
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
1\. Simple Linear Regression
|
|
26
|
+
|
|
27
|
+
2\. Multiple Linear Regression
|
|
28
|
+
|
|
29
|
+
3\. Support Vector Machine (SVM)
|
|
30
|
+
|
|
31
|
+
4\. Naive Bayes Classifier
|
|
32
|
+
|
|
33
|
+
5\. Logistic Regression
|
|
34
|
+
|
|
35
|
+
6\. K-Nearest Neighbors (KNN)
|
|
36
|
+
|
|
37
|
+
7\. Decision Tree Classifier
|
|
38
|
+
|
|
39
|
+
8\. K-Means Clustering
|
|
40
|
+
|
|
41
|
+
9\. Hierarchical Clustering
|
|
42
|
+
|
|
43
|
+
10\. Activation Functions
|
|
44
|
+
|
|
45
|
+
11\. TensorFlow Tensors and Matrix Operations
|
|
46
|
+
|
|
47
|
+
12\. Artificial Neural Network (ANN)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
\## Example
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
```python
|
|
56
|
+
|
|
57
|
+
from nupy import simple\_linear\_regression
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
print(simple\_linear\_regression())
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
The function displays the complete saved practical code as text. It does not execute the Machine Learning code.
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
\## Author
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
\*\*Rutuja Phalke\*\*
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nuppy
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A collection of my Machine Learning practical codes
|
|
5
|
+
Author-email: a-123 <a12964856@gmail.com>
|
|
6
|
+
Requires-Python: >=3.8
|
|
7
|
+
Description-Content-Type: text/markdown
|
|
8
|
+
|
|
9
|
+
\# nupy
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
A collection of my Machine Learning practical codes written in Python.
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
\## About
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
`nupy` is a personal Python library created by \*\*Rutuja Phalke\*\*.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
It contains 12 Machine Learning and Deep Learning practical codes. The library stores the complete practical code and allows users to display the code using simple Python functions.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
\## Included Codes
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
1\. Simple Linear Regression
|
|
34
|
+
|
|
35
|
+
2\. Multiple Linear Regression
|
|
36
|
+
|
|
37
|
+
3\. Support Vector Machine (SVM)
|
|
38
|
+
|
|
39
|
+
4\. Naive Bayes Classifier
|
|
40
|
+
|
|
41
|
+
5\. Logistic Regression
|
|
42
|
+
|
|
43
|
+
6\. K-Nearest Neighbors (KNN)
|
|
44
|
+
|
|
45
|
+
7\. Decision Tree Classifier
|
|
46
|
+
|
|
47
|
+
8\. K-Means Clustering
|
|
48
|
+
|
|
49
|
+
9\. Hierarchical Clustering
|
|
50
|
+
|
|
51
|
+
10\. Activation Functions
|
|
52
|
+
|
|
53
|
+
11\. TensorFlow Tensors and Matrix Operations
|
|
54
|
+
|
|
55
|
+
12\. Artificial Neural Network (ANN)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
\## Example
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
|
|
65
|
+
from nupy import simple\_linear\_regression
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
print(simple\_linear\_regression())
|
|
70
|
+
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
The function displays the complete saved practical code as text. It does not execute the Machine Learning code.
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
\## Author
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
\*\*Rutuja Phalke\*\*
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
nuppy.egg-info/PKG-INFO
|
|
4
|
+
nuppy.egg-info/SOURCES.txt
|
|
5
|
+
nuppy.egg-info/dependency_links.txt
|
|
6
|
+
nuppy.egg-info/top_level.txt
|
|
7
|
+
nupy/__init__.py
|
|
8
|
+
nupy/activation_functions.py
|
|
9
|
+
nupy/artificial_neural_network.py
|
|
10
|
+
nupy/decision_tree.py
|
|
11
|
+
nupy/hierarchical_clustering.py
|
|
12
|
+
nupy/kmeans.py
|
|
13
|
+
nupy/knn.py
|
|
14
|
+
nupy/logistic_regression.py
|
|
15
|
+
nupy/multiple_linear_regression.py
|
|
16
|
+
nupy/naive_bayes.py
|
|
17
|
+
nupy/simple_linear_regression.py
|
|
18
|
+
nupy/support_vector_machine.py
|
|
19
|
+
nupy/tensorflow_tensors.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
nupy
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from .simple_linear_regression import simple_linear_regression
|
|
2
|
+
from .multiple_linear_regression import multiple_linear_regression
|
|
3
|
+
from .support_vector_machine import support_vector_machine
|
|
4
|
+
from .naive_bayes import naive_bayes
|
|
5
|
+
from .logistic_regression import logistic_regression
|
|
6
|
+
from .knn import knn
|
|
7
|
+
from .decision_tree import decision_tree
|
|
8
|
+
from .kmeans import kmeans
|
|
9
|
+
from .hierarchical_clustering import hierarchical_clustering
|
|
10
|
+
from .activation_functions import activation_functions
|
|
11
|
+
from .tensorflow_tensors import tensorflow_tensors
|
|
12
|
+
from .artificial_neural_network import artificial_neural_network
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# Code 10: Activation Functions
|
|
2
|
+
|
|
3
|
+
CODE = r'''
|
|
4
|
+
# Code 1: Binary Sigmoid Activation Function
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
import matplotlib.pyplot as plt
|
|
8
|
+
|
|
9
|
+
plt.style.use('seaborn-v0_8')
|
|
10
|
+
plt.figure(figsize=(8, 4))
|
|
11
|
+
|
|
12
|
+
def SigmoidBinary(t):
|
|
13
|
+
return 1 / (1 + np.exp(-t))
|
|
14
|
+
|
|
15
|
+
t = np.linspace(-5, 5)
|
|
16
|
+
|
|
17
|
+
plt.plot(t, SigmoidBinary(t))
|
|
18
|
+
plt.title('Binary Sigmoid Activation Function')
|
|
19
|
+
plt.show()
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
# Code 2: Hyperbolic Tan Activation Function
|
|
23
|
+
|
|
24
|
+
import numpy as np
|
|
25
|
+
import matplotlib.pyplot as plt
|
|
26
|
+
|
|
27
|
+
plt.style.use('seaborn-v0_8')
|
|
28
|
+
plt.figure(figsize=(8, 4))
|
|
29
|
+
|
|
30
|
+
def HyperbolicTan(t):
|
|
31
|
+
return np.tanh(t)
|
|
32
|
+
|
|
33
|
+
t = np.linspace(-5, 5)
|
|
34
|
+
|
|
35
|
+
plt.plot(t, HyperbolicTan(t))
|
|
36
|
+
plt.title('Hyperbolic Tan Activation Function')
|
|
37
|
+
plt.show()
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
# Code 3: Rectified Linear Unit (ReLU) Activation Function
|
|
41
|
+
|
|
42
|
+
import numpy as np
|
|
43
|
+
import matplotlib.pyplot as plt
|
|
44
|
+
|
|
45
|
+
plt.style.use('seaborn-v0_8')
|
|
46
|
+
plt.figure(figsize=(8, 4))
|
|
47
|
+
|
|
48
|
+
def RectifiedLinearUnit(t):
|
|
49
|
+
lst = []
|
|
50
|
+
|
|
51
|
+
for i in t:
|
|
52
|
+
if i >= 0:
|
|
53
|
+
lst.append(i)
|
|
54
|
+
else:
|
|
55
|
+
lst.append(0)
|
|
56
|
+
|
|
57
|
+
return lst
|
|
58
|
+
|
|
59
|
+
arr = np.linspace(-5, 5)
|
|
60
|
+
|
|
61
|
+
plt.plot(arr, RectifiedLinearUnit(arr))
|
|
62
|
+
plt.title('Rectified Linear Unit Activation Function')
|
|
63
|
+
plt.show()
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# Code 4: Binary Step Activation Function
|
|
67
|
+
|
|
68
|
+
import numpy as np
|
|
69
|
+
import matplotlib.pyplot as plt
|
|
70
|
+
|
|
71
|
+
plt.style.use('seaborn-v0_8')
|
|
72
|
+
plt.figure(figsize=(8, 4))
|
|
73
|
+
|
|
74
|
+
def binaryStep(x):
|
|
75
|
+
lst = []
|
|
76
|
+
|
|
77
|
+
for i in x:
|
|
78
|
+
if i >= 0:
|
|
79
|
+
lst.append(1)
|
|
80
|
+
else:
|
|
81
|
+
lst.append(0)
|
|
82
|
+
|
|
83
|
+
return lst
|
|
84
|
+
|
|
85
|
+
x = np.linspace(-10, 10)
|
|
86
|
+
|
|
87
|
+
plt.plot(x, binaryStep(x))
|
|
88
|
+
plt.axis('tight')
|
|
89
|
+
plt.title('Binary Step Activation Function')
|
|
90
|
+
plt.show()
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
# Code 5: Linear Activation Function
|
|
94
|
+
|
|
95
|
+
import numpy as np
|
|
96
|
+
import matplotlib.pyplot as plt
|
|
97
|
+
|
|
98
|
+
plt.style.use('seaborn-v0_8')
|
|
99
|
+
plt.figure(figsize=(8, 4))
|
|
100
|
+
|
|
101
|
+
def linear(x):
|
|
102
|
+
return x
|
|
103
|
+
|
|
104
|
+
x = np.linspace(-10, 10)
|
|
105
|
+
|
|
106
|
+
plt.plot(x, linear(x))
|
|
107
|
+
plt.axis('tight')
|
|
108
|
+
plt.title('Linear Activation Function')
|
|
109
|
+
plt.show()
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
# Code 6: Softmax Activation Function
|
|
113
|
+
|
|
114
|
+
import numpy as np
|
|
115
|
+
import matplotlib.pyplot as plt
|
|
116
|
+
|
|
117
|
+
plt.style.use('seaborn-v0_8')
|
|
118
|
+
plt.figure(figsize=(8, 4))
|
|
119
|
+
|
|
120
|
+
def softmax(t):
|
|
121
|
+
return np.exp(t) / np.sum(np.exp(t))
|
|
122
|
+
|
|
123
|
+
t = np.linspace(-5, 5)
|
|
124
|
+
|
|
125
|
+
plt.plot(t, softmax(t))
|
|
126
|
+
plt.title('Softmax Activation Function')
|
|
127
|
+
plt.show()
|
|
128
|
+
'''
|
|
129
|
+
|
|
130
|
+
def activation_functions():
|
|
131
|
+
return CODE
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# Code 12: Artificial Neural Network (ANN)
|
|
2
|
+
|
|
3
|
+
CODE = r'''
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import tensorflow as tf
|
|
7
|
+
|
|
8
|
+
# Import dataset
|
|
9
|
+
dataset = pd.read_csv('Churn_Modelling.csv')
|
|
10
|
+
|
|
11
|
+
X = dataset.iloc[:, 3:-1].values
|
|
12
|
+
Y = dataset.iloc[:, -1].values
|
|
13
|
+
|
|
14
|
+
# Encoding Gender
|
|
15
|
+
from sklearn.preprocessing import LabelEncoder
|
|
16
|
+
|
|
17
|
+
label_encoder = LabelEncoder()
|
|
18
|
+
|
|
19
|
+
X[:, 2] = label_encoder.fit_transform(X[:, 2])
|
|
20
|
+
|
|
21
|
+
# One Hot Encoding Geography
|
|
22
|
+
from sklearn.compose import ColumnTransformer
|
|
23
|
+
from sklearn.preprocessing import OneHotEncoder
|
|
24
|
+
|
|
25
|
+
ct = ColumnTransformer(
|
|
26
|
+
transformers=[('encoder', OneHotEncoder(), [1])],
|
|
27
|
+
remainder='passthrough'
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
X = np.array(ct.fit_transform(X))
|
|
31
|
+
|
|
32
|
+
# Split dataset into training and testing sets
|
|
33
|
+
from sklearn.model_selection import train_test_split
|
|
34
|
+
|
|
35
|
+
X_train, X_test, Y_train, Y_test = train_test_split(
|
|
36
|
+
X, Y, test_size=0.2, random_state=0
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
# Feature Scaling
|
|
40
|
+
from sklearn.preprocessing import StandardScaler
|
|
41
|
+
|
|
42
|
+
sc = StandardScaler()
|
|
43
|
+
|
|
44
|
+
X_train = sc.fit_transform(X_train)
|
|
45
|
+
X_test = sc.transform(X_test)
|
|
46
|
+
|
|
47
|
+
# Initializing the ANN
|
|
48
|
+
ann = tf.keras.models.Sequential()
|
|
49
|
+
|
|
50
|
+
# Adding the input layer and first hidden layer
|
|
51
|
+
ann.add(tf.keras.layers.Dense(units=6, activation='relu'))
|
|
52
|
+
|
|
53
|
+
# Adding the second hidden layer
|
|
54
|
+
ann.add(tf.keras.layers.Dense(units=6, activation='relu'))
|
|
55
|
+
|
|
56
|
+
# Adding the output layer
|
|
57
|
+
ann.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
|
|
58
|
+
|
|
59
|
+
# Compiling the ANN
|
|
60
|
+
ann.compile(
|
|
61
|
+
optimizer='adam',
|
|
62
|
+
loss='binary_crossentropy',
|
|
63
|
+
metrics=['accuracy']
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
# Training the ANN
|
|
67
|
+
ann.fit(
|
|
68
|
+
X_train,
|
|
69
|
+
Y_train,
|
|
70
|
+
batch_size=32,
|
|
71
|
+
epochs=100
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Predicting the Test Set Results
|
|
75
|
+
Y_pred = ann.predict(X_test)
|
|
76
|
+
|
|
77
|
+
Y_pred = (Y_pred > 0.5)
|
|
78
|
+
|
|
79
|
+
print(
|
|
80
|
+
np.concatenate(
|
|
81
|
+
(Y_pred.reshape(len(Y_pred), 1),
|
|
82
|
+
Y_test.reshape(len(Y_test), 1)),
|
|
83
|
+
1
|
|
84
|
+
)
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
# Making the Confusion Matrix
|
|
88
|
+
from sklearn.metrics import confusion_matrix, accuracy_score
|
|
89
|
+
|
|
90
|
+
cm = confusion_matrix(Y_test, Y_pred)
|
|
91
|
+
|
|
92
|
+
print(cm)
|
|
93
|
+
|
|
94
|
+
print(accuracy_score(Y_test, Y_pred))
|
|
95
|
+
'''
|
|
96
|
+
|
|
97
|
+
def artificial_neural_network():
|
|
98
|
+
return CODE
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Code 7: Decision Tree Classifier
|
|
2
|
+
|
|
3
|
+
CODE = r'''
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import matplotlib.pyplot as plt
|
|
7
|
+
|
|
8
|
+
# Import dataset
|
|
9
|
+
dataset = pd.read_csv('Social_Network_Ads.csv')
|
|
10
|
+
|
|
11
|
+
X = dataset.iloc[:, :-1].values
|
|
12
|
+
Y = dataset.iloc[:, -1].values
|
|
13
|
+
|
|
14
|
+
print(X)
|
|
15
|
+
print(Y)
|
|
16
|
+
|
|
17
|
+
# Split dataset into training and testing sets
|
|
18
|
+
from sklearn.model_selection import train_test_split
|
|
19
|
+
|
|
20
|
+
X_train, X_test, Y_train, Y_test = train_test_split(
|
|
21
|
+
X, Y, test_size=0.25, random_state=0
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
print(X_train)
|
|
25
|
+
print(Y_train)
|
|
26
|
+
|
|
27
|
+
# Feature Scaling
|
|
28
|
+
from sklearn.preprocessing import StandardScaler
|
|
29
|
+
|
|
30
|
+
sc = StandardScaler()
|
|
31
|
+
|
|
32
|
+
X_train = sc.fit_transform(X_train)
|
|
33
|
+
X_test = sc.transform(X_test)
|
|
34
|
+
|
|
35
|
+
# Train Decision Tree Classifier
|
|
36
|
+
from sklearn.tree import DecisionTreeClassifier
|
|
37
|
+
|
|
38
|
+
classifier = DecisionTreeClassifier()
|
|
39
|
+
|
|
40
|
+
classifier.fit(X_train, Y_train)
|
|
41
|
+
|
|
42
|
+
# Predict the Test Set Results
|
|
43
|
+
Y_pred = classifier.predict(X_test)
|
|
44
|
+
|
|
45
|
+
print(
|
|
46
|
+
np.concatenate(
|
|
47
|
+
(Y_pred.reshape(len(Y_pred), 1),
|
|
48
|
+
Y_test.reshape(len(Y_test), 1)),
|
|
49
|
+
1
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# Making the Confusion Matrix
|
|
54
|
+
from sklearn.metrics import confusion_matrix, accuracy_score
|
|
55
|
+
|
|
56
|
+
cm = confusion_matrix(Y_test, Y_pred)
|
|
57
|
+
|
|
58
|
+
print(cm)
|
|
59
|
+
|
|
60
|
+
print(accuracy_score(Y_test, Y_pred))
|
|
61
|
+
'''
|
|
62
|
+
|
|
63
|
+
def decision_tree():
|
|
64
|
+
return CODE
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Code 9: Hierarchical Clustering
|
|
2
|
+
|
|
3
|
+
CODE = r'''
|
|
4
|
+
import numpy as np
|
|
5
|
+
import matplotlib.pyplot as plt
|
|
6
|
+
import pandas as pd
|
|
7
|
+
|
|
8
|
+
# Import dataset
|
|
9
|
+
dataset = pd.read_csv('Mall_Customers.csv')
|
|
10
|
+
|
|
11
|
+
# Select Annual Income and Spending Score
|
|
12
|
+
X = dataset.iloc[:, [3, 4]].values
|
|
13
|
+
|
|
14
|
+
# Create Dendrogram
|
|
15
|
+
import scipy.cluster.hierarchy as sch
|
|
16
|
+
|
|
17
|
+
dendrogram = sch.dendrogram(
|
|
18
|
+
sch.linkage(X, method='ward')
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
plt.title('Dendrogram')
|
|
22
|
+
plt.xlabel('Customers')
|
|
23
|
+
plt.ylabel('Euclidean Distances')
|
|
24
|
+
plt.show()
|
|
25
|
+
|
|
26
|
+
# Apply Hierarchical Clustering
|
|
27
|
+
from sklearn.cluster import AgglomerativeClustering
|
|
28
|
+
|
|
29
|
+
hc = AgglomerativeClustering(
|
|
30
|
+
n_clusters=5,
|
|
31
|
+
metric='euclidean',
|
|
32
|
+
linkage='ward'
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
y_hc = hc.fit_predict(X)
|
|
36
|
+
|
|
37
|
+
# Visualize the clusters
|
|
38
|
+
plt.scatter(
|
|
39
|
+
X[y_hc == 0, 0],
|
|
40
|
+
X[y_hc == 0, 1],
|
|
41
|
+
s=100,
|
|
42
|
+
label='Cluster 1'
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
plt.scatter(
|
|
46
|
+
X[y_hc == 1, 0],
|
|
47
|
+
X[y_hc == 1, 1],
|
|
48
|
+
s=100,
|
|
49
|
+
label='Cluster 2'
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
plt.scatter(
|
|
53
|
+
X[y_hc == 2, 0],
|
|
54
|
+
X[y_hc == 2, 1],
|
|
55
|
+
s=100,
|
|
56
|
+
label='Cluster 3'
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
plt.scatter(
|
|
60
|
+
X[y_hc == 3, 0],
|
|
61
|
+
X[y_hc == 3, 1],
|
|
62
|
+
s=100,
|
|
63
|
+
label='Cluster 4'
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
plt.scatter(
|
|
67
|
+
X[y_hc == 4, 0],
|
|
68
|
+
X[y_hc == 4, 1],
|
|
69
|
+
s=100,
|
|
70
|
+
label='Cluster 5'
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
plt.title('Clusters of customers')
|
|
74
|
+
plt.xlabel('Annual Income (k$)')
|
|
75
|
+
plt.ylabel('Spending Score (1 to 100)')
|
|
76
|
+
plt.legend()
|
|
77
|
+
plt.show()
|
|
78
|
+
'''
|
|
79
|
+
|
|
80
|
+
def hierarchical_clustering():
|
|
81
|
+
return CODE
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# Code 8: K-Means Clustering
|
|
2
|
+
|
|
3
|
+
CODE = r'''
|
|
4
|
+
import pandas as pd
|
|
5
|
+
import matplotlib.pyplot as plt
|
|
6
|
+
import numpy as np
|
|
7
|
+
|
|
8
|
+
# Import dataset
|
|
9
|
+
dataset = pd.read_csv('Mall_Customers.csv')
|
|
10
|
+
|
|
11
|
+
# Select Annual Income and Spending Score
|
|
12
|
+
X = dataset.iloc[:, [3, 4]].values
|
|
13
|
+
|
|
14
|
+
# Apply K-Means Clustering
|
|
15
|
+
from sklearn.cluster import KMeans
|
|
16
|
+
|
|
17
|
+
kmean = KMeans(
|
|
18
|
+
n_clusters=5,
|
|
19
|
+
init='k-means++',
|
|
20
|
+
random_state=42
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
y_kmeans = kmean.fit_predict(X)
|
|
24
|
+
|
|
25
|
+
print(y_kmeans)
|
|
26
|
+
|
|
27
|
+
# Visualize the clusters
|
|
28
|
+
plt.scatter(
|
|
29
|
+
X[y_kmeans == 0, 0],
|
|
30
|
+
X[y_kmeans == 0, 1],
|
|
31
|
+
s=100,
|
|
32
|
+
label='Cluster 1'
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
plt.scatter(
|
|
36
|
+
X[y_kmeans == 1, 0],
|
|
37
|
+
X[y_kmeans == 1, 1],
|
|
38
|
+
s=100,
|
|
39
|
+
label='Cluster 2'
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
plt.scatter(
|
|
43
|
+
X[y_kmeans == 2, 0],
|
|
44
|
+
X[y_kmeans == 2, 1],
|
|
45
|
+
s=100,
|
|
46
|
+
label='Cluster 3'
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
plt.scatter(
|
|
50
|
+
X[y_kmeans == 3, 0],
|
|
51
|
+
X[y_kmeans == 3, 1],
|
|
52
|
+
s=100,
|
|
53
|
+
label='Cluster 4'
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
plt.scatter(
|
|
57
|
+
X[y_kmeans == 4, 0],
|
|
58
|
+
X[y_kmeans == 4, 1],
|
|
59
|
+
s=100,
|
|
60
|
+
label='Cluster 5'
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
# Plot cluster centers
|
|
64
|
+
plt.scatter(
|
|
65
|
+
kmean.cluster_centers_[:, 0],
|
|
66
|
+
kmean.cluster_centers_[:, 1],
|
|
67
|
+
s=300,
|
|
68
|
+
c='black',
|
|
69
|
+
label='Centroids'
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
plt.title('Clusters of customers')
|
|
73
|
+
plt.xlabel('Annual Income (k$)')
|
|
74
|
+
plt.ylabel('Spending Score (1 to 100)')
|
|
75
|
+
plt.legend()
|
|
76
|
+
plt.show()
|
|
77
|
+
'''
|
|
78
|
+
|
|
79
|
+
def kmeans():
|
|
80
|
+
return CODE
|
nuppy-0.1.0/nupy/knn.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Code 6: K-Nearest Neighbors (KNN)
|
|
2
|
+
|
|
3
|
+
CODE = r'''
|
|
4
|
+
import numpy as np
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import matplotlib.pyplot as plt
|
|
7
|
+
|
|
8
|
+
# Import dataset
|
|
9
|
+
dataset = pd.read_csv('Social_Network_Ads.csv')
|
|
10
|
+
|
|
11
|
+
X = dataset.iloc[:, :-1].values
|
|
12
|
+
Y = dataset.iloc[:, -1].values
|
|
13
|
+
|
|
14
|
+
print(X)
|
|
15
|
+
print(Y)
|
|
16
|
+
|
|
17
|
+
# Split dataset into training and testing sets
|
|
18
|
+
from sklearn.model_selection import train_test_split
|
|
19
|
+
|
|
20
|
+
X_train, X_test, Y_train, Y_test = train_test_split(
|
|
21
|
+
X, Y, test_size=0.25, random_state=0
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
print(X_train)
|
|
25
|
+
print(Y_train)
|
|
26
|
+
|
|
27
|
+
# Feature Scaling
|
|
28
|
+
from sklearn.preprocessing import StandardScaler
|
|
29
|
+
|
|
30
|
+
sc = StandardScaler()
|
|
31
|
+
|
|
32
|
+
X_train = sc.fit_transform(X_train)
|
|
33
|
+
X_test = sc.transform(X_test)
|
|
34
|
+
|
|
35
|
+
# Train KNN Classifier
|
|
36
|
+
from sklearn.neighbors import KNeighborsClassifier
|
|
37
|
+
|
|
38
|
+
classifier = KNeighborsClassifier(
|
|
39
|
+
n_neighbors=5,
|
|
40
|
+
metric='minkowski',
|
|
41
|
+
p=2
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
classifier.fit(X_train, Y_train)
|
|
45
|
+
|
|
46
|
+
# Predict the Test Set Results
|
|
47
|
+
Y_pred = classifier.predict(X_test)
|
|
48
|
+
|
|
49
|
+
print(
|
|
50
|
+
np.concatenate(
|
|
51
|
+
(Y_pred.reshape(len(Y_pred), 1),
|
|
52
|
+
Y_test.reshape(len(Y_test), 1)),
|
|
53
|
+
1
|
|
54
|
+
)
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Making the Confusion Matrix
|
|
58
|
+
from sklearn.metrics import confusion_matrix, accuracy_score
|
|
59
|
+
|
|
60
|
+
cm = confusion_matrix(Y_test, Y_pred)
|
|
61
|
+
|
|
62
|
+
print(cm)
|
|
63
|
+
|
|
64
|
+
print(accuracy_score(Y_test, Y_pred))
|
|
65
|
+
'''
|
|
66
|
+
|
|
67
|
+
def knn():
|
|
68
|
+
return CODE
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Code 5: Logistic Regression on Iris Dataset
|
|
2
|
+
|
|
3
|
+
CODE = r'''
|
|
4
|
+
import pandas as pd
|
|
5
|
+
import matplotlib.pyplot as plt
|
|
6
|
+
|
|
7
|
+
# Import dataset
|
|
8
|
+
iris_data = pd.read_csv('iris.csv')
|
|
9
|
+
|
|
10
|
+
print(iris_data.head())
|
|
11
|
+
print(iris_data.info())
|
|
12
|
+
|
|
13
|
+
# Encode Species
|
|
14
|
+
from sklearn.preprocessing import LabelEncoder
|
|
15
|
+
|
|
16
|
+
label_encoder = LabelEncoder()
|
|
17
|
+
|
|
18
|
+
iris_data['Species'] = label_encoder.fit_transform(iris_data['Species'])
|
|
19
|
+
|
|
20
|
+
print(iris_data.head())
|
|
21
|
+
|
|
22
|
+
# Independent and Dependent Variables
|
|
23
|
+
X = iris_data.drop('Species', axis=1)
|
|
24
|
+
Y = iris_data['Species']
|
|
25
|
+
|
|
26
|
+
# Split dataset into training and testing sets
|
|
27
|
+
from sklearn.model_selection import train_test_split
|
|
28
|
+
|
|
29
|
+
X_train, X_test, Y_train, Y_test = train_test_split(
|
|
30
|
+
X, Y, test_size=0.2, random_state=2
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# Create Logistic Regression model
|
|
34
|
+
from sklearn.linear_model import LogisticRegression
|
|
35
|
+
|
|
36
|
+
classifier = LogisticRegression(max_iter=1000)
|
|
37
|
+
|
|
38
|
+
classifier.fit(X_train, Y_train)
|
|
39
|
+
|
|
40
|
+
# Predict the Test Set Results
|
|
41
|
+
Y_pred = classifier.predict(X_test)
|
|
42
|
+
|
|
43
|
+
print(Y_pred)
|
|
44
|
+
print(Y_test)
|
|
45
|
+
|
|
46
|
+
# Accuracy
|
|
47
|
+
from sklearn.metrics import accuracy_score, confusion_matrix
|
|
48
|
+
|
|
49
|
+
print("Training Accuracy:", classifier.score(X_train, Y_train))
|
|
50
|
+
print("Testing Accuracy:", classifier.score(X_test, Y_test))
|
|
51
|
+
|
|
52
|
+
# Confusion Matrix
|
|
53
|
+
cm = confusion_matrix(Y_test, Y_pred)
|
|
54
|
+
|
|
55
|
+
print(cm)
|
|
56
|
+
'''
|
|
57
|
+
|
|
58
|
+
def logistic_regression():
|
|
59
|
+
return CODE
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
CODE = r'''
|
|
2
|
+
import numpy as np
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
dataset = pd.read_csv('50_Startups.csv')
|
|
7
|
+
|
|
8
|
+
X = dataset.iloc[:, :-1].values
|
|
9
|
+
Y = dataset.iloc[:, -1].values
|
|
10
|
+
|
|
11
|
+
print(X)
|
|
12
|
+
print(Y)
|
|
13
|
+
|
|
14
|
+
from sklearn.compose import ColumnTransformer
|
|
15
|
+
from sklearn.preprocessing import OneHotEncoder
|
|
16
|
+
|
|
17
|
+
ct = ColumnTransformer(
|
|
18
|
+
transformers=[('encoder', OneHotEncoder(), [3])],
|
|
19
|
+
remainder='passthrough'
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
X = np.array(ct.fit_transform(X))
|
|
23
|
+
|
|
24
|
+
from sklearn.model_selection import train_test_split
|
|
25
|
+
|
|
26
|
+
X_train, X_test, Y_train, Y_test = train_test_split(
|
|
27
|
+
X, Y, test_size=0.2, random_state=0
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
print(X_train)
|
|
31
|
+
print(Y_train)
|
|
32
|
+
|
|
33
|
+
from sklearn.linear_model import LinearRegression
|
|
34
|
+
|
|
35
|
+
regressor = LinearRegression()
|
|
36
|
+
regressor.fit(X_train, Y_train)
|
|
37
|
+
|
|
38
|
+
Y_pred = regressor.predict(X_test)
|
|
39
|
+
|
|
40
|
+
np.set_printoptions(precision=2)
|
|
41
|
+
|
|
42
|
+
print(
|
|
43
|
+
np.concatenate(
|
|
44
|
+
(Y_pred.reshape(len(Y_pred), 1),
|
|
45
|
+
Y_test.reshape(len(Y_test), 1)),
|
|
46
|
+
1
|
|
47
|
+
)
|
|
48
|
+
)
|
|
49
|
+
'''
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def multiple_linear_regression():
|
|
53
|
+
return CODE
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# Code 4: Naive Bayes Classifier
|
|
2
|
+
|
|
3
|
+
CODE = r'''
|
|
4
|
+
import numpy as np
|
|
5
|
+
import matplotlib.pyplot as plt
|
|
6
|
+
import pandas as pd
|
|
7
|
+
|
|
8
|
+
# Import dataset
|
|
9
|
+
dataset = pd.read_csv('Social_Network_Ads.csv')
|
|
10
|
+
|
|
11
|
+
X = dataset.iloc[:, :-1].values
|
|
12
|
+
Y = dataset.iloc[:, -1].values
|
|
13
|
+
|
|
14
|
+
print(X)
|
|
15
|
+
print(Y)
|
|
16
|
+
|
|
17
|
+
# Split dataset into training and testing sets
|
|
18
|
+
from sklearn.model_selection import train_test_split
|
|
19
|
+
|
|
20
|
+
X_train, X_test, Y_train, Y_test = train_test_split(
|
|
21
|
+
X, Y, test_size=0.25, random_state=0
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
print(X_train)
|
|
25
|
+
print(Y_train)
|
|
26
|
+
|
|
27
|
+
# Feature Scaling
|
|
28
|
+
from sklearn.preprocessing import StandardScaler
|
|
29
|
+
|
|
30
|
+
sc = StandardScaler()
|
|
31
|
+
|
|
32
|
+
X_train = sc.fit_transform(X_train)
|
|
33
|
+
X_test = sc.transform(X_test)
|
|
34
|
+
|
|
35
|
+
# Train Naive Bayes Classifier
|
|
36
|
+
from sklearn.naive_bayes import GaussianNB
|
|
37
|
+
|
|
38
|
+
classifier = GaussianNB()
|
|
39
|
+
|
|
40
|
+
classifier.fit(X_train, Y_train)
|
|
41
|
+
|
|
42
|
+
# Predict the Test Set Results
|
|
43
|
+
Y_pred = classifier.predict(X_test)
|
|
44
|
+
|
|
45
|
+
print(
|
|
46
|
+
np.concatenate(
|
|
47
|
+
(Y_pred.reshape(len(Y_pred), 1),
|
|
48
|
+
Y_test.reshape(len(Y_test), 1)),
|
|
49
|
+
1
|
|
50
|
+
)
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
# Making the Confusion Matrix
|
|
54
|
+
from sklearn.metrics import confusion_matrix, accuracy_score
|
|
55
|
+
|
|
56
|
+
cm = confusion_matrix(Y_test, Y_pred)
|
|
57
|
+
|
|
58
|
+
print(cm)
|
|
59
|
+
|
|
60
|
+
print(accuracy_score(Y_test, Y_pred))
|
|
61
|
+
'''
|
|
62
|
+
|
|
63
|
+
def naive_bayes():
|
|
64
|
+
return CODE
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
CODE = r'''
|
|
2
|
+
import numpy as np
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
# Import dataset
|
|
7
|
+
dataset = pd.read_csv('Salary_Data.csv')
|
|
8
|
+
|
|
9
|
+
# Independent variable (Years of Experience)
|
|
10
|
+
X = dataset.iloc[:, :-1].values
|
|
11
|
+
|
|
12
|
+
# Dependent variable (Salary)
|
|
13
|
+
Y = dataset.iloc[:, -1].values
|
|
14
|
+
|
|
15
|
+
print(X)
|
|
16
|
+
print(Y)
|
|
17
|
+
|
|
18
|
+
# Split dataset into training and testing sets
|
|
19
|
+
from sklearn.model_selection import train_test_split
|
|
20
|
+
|
|
21
|
+
X_train, X_test, Y_train, Y_test = train_test_split(
|
|
22
|
+
X, Y, test_size=1/3, random_state=0
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
print(X_train)
|
|
26
|
+
print(Y_train)
|
|
27
|
+
|
|
28
|
+
# Create Linear Regression model
|
|
29
|
+
from sklearn.linear_model import LinearRegression
|
|
30
|
+
|
|
31
|
+
regressor = LinearRegression()
|
|
32
|
+
regressor.fit(X_train, Y_train)
|
|
33
|
+
|
|
34
|
+
# Visualize Training Set
|
|
35
|
+
plt.scatter(X_train, Y_train, color='red')
|
|
36
|
+
plt.plot(X_train, regressor.predict(X_train), color='blue')
|
|
37
|
+
|
|
38
|
+
plt.title('Salary vs Experience (Training Set)')
|
|
39
|
+
plt.xlabel('Years of Experience')
|
|
40
|
+
plt.ylabel('Salary')
|
|
41
|
+
|
|
42
|
+
plt.show()
|
|
43
|
+
'''
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def simple_linear_regression():
|
|
47
|
+
return CODE
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
CODE = r'''
|
|
2
|
+
import numpy as np
|
|
3
|
+
import matplotlib.pyplot as plt
|
|
4
|
+
import pandas as pd
|
|
5
|
+
|
|
6
|
+
# Step 2: Load Dataset
|
|
7
|
+
dataset = pd.read_csv('Social_Network_Ads.csv')
|
|
8
|
+
|
|
9
|
+
X = dataset.iloc[:, :-1].values
|
|
10
|
+
Y = dataset.iloc[:, -1].values
|
|
11
|
+
|
|
12
|
+
print(X)
|
|
13
|
+
print(Y)
|
|
14
|
+
|
|
15
|
+
# Step 3: Split Dataset
|
|
16
|
+
from sklearn.model_selection import train_test_split
|
|
17
|
+
|
|
18
|
+
X_train, X_test, Y_train, Y_test = train_test_split(
|
|
19
|
+
X, Y, test_size=0.25, random_state=0
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
print(X_train)
|
|
23
|
+
print(Y_train)
|
|
24
|
+
|
|
25
|
+
# Step 4: Feature Scaling
|
|
26
|
+
from sklearn.preprocessing import StandardScaler
|
|
27
|
+
|
|
28
|
+
sc = StandardScaler()
|
|
29
|
+
|
|
30
|
+
X_train = sc.fit_transform(X_train)
|
|
31
|
+
X_test = sc.transform(X_test)
|
|
32
|
+
|
|
33
|
+
# Step 5: SVM Classifier
|
|
34
|
+
from sklearn.svm import SVC
|
|
35
|
+
|
|
36
|
+
classifier = SVC(kernel="linear", random_state=0)
|
|
37
|
+
|
|
38
|
+
classifier.fit(X_train, Y_train)
|
|
39
|
+
|
|
40
|
+
# Step 6: Prediction
|
|
41
|
+
Y_pred = classifier.predict(X_test)
|
|
42
|
+
|
|
43
|
+
print(
|
|
44
|
+
np.concatenate(
|
|
45
|
+
(Y_pred.reshape(len(Y_pred), 1),
|
|
46
|
+
Y_test.reshape(len(Y_test), 1)),
|
|
47
|
+
1
|
|
48
|
+
)
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# Step 7: Confusion Matrix
|
|
52
|
+
from sklearn.metrics import confusion_matrix, accuracy_score
|
|
53
|
+
|
|
54
|
+
cm = confusion_matrix(Y_test, Y_pred)
|
|
55
|
+
|
|
56
|
+
print(cm)
|
|
57
|
+
|
|
58
|
+
print(accuracy_score(Y_test, Y_pred))
|
|
59
|
+
'''
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def support_vector_machine():
|
|
63
|
+
return CODE
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Code 11: TensorFlow Tensors and Matrix Operations
|
|
2
|
+
|
|
3
|
+
CODE = r'''
|
|
4
|
+
import numpy as np
|
|
5
|
+
import tensorflow as tf
|
|
6
|
+
|
|
7
|
+
# display TensorFlow version
|
|
8
|
+
print(tf.__version__)
|
|
9
|
+
|
|
10
|
+
# 1D tensor
|
|
11
|
+
tensor_1d = np.array([1.3, 1, 4.0, 23.99])
|
|
12
|
+
|
|
13
|
+
print(tensor_1d)
|
|
14
|
+
print(tensor_1d[0])
|
|
15
|
+
print(tensor_1d[2])
|
|
16
|
+
|
|
17
|
+
# 2D tensor
|
|
18
|
+
tensor_2d = np.array([
|
|
19
|
+
(1, 2, 3, 4),
|
|
20
|
+
(4, 5, 6, 7),
|
|
21
|
+
(8, 9, 10, 11),
|
|
22
|
+
(12, 13, 14, 15)
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
print(tensor_2d)
|
|
26
|
+
print(tensor_2d[3][2])
|
|
27
|
+
|
|
28
|
+
# Matrix operations
|
|
29
|
+
matrix1 = np.array(
|
|
30
|
+
[(2, 2, 2), (2, 2, 2), (2, 2, 2)],
|
|
31
|
+
dtype='int32'
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
matrix2 = np.array(
|
|
35
|
+
[(1, 1, 1), (1, 1, 1), (1, 1, 1)],
|
|
36
|
+
dtype='int32'
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
print(matrix1)
|
|
40
|
+
print(matrix2)
|
|
41
|
+
|
|
42
|
+
matrix1 = tf.constant(matrix1)
|
|
43
|
+
matrix2 = tf.constant(matrix2)
|
|
44
|
+
|
|
45
|
+
matrix_product = tf.matmul(matrix1, matrix2)
|
|
46
|
+
print('Product : ', matrix_product)
|
|
47
|
+
|
|
48
|
+
matrix_sum = tf.add(matrix1, matrix2)
|
|
49
|
+
print('Sum : ', matrix_sum)
|
|
50
|
+
|
|
51
|
+
matrix_sub = tf.math.subtract(matrix1, matrix2)
|
|
52
|
+
print('Subtraction : ', matrix_sub)
|
|
53
|
+
|
|
54
|
+
matrix_div = tf.math.divide(matrix1, matrix2)
|
|
55
|
+
print('Division : ', matrix_div)
|
|
56
|
+
|
|
57
|
+
matrix_3 = np.array(
|
|
58
|
+
[(2, 7, 2), (1, 4, 2), (9, 0, 2)],
|
|
59
|
+
dtype='float32'
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
print(matrix_3)
|
|
63
|
+
|
|
64
|
+
a = 9
|
|
65
|
+
b = 10
|
|
66
|
+
|
|
67
|
+
print('a : ', a)
|
|
68
|
+
print('b : ', b)
|
|
69
|
+
|
|
70
|
+
res = tf.math.less(x=a, y=b)
|
|
71
|
+
print('Result : ', res)
|
|
72
|
+
'''
|
|
73
|
+
|
|
74
|
+
def tensorflow_tensors():
|
|
75
|
+
return CODE
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "nuppy"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "A collection of my Machine Learning practical codes"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [
|
|
11
|
+
{name = "a-123", email = "a12964856@gmail.com"}
|
|
12
|
+
]
|
|
13
|
+
requires-python = ">=3.8"
|
nuppy-0.1.0/setup.cfg
ADDED