Created
May 27, 2015 16:56
-
-
Save jamesthomson/c29f6c9c44703dd9f606 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sklearn.datasets import load_iris | |
iris = load_iris() | |
X = iris.data | |
print X | |
#scale the data | |
from sklearn.preprocessing import StandardScaler | |
SS=StandardScaler() | |
XS=SS.fit_transform(X) | |
print XS | |
#PCA | |
from sklearn.decomposition import PCA | |
#declare model and number of components | |
pca = PCA(n_components=2) | |
print pca | |
#fit model | |
pca_fit=pca.fit_transform(XS) | |
#explained variance for scree plot | |
print(pca.explained_variance_ratio_) | |
#eigenvalues | |
print(pca.components_) | |
#fitted projections | |
print(pca_fit) | |
#fit logistic regression | |
from sklearn.linear_model import LogisticRegression | |
Y = iris.target | |
print Y | |
#initilize | |
logistic=LogisticRegression() | |
#fit | |
logistic.fit(pca_fit,Y) | |
#intercept | |
print logistic.intercept_ | |
#coeffcients | |
print logistic.coef_ | |
#predict classification | |
print logistic.predict(pca_fit) | |
#predict prob | |
print logistic.predict_proba(pca_fit) | |
#KNN | |
from sklearn.neighbors import KNeighborsClassifier | |
neigh = KNeighborsClassifier(n_neighbors=3) | |
neigh.fit(pca_fit, y) | |
print(neigh.predict(pca_fit)) | |
#Tree | |
from sklearn.tree import DecisionTreeClassifier | |
tree = DecisionTreeClassifier(max_depth=2) | |
tree.fit(pca_fit,y) | |
print(tree.predict(pca_fit)) | |
#RandomForest | |
from sklearn.ensemble import RandomForestClassifier | |
RFC = RandomForestClassifier(n_estimators=10, max_depth=2) | |
RFC.fit(pca_fit,y) | |
print(RFC.predict(pca_fit)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment