示例#1
0
def split_test_train_data():
    df_wine = pd.read_csv(
        'https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data',
        header=None)
    df_wine.columns = [
        'Class label', 'Alcohol', 'Malic acid', 'Ash', 'Alcalinity of Ash',
        'Magnesium', 'Total phenols', 'Flavanoids', 'Nonflavanoid phenols',
        'Proanthocyanins', 'Color intensity', 'Hue',
        'OD280/OD315 of diluted wines', 'Proline'
    ]

    X, y = df_wine.iloc[:, 1:].values, df_iloc[:, 0].values
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.3,
                                                        random_state=0)
    mms = MinMaxScalar()
    stdsc = StandardScalar()
    X_train_norm = mms.fit_transform(X_train)
    X_test_norm = mmx.transform(X_test)
    X_train_std = stdsc.fit_transform(X_train)
    X_test_std = stdsc.transform(X_test)
示例#2
0
import pandas as pd 

#Importing the dataset
dataset = pd.read_csv('Social_Network_Ads.csv')
X = dataset.iloc[:, [2,3]].values
y = dataset.iloc[:, 4].values

#Splitting the dataset into the Training set and Test set
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

#Feature Scaling (Zscore, it standardizes the data) no need in 
from sklearn.preprocessing import StandardScalar
sc_X = StandardScalar()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.transform(X_test)

#Applying Kernal PCA
from sklearn.decomposition import KernelPCA
kpca = KernelPCA(n_components = 2, kernel = 'rbf')
X_train = kpca.fit_transform(X_train)
X_test = kpca.transform(X_test)

#Fitting Logistic regression to the Training set
from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state = 0)
classifer.fit(X_train, y_train)

#Predicting the Test set results
y_pred = classifier.predict(X_test)
示例#3
0
import matplotlib.pyplot as plt
#dataset problem- classify whether the person will purchase a product or not
#age/salary independent,purchase is the dependent variable
D = pd.read_csv("Social_Network_Ads.csv")
X = D.iloc[:, [2, 3]].values
y = D.iloc[:, 4].values  #depedent variable
#maybe curved or linear line for classification
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.25,
                                                    random_state=0)
from sklearn.preprocessing import StandardScalar
sc = StandardScalar()  #feature scaling[-2,+2]
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
Classfier = SVC(kernel='linear', random_state=0)
Classfier.fit(X_train, y_train)
y_pred = Classfier.predict(X_test)

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

#visualizing the SVM KERNELS
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(
    np.arange(start=X_set[:, 0].min() - 1,
              stop=X_set[:, 0].max() + 1,
              step=0.01),
    np.arange(start=X_set[:, 1].min() - 1,
示例#4
0
len(X_train[X_train['Embarked'] == 'S'])
len(X_train[X_train['Embarked'] == 'C'])
len(X_train[X_train['Embarked'] == 'Q'])
len(X_train[X_train['Embarked'] ==
            ' nan'])  # no way of dealing with nan's this way

X_train['Embarked'].fillna('S', inplace=True)  # used S as it is mode
X_train['Embarked'] = labelencoder_X.fit_transform(X_train['Embarked'])

np.mean(X_train['Age'])
X_train['Age'].fillna(np.mean(X_train['Age']), inplace=True)

X_Pclass = pd.get_dummies(X_train['Pclass'],
                          prefix=['Pclass'],
                          drop_first=True)  # onehotencoding dataframe
X_Embarked = pd.get_dummies(X_train['Embarked'],
                            prefix=['Embarked'],
                            drop_first=True)

X_train = X_train.drop('Pclass', axis=1)
X_train = X_train.drop('Embarked', axis=1)
X_train = X_train.append(X_Pclass)
X_train = X_train.append(X_Embarked)

sc = StandardScalar()
X_train = sc.fit_transform(X_train)
X_train = sc.transform(X_train)

knn = KNeighborsClassifier(n_neighbors=5, metric='minkowski', p=2)
knn.fit(X_train, y_train)
示例#5
0
1)Building a diabetes classifier
You'll be using the Pima Indians diabetes dataset to predict whether a person has diabetes using logistic regression. 
There are 8 features and one target in this dataset. The data has been split into a training and test set and pre-loaded for you as X_train, y_train, X_test, and y_test.

A StandardScaler() instance has been predefined as scaler and a LogisticRegression() one as lr

"""

# Fit the scaler on the training features and transform these in one go
X_train_std = scaler.fit_transform(X_train)

# Fit the logistic regression model on the scaled training data
lr.fit(X_train_std, y_train)

# Scale the test features
X_test_std = scaler.transform(X_test)

# Predict diabetes presence on the scaled test set
y_pred = lr.predict(X_test_std)

# Prints accuracy metrics and feature coefficients
print("{0:.1%} accuracy on test set.".format(accuracy_score(y_test, y_pred)))
print(dict(zip(X.columns, abs(lr.coef_[0]).round(2))))
"""
79.6% accuracy on test set.
{'bmi': 0.38, 'insulin': 0.19, 'glucose': 1.23, 'diastolic': 0.03, 'family': 0.34, 'age': 0.34, 'triceps': 0.24, 'pregnant': 0.04}

Great! We get almost 80% accuracy on the test set. Take a look at the differences in model coefficients for the different features.
"""
"""
2) Manual Recursive Feature Elimination 
import pandas as pd

dataset = pd.read_csv('Social_Network.csv')
x = dataset.iloc[:, [2, 3]].values
y = dataset.iloc[:, 4].values

from sklearn.cross_validation import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,
                                                    y,
                                                    test_size=0.25,
                                                    random_state=0)

from sklearn.preprocessing import StandardScalar
sc_x = StandardScalar()
x_train = sc_x.fit_transform(x_train)
x_test = sc_x.transform(x_test)

from sklearn.linear_model import LogisticRegression
classifier = LogisticRegression(random_state=0)
classifier.fit(x_train, y_train)

y_pred = classifier.predict(x_test)

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

from matplotlib.colors import ListedColormap
x_set, y_set = x_train, y_train
x1, x2 = np.meshgrid(
    np.arange(start=x_set[:, 0].min() - 1,
              stop=x_set[:, 0].max() + 1,