国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python機器學習算法速查

2019-11-08 01:40:30
字體:
來源:轉載
供稿:網友

常見的機器學習算法

以下是最常用的機器學習算法,大部分數據問題都可以通過它們解決:

線性回歸 (Linear Regression)

邏輯回歸 (Logistic Regression)

決策樹 (Decision Tree)

支持向量機(SVM)

樸素貝葉斯 (Naive Bayes)

K鄰近算法(KNN)

K-均值算法(K-means)

隨機森林 (Random Forest)

降低維度算法(Dimensionality Reduction Algorithms)

Gradient Boost和Adaboost算法

1.線性回歸 (Linear Regression)

#Import Library#Import other necessary libraries like pandas, numpy...from sklearn import linear_model#Load Train and Test datasets#Identify feature and response variable(s) and values must be numeric and numpy arraysx_train=input_variables_values_training_datasetsy_train=target_variables_values_training_datasetsx_test=input_variables_values_test_datasets# Create linear regression objectlinear = linear_model.LinearRegression()# Train the model using the training sets and check scorelinear.fit(x_train, y_train)linear.score(x_train, y_train)#Equation coefficient and Intercept2.邏輯回歸 (Logistic Regression)#Import Libraryfrom sklearn.linear_model import LogisticRegression#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset# Create logistic regression objectmodel = LogisticRegression()# Train the model using the training sets and check scoremodel.fit(X, y)model.score(X, y)#Equation coefficient and Interceptprint('Coefficient: /n', model.coef_)print('Intercept: /n', model.intercept_)#Predict Outputpredicted= model.predict(x_test)

3.決策樹 (Decision Tree)

#Import Library#Import other necessary libraries like pandas, numpy...from sklearn import tree#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset# Create tree object model = tree.DecisionTreeClassifier(criterion='gini') # for classification, here you can change the algorithm as gini or entropy (information gain) by default it is gini # model = tree.DecisionTreeRegressor() for regression# Train the model using the training sets and check scoremodel.fit(X, y)model.score(X, y)#Predict Outputpredicted= model.predict(x_test)

4.支持向量機(SVM)

#Import Libraryfrom sklearn import svm#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset# Create SVM classification object model = svm.SVC() # there is various option associated with it, this is simple for classification. You can refer link, for mo# re detail.# Train the model using the training sets and check scoremodel.fit(X, y)model.score(X, y)#Predict Outputpredicted= model.predict(x_test)

5.樸素貝葉斯 (Naive Bayes)

#Import Libraryfrom sklearn.naive_bayes import GaussianNB#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset# Create SVM classification object model = GaussianNB() # there is other distribution for multinomial classes like Bernoulli Naive Bayes, Refer link# Train the model using the training sets and check scoremodel.fit(X, y)#Predict Outputpredicted= model.predict(x_test)

6.K鄰近算法(KNN)

#Import Libraryfrom sklearn.neighbors import KNeighborsClassifier#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset# Create KNeighbors classifier object model = KNeighborsClassifier(n_neighbors=6) # default value for n_neighbors is 5# Train the model using the training sets and check scoremodel.fit(X, y)#Predict Outputpredicted= model.predict(x_test)

7.K-均值算法(K-means )

#Import Libraryfrom sklearn.cluster import KMeans#Assumed you have, X (attributes) for training data set and x_test(attributes) of test_dataset# Create KNeighbors classifier object model model = KMeans(n_clusters=3, random_state=0)# Train the model using the training sets and check scoremodel.fit(X)#Predict Outputpredicted= model.predict(x_test)

8.隨機森林 (Random Forest)

#random forest#import libraryfrom sklearn.ensemble import RandomForestClassifier#assumed you have x(predictor)and y(target) for training data set and x_test(predictor)of test_dataset#create random forest objectmodel=RandomForestClassifier()#train the model using the training sets and chek scoremodel.fit(x,y)#predict outputpredict=model.presort(x_test)

9.降低維度算法(Dimensionality Reduction Algorithms)

#Import Libraryfrom sklearn import decomposition#Assumed you have training and test data set as train and test# Create PCA obeject pca= decomposition.PCA(n_components=k) #default value of k =min(n_sample, n_features)# For Factor analysis#fa= decomposition.FactorAnalysis()# Reduced the dimension of training dataset using PCAtrain_reduced = pca.fit_transform(train)#Reduced the dimension of test datasettest_reduced = pca.transform(test)

10.Gradient Boost和Adaboost算法

#Import Libraryfrom sklearn.ensemble import GradientBoostingClassifier#Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset# Create Gradient Boosting Classifier objectmodel= GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0)# Train the model using the training sets and check scoremodel.fit(X, y)#Predict Outputpredicted= model.predict(x_test)

以下實例中predict數據時為了驗證其擬合度,采用的是訓練集數據作為參數,實際中應該采用的是測試集,不要被誤導了!!!

這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述 這里寫圖片描述

這里寫圖片描述

這里寫圖片描述 這里寫圖片描述

這里寫圖片描述

這里寫圖片描述 這里寫圖片描述 參考:http://blog.csdn.net/han_xiaoyang/article/details/51191386


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 会昌县| 灌阳县| 奎屯市| 闵行区| 辽宁省| 平陆县| 图木舒克市| 潢川县| 皮山县| 长丰县| 株洲市| 越西县| 讷河市| 南汇区| 刚察县| 安龙县| 青州市| 开阳县| 阳城县| 昭通市| 久治县| 苍溪县| 沈阳市| 龙川县| 澜沧| 安庆市| 通州市| 从江县| 福清市| 秭归县| 彭泽县| 昆山市| 会东县| 辽源市| 高雄县| 锡林浩特市| 桦甸市| 澄迈县| 汶川县| 阿克陶县| 永清县|