Python: Removing Matrix columns that contain NaN

Removing Matrix columns that contain NaN. This is a lengthy answer, but hopefully easy to follow.
def column_to_vector(matrix, i):
    return [row[i] for row in matrix]
import numpy
def remove_NaN_columns(matrix):
    import scipy
    import math
    from numpy import column_stack, vstack

    columns = A.shape[1]
    #print("columns", columns)
    result = []
    skip_column = True
    for column in range(0, columns):
        vector = column_to_vector(A, column)
        skip_column = False
        for value in vector:
            # print(column, vector, value, math.isnan(value) )
            if math.isnan(value):
                skip_column = True
        if skip_column == False:
            result.append(vector)
    return column_stack(result)

### test it
A = vstack(([ float('NaN'), 2., 3., float('NaN')], [ 1., 2., 3., 9]))
print("A shape", A.shape, "\n", A)
B = remove_NaN_columns(A)
print("B shape", B.shape, "\n", B)

A shape (2, 4) 
 [[ nan   2.   3.  nan]
 [  1.   2.   3.   9.]]
B shape (2, 2) 
 [[ 2.  3.]
 [ 2.  3.]]


As an Amazon Associate I earn from qualifying purchases.

Python: Removing Matrix columns that contain NaN

Removing Matrix columns that contain NaN. This is a lengthy answer, but hopefully easy to follow.
def column_to_vector(matrix, i):
return [row[i] for row in matrix]
import numpy
def remove_NaN_columns(matrix):
import scipy
import math
from numpy import column_stack, vstack

columns
= A.shape[1]
#print("columns", columns)
result
= []
skip_column
= True
for column in range(0, columns):
vector
= column_to_vector(A, column)
skip_column
= False
for value in vector:
# print(column, vector, value, math.isnan(value) )
if math.isnan(value):
skip_column
= True
if skip_column == False:
result
.append(vector)
return column_stack(result)

### test it
A
= vstack(([ float('NaN'), 2., 3., float('NaN')], [ 1., 2., 3., 9]))
print("A shape", A.shape, "\n", A)
B
= remove_NaN_columns(A)
print("B shape", B.shape, "\n", B)

A shape
(2, 4)
[[ nan 2. 3. nan]
[ 1. 2. 3. 9.]]
B shape
(2, 2)
[[ 2. 3.]
[ 2. 3.]]


As an Amazon Associate I earn from qualifying purchases.

MacBook Pro WiFi problems

I was having problems with WiFi connections on both 2013 MacBook Pro 15 i7 and 2015 MacBook Pro 13 i5.

 To solve it I had to delete the following preferences file:

$ rm /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist


As an Amazon Associate I earn from qualifying purchases.

MacBook Pro WiFi problems

I was having problems with WiFi connections on both 2013 MacBook Pro 15 i7 and 2015 MacBook Pro 13 i5.

 To solve it I had to delete the following preferences file:

$ rm /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist


As an Amazon Associate I earn from qualifying purchases.

Can I hunt rabbits on public land in Illinois with a rifle?

Can I hunt rabbits on public land in Illinois with a rifle?

I found this quote:

Q: Can I use a centerfire rifle for hunting in Illinois? Or handguns? 

A: The following species may be taken with a rifle or handgun: coyote, striped skunk, woodchuck, squirrel, rabbit, raccoon, opossum and red and gray fox. We do not regulate the caliber of handgun or rifle that may be used, except on state-owned or managed areas, which normally allow only the use of shotguns or .22 caliber rimfire rifles.


https://www.dnr.illinois.gov/hunting/Documents/HuntTrapDigest.pdf 


As an Amazon Associate I earn from qualifying purchases.

Post Scriptum

The views in this article are mine and do not reflect those of my employer.
I am preparing to cancel the subscription to the e-mail newsletter that sends my articles.
Follow me on:
X.com (Twitter)
LinkedIn
Google Scholar

Popular Recent Posts

Most Popular Articles

apt quotation..