added PCA warm up
This commit is contained in:
@@ -143,6 +143,9 @@ MathJax.Hub.Config({
|
||||
|
||||
<h2 id="___sec6" class="anchor">Why should we think of reducing the dimensionality </h2>
|
||||
|
||||
<p>
|
||||
In addition to the plot of the features, we study now also the covariance (or rather the correlation matrix).
|
||||
We use also <b>Pandas</b> to compute the correlation matrix.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -152,16 +155,19 @@ MathJax.Hub.Config({
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #408080; font-style: italic"># Making a data frame</span>
|
||||
cancerpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
|
||||
|
||||
fig, axes <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(<span style="color: #666666">15</span>,<span style="color: #666666">2</span>,figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">20</span>))
|
||||
male <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">0</span>]
|
||||
bene <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">1</span>]
|
||||
malignant <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">0</span>]
|
||||
benign <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">1</span>]
|
||||
ax <span style="color: #666666">=</span> axes<span style="color: #666666">.</span>ravel()
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">30</span>):
|
||||
_, bins <span style="color: #666666">=</span> np<span style="color: #666666">.</span>histogram(cancer<span style="color: #666666">.</span>data[:,i], bins <span style="color: #666666">=50</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(male[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(bene[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(malignant[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(benign[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>set_title(cancer<span style="color: #666666">.</span>feature_names[i])
|
||||
ax[i]<span style="color: #666666">.</span>set_yticks(())
|
||||
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">"Feature magnitude"</span>)
|
||||
@@ -170,19 +176,27 @@ ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>le
|
||||
fig<span style="color: #666666">.</span>tight_layout()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Set up training data</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">sns</span>
|
||||
correlation_matrix <span style="color: #666666">=</span> cancerpd<span style="color: #666666">.</span>corr()<span style="color: #666666">.</span>round(<span style="color: #666666">1</span>)
|
||||
<span style="color: #408080; font-style: italic"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
||||
<span style="color: #408080; font-style: italic"># annot = True to print the values inside the square</span>
|
||||
sns<span style="color: #666666">.</span>heatmap(data<span style="color: #666666">=</span>correlation_matrix, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
<span style="color: #408080; font-style: italic">#split into train and test and then scale thereafter</span>
|
||||
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
|
||||
<span style="color: #408080; font-style: italic"># Perform Logistic Regression </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_train<span style="color: #666666">.</span>shape)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_test<span style="color: #666666">.</span>shape)
|
||||
|
||||
logreg <span style="color: #666666">=</span> LogisticRegression()
|
||||
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy from Logistic Regression: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Scale data</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> MinMaxScaler, StandardScaler
|
||||
scaler <span style="color: #666666">=</span> StandardScaler()
|
||||
scaler<span style="color: #666666">.</span>fit(X_train)
|
||||
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
|
||||
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
|
||||
|
||||
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
</pre></div>
|
||||
|
||||
@@ -420,6 +420,9 @@ logreg.fit(X_train_scaled, y_train)
|
||||
<section>
|
||||
<h2 id="___sec6">Why should we think of reducing the dimensionality </h2>
|
||||
|
||||
<p>
|
||||
In addition to the plot of the features, we study now also the covariance (or rather the correlation matrix).
|
||||
We use also <b>Pandas</b> to compute the correlation matrix.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -429,16 +432,19 @@ logreg.fit(X_train_scaled, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
|
||||
cancer = load_breast_cancer()
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
||||
<span style="color: #228B22"># Making a data frame</span>
|
||||
cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)
|
||||
|
||||
fig, axes = plt.subplots(<span style="color: #B452CD">15</span>,<span style="color: #B452CD">2</span>,figsize=(<span style="color: #B452CD">10</span>,<span style="color: #B452CD">20</span>))
|
||||
male = cancer.data[cancer.target == <span style="color: #B452CD">0</span>]
|
||||
bene = cancer.data[cancer.target == <span style="color: #B452CD">1</span>]
|
||||
malignant = cancer.data[cancer.target == <span style="color: #B452CD">0</span>]
|
||||
benign = cancer.data[cancer.target == <span style="color: #B452CD">1</span>]
|
||||
ax = axes.ravel()
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">30</span>):
|
||||
_, bins = np.histogram(cancer.data[:,i], bins =<span style="color: #B452CD">50</span>)
|
||||
ax[i].hist(male[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
||||
ax[i].hist(bene[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
||||
ax[i].hist(malignant[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
||||
ax[i].hist(benign[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
||||
ax[i].set_title(cancer.feature_names[i])
|
||||
ax[i].set_yticks(())
|
||||
ax[<span style="color: #B452CD">0</span>].set_xlabel(<span style="color: #CD5555">"Feature magnitude"</span>)
|
||||
@@ -447,19 +453,27 @@ ax[<span style="color: #B452CD">0</span>].legend([<span style="color: #CD5555">&
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
|
||||
<span style="color: #228B22"># Set up training data</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">seaborn</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">sns</span>
|
||||
correlation_matrix = cancerpd.corr().round(<span style="color: #B452CD">1</span>)
|
||||
<span style="color: #228B22"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
||||
<span style="color: #228B22"># annot = True to print the values inside the square</span>
|
||||
sns.heatmap(data=correlation_matrix, annot=<span style="color: #658b00">True</span>)
|
||||
plt.show()
|
||||
<span style="color: #228B22">#split into train and test and then scale thereafter</span>
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=<span style="color: #B452CD">0</span>)
|
||||
<span style="color: #228B22"># Perform Logistic Regression </span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X_train.shape)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X_test.shape)
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy: {:.2f}"</span>.format(logreg.score(X_test,y_test)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy from Logistic Regression: {:.2f}"</span>.format(logreg.score(X_test,y_test)))
|
||||
|
||||
<span style="color: #228B22"># Scale data</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> MinMaxScaler, StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
||||
</pre></div>
|
||||
|
||||
@@ -403,6 +403,9 @@ logreg.fit(X_train_scaled, y_train)
|
||||
|
||||
<h2 id="___sec6">Why should we think of reducing the dimensionality </h2>
|
||||
|
||||
<p>
|
||||
In addition to the plot of the features, we study now also the covariance (or rather the correlation matrix).
|
||||
We use also <b>Pandas</b> to compute the correlation matrix.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "perldoc" -->
|
||||
@@ -412,16 +415,19 @@ logreg.fit(X_train_scaled, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.datasets</span> <span style="color: #8B008B; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.linear_model</span> <span style="color: #8B008B; font-weight: bold">import</span> LogisticRegression
|
||||
cancer = load_breast_cancer()
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">pandas</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">pd</span>
|
||||
<span style="color: #228B22"># Making a data frame</span>
|
||||
cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)
|
||||
|
||||
fig, axes = plt.subplots(<span style="color: #B452CD">15</span>,<span style="color: #B452CD">2</span>,figsize=(<span style="color: #B452CD">10</span>,<span style="color: #B452CD">20</span>))
|
||||
male = cancer.data[cancer.target == <span style="color: #B452CD">0</span>]
|
||||
bene = cancer.data[cancer.target == <span style="color: #B452CD">1</span>]
|
||||
malignant = cancer.data[cancer.target == <span style="color: #B452CD">0</span>]
|
||||
benign = cancer.data[cancer.target == <span style="color: #B452CD">1</span>]
|
||||
ax = axes.ravel()
|
||||
|
||||
<span style="color: #8B008B; font-weight: bold">for</span> i <span style="color: #8B008B">in</span> <span style="color: #658b00">range</span>(<span style="color: #B452CD">30</span>):
|
||||
_, bins = np.histogram(cancer.data[:,i], bins =<span style="color: #B452CD">50</span>)
|
||||
ax[i].hist(male[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
||||
ax[i].hist(bene[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
||||
ax[i].hist(malignant[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
||||
ax[i].hist(benign[:,i], bins = bins, alpha = <span style="color: #B452CD">0.5</span>)
|
||||
ax[i].set_title(cancer.feature_names[i])
|
||||
ax[i].set_yticks(())
|
||||
ax[<span style="color: #B452CD">0</span>].set_xlabel(<span style="color: #CD5555">"Feature magnitude"</span>)
|
||||
@@ -430,19 +436,27 @@ ax[<span style="color: #B452CD">0</span>].legend([<span style="color: #CD5555">&
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
|
||||
<span style="color: #228B22"># Set up training data</span>
|
||||
<span style="color: #8B008B; font-weight: bold">import</span> <span style="color: #008b45; text-decoration: underline">seaborn</span> <span style="color: #8B008B; font-weight: bold">as</span> <span style="color: #008b45; text-decoration: underline">sns</span>
|
||||
correlation_matrix = cancerpd.corr().round(<span style="color: #B452CD">1</span>)
|
||||
<span style="color: #228B22"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
||||
<span style="color: #228B22"># annot = True to print the values inside the square</span>
|
||||
sns.heatmap(data=correlation_matrix, annot=<span style="color: #658b00">True</span>)
|
||||
plt.show()
|
||||
<span style="color: #228B22">#split into train and test and then scale thereafter</span>
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=<span style="color: #B452CD">0</span>)
|
||||
<span style="color: #228B22"># Perform Logistic Regression </span>
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X_train.shape)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(X_test.shape)
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy: {:.2f}"</span>.format(logreg.score(X_test,y_test)))
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy from Logistic Regression: {:.2f}"</span>.format(logreg.score(X_test,y_test)))
|
||||
|
||||
<span style="color: #228B22"># Scale data</span>
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> StandardScaler
|
||||
<span style="color: #8B008B; font-weight: bold">from</span> <span style="color: #008b45; text-decoration: underline">sklearn.preprocessing</span> <span style="color: #8B008B; font-weight: bold">import</span> MinMaxScaler, StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
<span style="color: #8B008B; font-weight: bold">print</span>(<span style="color: #CD5555">"Test set accuracy scaled data: {:.2f}"</span>.format(logreg.score(X_test_scaled,y_test)))
|
||||
</pre></div>
|
||||
|
||||
@@ -408,6 +408,9 @@ logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
|
||||
<h2 id="___sec6">Why should we think of reducing the dimensionality </h2>
|
||||
|
||||
<p>
|
||||
In addition to the plot of the features, we study now also the covariance (or rather the correlation matrix).
|
||||
We use also <b>Pandas</b> to compute the correlation matrix.
|
||||
<p>
|
||||
|
||||
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
||||
@@ -417,16 +420,19 @@ logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.datasets</span> <span style="color: #008000; font-weight: bold">import</span> load_breast_cancer
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.linear_model</span> <span style="color: #008000; font-weight: bold">import</span> LogisticRegression
|
||||
cancer <span style="color: #666666">=</span> load_breast_cancer()
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">pandas</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">pd</span>
|
||||
<span style="color: #408080; font-style: italic"># Making a data frame</span>
|
||||
cancerpd <span style="color: #666666">=</span> pd<span style="color: #666666">.</span>DataFrame(cancer<span style="color: #666666">.</span>data, columns<span style="color: #666666">=</span>cancer<span style="color: #666666">.</span>feature_names)
|
||||
|
||||
fig, axes <span style="color: #666666">=</span> plt<span style="color: #666666">.</span>subplots(<span style="color: #666666">15</span>,<span style="color: #666666">2</span>,figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">20</span>))
|
||||
male <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">0</span>]
|
||||
bene <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">1</span>]
|
||||
malignant <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">0</span>]
|
||||
benign <span style="color: #666666">=</span> cancer<span style="color: #666666">.</span>data[cancer<span style="color: #666666">.</span>target <span style="color: #666666">==</span> <span style="color: #666666">1</span>]
|
||||
ax <span style="color: #666666">=</span> axes<span style="color: #666666">.</span>ravel()
|
||||
|
||||
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">30</span>):
|
||||
_, bins <span style="color: #666666">=</span> np<span style="color: #666666">.</span>histogram(cancer<span style="color: #666666">.</span>data[:,i], bins <span style="color: #666666">=50</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(male[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(bene[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(malignant[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>hist(benign[:,i], bins <span style="color: #666666">=</span> bins, alpha <span style="color: #666666">=</span> <span style="color: #666666">0.5</span>)
|
||||
ax[i]<span style="color: #666666">.</span>set_title(cancer<span style="color: #666666">.</span>feature_names[i])
|
||||
ax[i]<span style="color: #666666">.</span>set_yticks(())
|
||||
ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>set_xlabel(<span style="color: #BA2121">"Feature magnitude"</span>)
|
||||
@@ -435,19 +441,27 @@ ax[<span style="color: #666666">0</span>]<span style="color: #666666">.</span>le
|
||||
fig<span style="color: #666666">.</span>tight_layout()
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Set up training data</span>
|
||||
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">seaborn</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">sns</span>
|
||||
correlation_matrix <span style="color: #666666">=</span> cancerpd<span style="color: #666666">.</span>corr()<span style="color: #666666">.</span>round(<span style="color: #666666">1</span>)
|
||||
<span style="color: #408080; font-style: italic"># use the heatmap function from seaborn to plot the correlation matrix</span>
|
||||
<span style="color: #408080; font-style: italic"># annot = True to print the values inside the square</span>
|
||||
sns<span style="color: #666666">.</span>heatmap(data<span style="color: #666666">=</span>correlation_matrix, annot<span style="color: #666666">=</span><span style="color: #008000">True</span>)
|
||||
plt<span style="color: #666666">.</span>show()
|
||||
<span style="color: #408080; font-style: italic">#split into train and test and then scale thereafter</span>
|
||||
X_train, X_test, y_train, y_test <span style="color: #666666">=</span> train_test_split(cancer<span style="color: #666666">.</span>data,cancer<span style="color: #666666">.</span>target,random_state<span style="color: #666666">=0</span>)
|
||||
<span style="color: #408080; font-style: italic"># Perform Logistic Regression </span>
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_train<span style="color: #666666">.</span>shape)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(X_test<span style="color: #666666">.</span>shape)
|
||||
|
||||
logreg <span style="color: #666666">=</span> LogisticRegression()
|
||||
logreg<span style="color: #666666">.</span>fit(X_train, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy from Logistic Regression: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test,y_test)))
|
||||
|
||||
<span style="color: #408080; font-style: italic"># Scale data</span>
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> StandardScaler
|
||||
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">sklearn.preprocessing</span> <span style="color: #008000; font-weight: bold">import</span> MinMaxScaler, StandardScaler
|
||||
scaler <span style="color: #666666">=</span> StandardScaler()
|
||||
scaler<span style="color: #666666">.</span>fit(X_train)
|
||||
X_train_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_train)
|
||||
X_test_scaled <span style="color: #666666">=</span> scaler<span style="color: #666666">.</span>transform(X_test)
|
||||
|
||||
logreg<span style="color: #666666">.</span>fit(X_train_scaled, y_train)
|
||||
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">"Test set accuracy scaled data: {:.2f}"</span><span style="color: #666666">.</span>format(logreg<span style="color: #666666">.</span>score(X_test_scaled,y_test)))
|
||||
</pre></div>
|
||||
|
||||
@@ -283,7 +283,10 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Why should we think of reducing the dimensionality"
|
||||
"## Why should we think of reducing the dimensionality\n",
|
||||
"\n",
|
||||
"In addition to the plot of the features, we study now also the covariance (or rather the correlation matrix).\n",
|
||||
"We use also **Pandas** to compute the correlation matrix."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -300,16 +303,19 @@
|
||||
"from sklearn.datasets import load_breast_cancer\n",
|
||||
"from sklearn.linear_model import LogisticRegression\n",
|
||||
"cancer = load_breast_cancer()\n",
|
||||
"import pandas as pd\n",
|
||||
"# Making a data frame\n",
|
||||
"cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)\n",
|
||||
"\n",
|
||||
"fig, axes = plt.subplots(15,2,figsize=(10,20))\n",
|
||||
"male = cancer.data[cancer.target == 0]\n",
|
||||
"bene = cancer.data[cancer.target == 1]\n",
|
||||
"malignant = cancer.data[cancer.target == 0]\n",
|
||||
"benign = cancer.data[cancer.target == 1]\n",
|
||||
"ax = axes.ravel()\n",
|
||||
"\n",
|
||||
"for i in range(30):\n",
|
||||
" _, bins = np.histogram(cancer.data[:,i], bins =50)\n",
|
||||
" ax[i].hist(male[:,i], bins = bins, alpha = 0.5)\n",
|
||||
" ax[i].hist(bene[:,i], bins = bins, alpha = 0.5)\n",
|
||||
" ax[i].hist(malignant[:,i], bins = bins, alpha = 0.5)\n",
|
||||
" ax[i].hist(benign[:,i], bins = bins, alpha = 0.5)\n",
|
||||
" ax[i].set_title(cancer.feature_names[i])\n",
|
||||
" ax[i].set_yticks(())\n",
|
||||
"ax[0].set_xlabel(\"Feature magnitude\")\n",
|
||||
@@ -318,19 +324,27 @@
|
||||
"fig.tight_layout()\n",
|
||||
"plt.show()\n",
|
||||
"\n",
|
||||
"# Set up training data\n",
|
||||
"import seaborn as sns\n",
|
||||
"correlation_matrix = cancerpd.corr().round(1)\n",
|
||||
"# use the heatmap function from seaborn to plot the correlation matrix\n",
|
||||
"# annot = True to print the values inside the square\n",
|
||||
"sns.heatmap(data=correlation_matrix, annot=True)\n",
|
||||
"plt.show()\n",
|
||||
"#split into train and test and then scale thereafter\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)\n",
|
||||
"# Perform Logistic Regression \n",
|
||||
"print(X_train.shape)\n",
|
||||
"print(X_test.shape)\n",
|
||||
"\n",
|
||||
"logreg = LogisticRegression()\n",
|
||||
"logreg.fit(X_train, y_train)\n",
|
||||
"print(\"Test set accuracy: {:.2f}\".format(logreg.score(X_test,y_test)))\n",
|
||||
"print(\"Test set accuracy from Logistic Regression: {:.2f}\".format(logreg.score(X_test,y_test)))\n",
|
||||
"\n",
|
||||
"# Scale data\n",
|
||||
"from sklearn.preprocessing import StandardScaler\n",
|
||||
"from sklearn.preprocessing import MinMaxScaler, StandardScaler\n",
|
||||
"scaler = StandardScaler()\n",
|
||||
"scaler.fit(X_train)\n",
|
||||
"X_train_scaled = scaler.transform(X_train)\n",
|
||||
"X_test_scaled = scaler.transform(X_test)\n",
|
||||
"\n",
|
||||
"logreg.fit(X_train_scaled, y_train)\n",
|
||||
"print(\"Test set accuracy scaled data: {:.2f}\".format(logreg.score(X_test_scaled,y_test)))"
|
||||
]
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -251,6 +251,8 @@ print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,
|
||||
!split
|
||||
===== Why should we think of reducing the dimensionality =====
|
||||
|
||||
In addition to the plot of the features, we study now also the covariance (or rather the correlation matrix).
|
||||
We use also _Pandas_ to compute the correlation matrix.
|
||||
!bc pycod
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
@@ -258,16 +260,19 @@ from sklearn.model_selection import train_test_split
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
cancer = load_breast_cancer()
|
||||
import pandas as pd
|
||||
# Making a data frame
|
||||
cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)
|
||||
|
||||
fig, axes = plt.subplots(15,2,figsize=(10,20))
|
||||
male = cancer.data[cancer.target == 0]
|
||||
bene = cancer.data[cancer.target == 1]
|
||||
malignant = cancer.data[cancer.target == 0]
|
||||
benign = cancer.data[cancer.target == 1]
|
||||
ax = axes.ravel()
|
||||
|
||||
for i in range(30):
|
||||
_, bins = np.histogram(cancer.data[:,i], bins =50)
|
||||
ax[i].hist(male[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].hist(bene[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].hist(malignant[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].hist(benign[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].set_title(cancer.feature_names[i])
|
||||
ax[i].set_yticks(())
|
||||
ax[0].set_xlabel("Feature magnitude")
|
||||
@@ -276,22 +281,34 @@ ax[0].legend(["Malignant", "Benign"], loc ="best")
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
|
||||
# Set up training data
|
||||
import seaborn as sns
|
||||
correlation_matrix = cancerpd.corr().round(1)
|
||||
# use the heatmap function from seaborn to plot the correlation matrix
|
||||
# annot = True to print the values inside the square
|
||||
sns.heatmap(data=correlation_matrix, annot=True)
|
||||
plt.show()
|
||||
#split into train and test and then scale thereafter
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
|
||||
# Perform Logistic Regression
|
||||
print(X_train.shape)
|
||||
print(X_test.shape)
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
print("Test set accuracy: {:.2f}".format(logreg.score(X_test,y_test)))
|
||||
print("Test set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
|
||||
|
||||
# Scale data
|
||||
from sklearn.preprocessing import StandardScaler
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
!ec
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.datasets import load_breast_cancer
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
cancer = load_breast_cancer()
|
||||
import pandas as pd
|
||||
|
||||
cancerpd = pd.DataFrame(cancer.data, columns=cancer.feature_names)
|
||||
|
||||
fig, axes = plt.subplots(15,2,figsize=(10,20))
|
||||
malignant = cancer.data[cancer.target == 0]
|
||||
benign = cancer.data[cancer.target == 1]
|
||||
ax = axes.ravel()
|
||||
|
||||
for i in range(30):
|
||||
_, bins = np.histogram(cancer.data[:,i], bins =50)
|
||||
ax[i].hist(malignant[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].hist(benign[:,i], bins = bins, alpha = 0.5)
|
||||
ax[i].set_title(cancer.feature_names[i])
|
||||
ax[i].set_yticks(())
|
||||
ax[0].set_xlabel("Feature magnitude")
|
||||
ax[0].set_ylabel("Frequency")
|
||||
ax[0].legend(["Malignant", "Benign"], loc ="best")
|
||||
fig.tight_layout()
|
||||
plt.show()
|
||||
|
||||
import seaborn as sns
|
||||
correlation_matrix = cancerpd.corr().round(1)
|
||||
# use the heatmap function from seaborn to plot the correlation matrix
|
||||
# annot = True to print the values inside the square
|
||||
sns.heatmap(data=correlation_matrix, annot=True)
|
||||
plt.show()
|
||||
X_train, X_test, y_train, y_test = train_test_split(cancer.data,cancer.target,random_state=0)
|
||||
print(X_train.shape)
|
||||
print(X_test.shape)
|
||||
|
||||
logreg = LogisticRegression()
|
||||
logreg.fit(X_train, y_train)
|
||||
print("Test set accuracy from Logistic Regression: {:.2f}".format(logreg.score(X_test,y_test)))
|
||||
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
||||
scaler = StandardScaler()
|
||||
scaler.fit(X_train)
|
||||
X_train_scaled = scaler.transform(X_train)
|
||||
X_test_scaled = scaler.transform(X_test)
|
||||
|
||||
logreg.fit(X_train_scaled, y_train)
|
||||
print("Test set accuracy scaled data: {:.2f}".format(logreg.score(X_test_scaled,y_test)))
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user