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
| # LSTM for sequence classification in the IMDB dataset | |
| import numpy | |
| from keras.datasets import imdb | |
| from keras.models import Sequential | |
| from keras.layers import Dense | |
| from keras.layers import LSTM | |
| from keras.layers.embeddings import Embedding | |
| from keras.preprocessing import sequence | |
| # fix random seed for reproducibility | |
| numpy.random.seed(7) |
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
| from __future__ import print_function | |
| import keras | |
| from keras.datasets import cifar10 | |
| from keras.preprocessing.image import ImageDataGenerator | |
| from keras.models import Sequential | |
| from keras.layers import Dense, Dropout, Activation, Flatten | |
| from keras.layers import Conv2D, MaxPooling2D | |
| import os | |
| batch_size = 32 |
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
| # Load Datasets | |
| df_train = pd.read_csv('../input/train.csv') | |
| df_test = pd.read_csv('../input/test.csv') | |
| df_submit = pd.read_csv('../input/sample_submission.csv') | |
| # To numpy array - dataset of train | |
| x_all = df_train.drop(['target', 'id'], axis=1).values | |
| y_all = keras.utils.np_utils.to_categorical(df_train['target'].values) | |
| # For imbalanced data, better-way maybe exist! |
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
| # Example from Sklean Doc | |
| from sklearn.cluster import DBSCAN | |
| import numpy as np | |
| X = np.array([[1, 2], [2, 2], [2, 3],[8, 7], [8, 8], [25, 80]]) | |
| clustering = DBSCAN(eps=3, min_samples=2).fit(X) | |
| clustering.labels_ | |
| array([ 0, 0, 0, 1, 1, -1]) | |
| print(clustering) | |
| DBSCAN(algorithm='auto', eps=3, leaf_size=30, metric='euclidean', | |
| metric_params=None, min_samples=2, n_jobs=None, p=None) |
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 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) |
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
| from xgboost import XGBClassifier | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import accuracy_score | |
| X = dataset[:,0:10] | |
| Y = dataset[:,10:] | |
| seed = 1 | |
| X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.33, random_state=seed) | |
| model = XGBClassifier() |
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 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) | |
| print(predicted) |
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
| from sklearn.datasets import load_iris | |
| from sklearn.ensemble import RandomForestClassifier | |
| import pandas as pd | |
| import numpy as np | |
| iris=load_iris() | |
| df=pd.DataFrame(iris.data,columns=iris.feature_names) | |
| df['is_train']=np.random.uniform(0,1,len(df))<=.75 | |
| df['species']=pd.Categorical.from_codes(iris.target,iris.target_names) | |
| train,test=df[df['is_train']==True],df[df['is_train']==False] | |
| features=df.columns[:4] |
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
| from sklearn.naive_bayes import GaussianNB | |
| from sklearn.naive_bayes import MultinomialNB | |
| from sklearn import datasets | |
| from sklearn.metrics import confusion_matrix | |
| from sklearn.model_selection import train_test_split | |
| iris=datasets.load_iris() | |
| x=iris.data | |
| y=iris.target | |
| x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=0) | |
| gnb=GaussianNB() |
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 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) |