PythonDataScience_XX - Jake VanderPlas Flashcards

1
Q

Wie kann ich one hot encoding verwenden?

Die Liste heißt data

Der Zielname vec

A

from sklearn.feature_extraction import DictVectorizer
vec = DictVectorizer(sparse=True, dtype=int)
vec.fit_transform(data)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Wie kann ich diese Arrays plotten?

x = np.array([1, 2, 3, 4, 5])
y = np.array([4, 2, 1, 3, 7])

import …

A

%matplotlib inline –> will lead to static images of your plot embedded in the notebook
import numpy as np
import matplotlib.pyplot as plt

x = np.array([1, 2, 3, 4, 5])
y = np.array([4, 2, 1, 3, 7])
plt.scatter(x, y);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Wie kann ich einfaches Model für lineare Regresion anwenden?

x = np.array([1, 2, 3, 4, 5])
y = np.array([4, 2, 1, 3, 7])

import …

X = …

model = …

yfit = …

A

from sklearn.linear_model import LinearRegression
X = x[:, np.newaxis]
model = LinearRegression().fit(X, y)
yfit = model.predict(X)
plt.scatter(x, y)
plt.plot(x, yfit);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Wie kann ich ein Model mit polynominellen Features anwenden?

x = np.array([1, 2, 3, 4, 5])
y = np.array([4, 2, 1, 3, 7])

import …

poly = …

X2 = …

model = …

yfit = …

plt…

A

from sklearn.preprocessing import PolynomialFeatures
poly = PolynomialFeatures(degree=3, include_bias=False)
X2 = poly.fit_transform(X)

model = LinearRegression().fit(X2, y)
yfit = model.predict(X2)

plt. scatter(x, y)
plt. plot(x, yfit);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Wie kann ich hier den durchschnittlichen Wert eintragen lassen? Danach lineare Regression.

from numpy import nan
X = np.array([[nan, 0, 3],
[3, 7, 9],
[3, 5, 2],
[4, nan, 6],
[8, 8, 1]])
y = np.array([14, 16, -1, 8, -5])

import …

imp = …

X2 = …

model = …

A

from sklearn.impute import SimpleImputer
imp = SimpleImputer(strategy=’mean’)
X2 = imp.fit_transform(X)

model = LinearRegression().fit(X2, y)
model.predict(X2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Wie kann ich eine Pipeline für die folgenden Aufgaben erstellen?

  • Impute missing values using the mean
  • Transform features to quadratic
  • Fit a linear regression

import = …

model = …

A

from sklearn.pipeline import make_pipeline

model = make_pipeline(SimpleImputer(strategy=’mean’),
PolynomialFeatures(degree=2),
LinearRegression())

model.fit(X, y) # X with missing values, from above
print(y)
print(model.predict(X))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Wie kann ich Test- und Trainigsdaten verwenden?

A
from sklearn.model\_selection import train\_test\_split
# split the data with 50% in each set
X1, X2, y1, y2 = train\_test\_split(X, y, random\_state=0,
 train\_size=0.5)

fit the model on one set of data
model.fit(X1, y1)

evaluate the model on the second set of data
y2_model = model.predict(X2)
accuracy_score(y2, y2_model)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Wie kann ich cross validation verwenden?

A

from sklearn.model_selection import cross_val_score
cross_val_score(model, X, y, cv=5)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Wie kann ich KNN verwenden?

A

from sklearn.neighbors import KNeighborsClassifier
model = KNeighborsClassifier(n_neighbors=1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Was ist eine Lernkurve?

A

Ein Diagramm mit dem Trainings- und Validierungsscore im Hinblick auf die Größe des Trainingsdatensatzes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Was ist Naive Bayes Modelle?

A

Naive Bayes models are a group of extremely fast and simple classification algorithms that are often suitable for very high-dimensional datasets. Because they are so fast and have so few tunable parameters, they end up being very useful as a quick-and-dirty baseline for a classification problem.

Eine Gruppe von einfachen Klassifizierungsalgorithmen, die oft sehr passend sind für hoch-dimensionale Datensätze. Sie sind schnell und haben wenige Hyperparameter –> gut als quick and dirty baseline für Klassifizierung.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Warum ist Naive Bayes naiv?

A

Naive Bayes (NB) is ‘naive’ because it makes the assumption that features of a measurement are independent of each other. This is naive because it is (almost) never true. Here is why NB works anyway.

NB is a very intuitive classification algorithm. It asks the question, “Given these features, does this measurement belong to class A or B?”, and answers it by taking the proportion of all previous measurements with the same features belonging to class A multiplied by the proportion of all measurements in class A. If this number is bigger then the corresponding calculation for class B then we say the measurement belongs in class A. Simple, right?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Wie könnte ich einen solchen Plot erstellen?

A

from sklearn.datasets import make_blobs
X, y = make_blobs(100, 2, centers=2, random_state=2, cluster_std=1.5)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap=’RdBu’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Wie könnte ich eine solche Grafik erzeugen?

A

from sklearn.datasets.samples_generator import make_blobs
X, y = make_blobs(n_samples=50, centers=2,
random_state=0, cluster_std=0.60)
plt.scatter(X[:, 0], X[:, 1], c=y, s=50, cmap=’autumn’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Wie kann ich einen SVC trainieren?

A

from sklearn.svm import SVC # “Support vector classifier”
model = SVC(kernel=’linear’, C=1E10)
model.fit(X, y)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Wie bekomme ich die Support Vektoren von model?

A

model.support_vectors_

17
Q

Wie kann ich bei SVC die Softness einstellen?

A

Mit dem Parameter C

Mit einem sehr großen C ist die Grenze sehr hart, es können also keine falsch klassifizierten Punkte drinnen liegen

Mit einem kleinen C wird die Grenze weicher und es können auch falsch klassifizierte im Bereich sein