python机器学习算法速查
2016-12-23 13:25
561 查看
广为流传的机器学习算法速查(Python & R)现将python共享之,共勉!
#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 arrays
x_train=input_variables_values_training_datasets
y_train=target_variables_values_training_datasets
x_test=input_variables_values_test_datasets
# Create linear regression object
linear = linear_model.LinearRegression()
# Train the model using the training sets and check score
linear.fit(x_train, y_train)
linear.score(x_train, y_train)
#Equation coefficient and Intercept
print('Coefficient: \n', linear.coef_)
print('Intercept: \n', linear.intercept_)
#Predict Output
predicted= linear.predict(x_test)
#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 arrays
x_train=input_variables_values_training_datasets
y_train=target_variables_values_training_datasets
x_test=input_variables_values_test_datasets
# Create linear regression object
linear = linear_model.LinearRegression()
# Train the model using the training sets and check score
linear.fit(x_train, y_train)
linear.score(x_train, y_train)
#Equation coefficient and Intercept
print('Coefficient: \n', linear.coef_)
print('Intercept: \n', linear.intercept_)
#Predict Output
predicted= linear.predict(x_test)
#2.逻辑回归 (Logistic Regression) #Import Library from 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 object model = LogisticRegression() # Train the model using the training sets and check score model.fit(X, y) model.score(X, y) #Equation coefficient and Intercept print('Coefficient: \n', model.coef_) print('Intercept: \n', model.intercept_) #Predict Output predicted= 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 score model.fit(X, y) model.score(X, y) #Predict Output predicted= model.predict(x_test)
#4.支持向量机(SVM) #Import Library from 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 score model.fit(X, y) model.score(X, y) #Predict Output predicted= model.predict(x_test)
#5.朴素贝叶斯 (Naive Bayes) #Import Library from 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 score model.fit(X, y) #Predict Output predicted= model.predict(x_test)
#6.K邻近算法(KNN) #Import Library from 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 score model.fit(X, y) #Predict Output predicted= model.predict(x_test)
#7.K-均值算法(K-means) #Import Library from 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 k_means = KMeans(n_clusters=3, random_state=0) # Train the model using the training sets and check score model.fit(X) #Predict Output predicted= model.predict(x_test)
#8.随机森林 (Random Forest) #random forest #import library from 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 object model=RandomForestClassifier() #train the model using the training sets and chek score model.fit(x,y) #predict output predict=model.presort(x_test)
#9.降低维度算法(Dimensionality Reduction Algorithms) #Import Library from 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 PCA train_reduced = pca.fit_transform(train) #Reduced the dimension of test dataset test_reduced = pca.transform(test)
#10.Gradient Boost和Adaboost算法 #Import Library from 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 object model= GradientBoostingClassifier(n_estimators=100, learning_rate=1.0, max_depth=1, random_state=0) # Train the model using the training sets and check score model.fit(X, y) #Predict Output predicted= model.predict(x_test)
相关文章推荐
- 机器学习算法 Python&R 速查表
- 机器学习算法 Python & R速查表
- Python机器学习算法速查
- 机器学习算法与Python实践之(七)逻辑回归(Logistic Regression)
- 机器学习算法与Python实践之(三)支持向量机(SVM)进阶
- 机器学习算法与Python实践之(五)k均值聚类(k-means)
- 机器学习算法与Python实践之(三)支持向量机(SVM)进阶
- 机器学习算法与Python实践之(二)支持向量机(SVM)初级
- Python速查笔记(2) 内置类型
- 【机器学习算法-python实现】决策树-Decision tree(1) 信息熵划分数据集
- 【机器学习算法-python实现】KNN-k近邻算法的实现(附源码)
- python速查笔记(1) 类型与函数
- 机器学习算法与Python实践之(五)k均值聚类(k-means)
- 【机器学习算法-python实现】扫黄神器-朴素贝叶斯分类器的实现
- 【机器学习算法-python实现】决策树-Decision tree(2) 决策树的实现
- 机器学习算法与Python实践之(六)二分k均值聚类
- [置顶] python实现shell下小抄速查工具
- 机器学习算法与Python实践之(一)k近邻(KNN)
- 机器学习算法与Python实践之(一)k近邻(KNN)
- 机器学习算法与Python实践之(五)k均值聚类(k-means)