mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Wrap lines that go beyond GitHub Editor (#1925)
* Wrap lines that go beyond GiHub Editor
* flake8 --count --select=E501 --max-line-length=127
* updating DIRECTORY.md
* Update strassen_matrix_multiplication.py
* fixup! Format Python code with psf/black push
* Update decision_tree.py
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -20,15 +20,21 @@ class Decision_Tree:
|
||||
mean_squared_error:
|
||||
@param labels: a one dimensional numpy array
|
||||
@param prediction: a floating point value
|
||||
return value: mean_squared_error calculates the error if prediction is used to estimate the labels
|
||||
return value: mean_squared_error calculates the error if prediction is used to
|
||||
estimate the labels
|
||||
>>> tester = Decision_Tree()
|
||||
>>> test_labels = np.array([1,2,3,4,5,6,7,8,9,10])
|
||||
>>> test_prediction = np.float(6)
|
||||
>>> assert tester.mean_squared_error(test_labels, test_prediction) == Test_Decision_Tree.helper_mean_squared_error_test(test_labels, test_prediction)
|
||||
>>> tester.mean_squared_error(test_labels, test_prediction) == (
|
||||
... Test_Decision_Tree.helper_mean_squared_error_test(test_labels,
|
||||
... test_prediction))
|
||||
True
|
||||
>>> test_labels = np.array([1,2,3])
|
||||
>>> test_prediction = np.float(2)
|
||||
>>> assert tester.mean_squared_error(test_labels, test_prediction) == Test_Decision_Tree.helper_mean_squared_error_test(test_labels, test_prediction)
|
||||
|
||||
>>> tester.mean_squared_error(test_labels, test_prediction) == (
|
||||
... Test_Decision_Tree.helper_mean_squared_error_test(test_labels,
|
||||
... test_prediction))
|
||||
True
|
||||
"""
|
||||
if labels.ndim != 1:
|
||||
print("Error: Input labels must be one dimensional")
|
||||
@@ -46,7 +52,8 @@ class Decision_Tree:
|
||||
"""
|
||||
|
||||
"""
|
||||
this section is to check that the inputs conform to our dimensionality constraints
|
||||
this section is to check that the inputs conform to our dimensionality
|
||||
constraints
|
||||
"""
|
||||
if X.ndim != 1:
|
||||
print("Error: Input data set must be one dimensional")
|
||||
@@ -72,7 +79,8 @@ class Decision_Tree:
|
||||
"""
|
||||
loop over all possible splits for the decision tree. find the best split.
|
||||
if no split exists that is less than 2 * error for the entire array
|
||||
then the data set is not split and the average for the entire array is used as the predictor
|
||||
then the data set is not split and the average for the entire array is used as
|
||||
the predictor
|
||||
"""
|
||||
for i in range(len(X)):
|
||||
if len(X[:i]) < self.min_leaf_size:
|
||||
@@ -136,7 +144,7 @@ class Test_Decision_Tree:
|
||||
helper_mean_squared_error_test:
|
||||
@param labels: a one dimensional numpy array
|
||||
@param prediction: a floating point value
|
||||
return value: helper_mean_squared_error_test calculates the mean squared error
|
||||
return value: helper_mean_squared_error_test calculates the mean squared error
|
||||
"""
|
||||
squared_error_sum = np.float(0)
|
||||
for label in labels:
|
||||
@@ -147,9 +155,10 @@ class Test_Decision_Tree:
|
||||
|
||||
def main():
|
||||
"""
|
||||
In this demonstration we're generating a sample data set from the sin function in numpy.
|
||||
We then train a decision tree on the data set and use the decision tree to predict the
|
||||
label of 10 different test values. Then the mean squared error over this test is displayed.
|
||||
In this demonstration we're generating a sample data set from the sin function in
|
||||
numpy. We then train a decision tree on the data set and use the decision tree to
|
||||
predict the label of 10 different test values. Then the mean squared error over
|
||||
this test is displayed.
|
||||
"""
|
||||
X = np.arange(-1.0, 1.0, 0.005)
|
||||
y = np.sin(X)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
## Logistic Regression from scratch
|
||||
# Logistic Regression from scratch
|
||||
|
||||
# In[62]:
|
||||
|
||||
@@ -8,8 +8,12 @@
|
||||
|
||||
# importing all the required libraries
|
||||
|
||||
""" Implementing logistic regression for classification problem
|
||||
Helpful resources : 1.Coursera ML course 2.https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac"""
|
||||
"""
|
||||
Implementing logistic regression for classification problem
|
||||
Helpful resources:
|
||||
Coursera ML course
|
||||
https://medium.com/@martinpella/logistic-regression-from-scratch-in-python-124c5636b8ac
|
||||
"""
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
@@ -21,7 +25,8 @@ from sklearn import datasets
|
||||
|
||||
# In[67]:
|
||||
|
||||
# sigmoid function or logistic function is used as a hypothesis function in classification problems
|
||||
# sigmoid function or logistic function is used as a hypothesis function in
|
||||
# classification problems
|
||||
|
||||
|
||||
def sigmoid_function(z):
|
||||
|
||||
@@ -3,6 +3,7 @@ from sklearn import svm
|
||||
from sklearn.model_selection import train_test_split
|
||||
import doctest
|
||||
|
||||
|
||||
# different functions implementing different types of SVM's
|
||||
def NuSVC(train_x, train_y):
|
||||
svc_NuSVC = svm.NuSVC()
|
||||
@@ -17,8 +18,11 @@ def Linearsvc(train_x, train_y):
|
||||
|
||||
|
||||
def SVC(train_x, train_y):
|
||||
# svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, shrinking=True, probability=False,tol=0.001, cache_size=200, class_weight=None, verbose=False, max_iter=-1, random_state=None)
|
||||
# various parameters like "kernel","gamma","C" can effectively tuned for a given machine learning model.
|
||||
# svm.SVC(C=1.0, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, shrinking=True,
|
||||
# probability=False,tol=0.001, cache_size=200, class_weight=None, verbose=False,
|
||||
# max_iter=-1, random_state=None)
|
||||
# various parameters like "kernel","gamma","C" can effectively tuned for a given
|
||||
# machine learning model.
|
||||
SVC = svm.SVC(gamma="auto")
|
||||
SVC.fit(train_x, train_y)
|
||||
return SVC
|
||||
@@ -27,8 +31,8 @@ def SVC(train_x, train_y):
|
||||
def test(X_new):
|
||||
"""
|
||||
3 test cases to be passed
|
||||
an array containing the sepal length (cm), sepal width (cm),petal length (cm),petal width (cm)
|
||||
based on which the target name will be predicted
|
||||
an array containing the sepal length (cm), sepal width (cm), petal length (cm),
|
||||
petal width (cm) based on which the target name will be predicted
|
||||
>>> test([1,2,1,4])
|
||||
'virginica'
|
||||
>>> test([5, 2, 4, 1])
|
||||
|
||||
Reference in New Issue
Block a user