Home > Article > Backend Development > Traditional machine learning examples in Python
Python is one of the most popular programming languages and one of the important tools in the field of machine learning. Traditional machine learning is an important branch of the field of machine learning. It is dedicated to building a model to predict and classify new data through learning from historical data. This article will introduce some standard traditional machine learning examples in Python.
Linear regression is a method used to estimate the relationship between two variables. It uses the least squares method to calculate an optimal straight line fit based on the linear relationship between observed data points. In Python, scikit-learn is a library for machine learning, which includes many commonly used machine learning algorithms, including linear regression.
Example:
from sklearn.linear_model import LinearRegression X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=.2) linreg=LinearRegression() linreg.fit(X_train,y_train)
In the above example, the LinearRegression (linear regression) function and the train_test_split function are first imported. The train_test_split function is used to split the data set into a training data set. and test data set. Then a linear regression model object is initialized, and the training data set is sent to the model for training fit(). After the model is trained, make predictions on the test data set.
The decision tree algorithm is a non-parametric machine learning algorithm. It helps us better understand the information about the data set by visualizing the data in the form of a tree diagram. We can use trees to make predictions, making it a tree model. In Python, the implementation of the decision tree model is also very simple. We only need to import the DecisionTreeClassifier.
Example:
from sklearn.tree import DecisionTreeClassifier from sklearn.metrics import accuracy_score dt = DecisionTreeClassifier(max_depth=10) dt.fit(X_train, y_train) y_pred_dt = dt.predict(X_test) acc_decision_tree=accuracy_score(y_test, y_pred_dt)
In the above example, we first import the DecisionTreeClassifier function and accuracy_score function, max_depth is the depth of the decision tree, X_train and y_train are training data, and X_test and y_test are test data. Then use the fit() function to fit the model, and the predict() function to predict the model results.
The support vector machine algorithm is a classification algorithm that can find a relationship between training data and test data. The optimal boundary line (i.e., the decision boundary) and divides the test data into two different categories through this boundary line. In Python, we can use the svm.SVC function to implement SVM.
Example:
from sklearn import svm svm = svm.SVC(kernel='linear',C=1,gamma='auto') svm.fit(X_train, y_train) y_pred_svm = svm.predict(X_test) svm_score = svm.score(X_test, y_test)
In the above example, we use svm.SVC to initialize an SVM classifier model, specifying the kernel as linear and the gamma value as auto. And import the training data set into the model for model training, and use the predict() function to make predictions on the test data set. Use the score() function to calculate the model accuracy score.
Summary:
The above are the implementations of three traditional machine learning. These machine learning methods are widely used in many fields. In Python, the scikit-learn library can be used to quickly and effectively implement traditional machine learning models and obtain specific digital results.
The above is the detailed content of Traditional machine learning examples in Python. For more information, please follow other related articles on the PHP Chinese website!