numpy 对矩阵中Nan的处理:采用平均值的方法

尽管我们可以将所有的NaN替换成0,但是由于并不知道这些值的意义,所以这样做是个下策。如果它们是开氏温度,那么将它们置成0这种处理策略就太差劲了。

下面我们用平均值来代替缺失值,平均值根据那些非NaN得到。

from numpy import *
datMat = mat([[1,2,3],[4,Nan,6]])
numFeat = shape(datMat)[1]
for i in range(numFeat):
  meanVal = mean(datMat[nonzero(~isnan(datMat[:,i].A))[0],i])
  #values that are not NaN (a number)
  datMat[nonzero(isnan(datMat[:,i].A))[0],i] = meanVal
  #set NaN values to mean

以上这篇numpy 对矩阵中Nan的处理:采用平均值的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • numpy求平均值的维度设定的例子

    废话不多说,我就直接上代码吧! >>> a = np.array([[1, 2], [3, 4]]) >>> np.mean(a) # 将上面二维矩阵的每个元素相加除以元素个数(求平均数) 2.5 >>> np.mean(a, axis=0) # axis=0,计算所有子数组的平均值 array([ 2., 3.]) >>> np.mean(a, axis=1) # axis=1,对每一个子数组,计算它的平均值 array([ 1.5

  • python 按不同维度求和,最值,均值的实例

    当变量维数加大时很难想象是怎样按不同维度求和的,高清楚一个,其他的应该就很清楚了,什么都不说了,上例子,例子一看便明白-.. a=range(27) a=np.array(a) a=np.reshape(a,[3,3,3]) 输出a的结果是: array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 2

  • python增加矩阵维度的实例讲解

    numpy.expand_dims(a, axis) Examples >>> x = np.array([1,2]) >>> x.shape (2,) >>> y = np.expand_dims(x, axis=0) >>> y array([[1, 2]]) >>> y.shape (1, 2) >>> y = np.expand_dims(x, axis=1) # Equivalent to

  • Numpy 改变数组维度的几种方法小结

    来自 <Python数据分析基础教程:Numpy 学习指南(第2版)> Numpy改变数组维度的方法有: reshape() ravel() flatten() 用元组设置维度 transpose() resize() 下面将依次进行说明 0. 首先,创建一个多维数组 from numpy import * a = arange(24) 得到: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23] 1.reshape

  • python处理二进制数据的方法

    本文实例讲述了python处理二进制数据的方法.分享给大家供大家参考.具体如下: #!/usr/env/env python #-*- coding: cp936 -*- ''''' add Head Infomation for pcm file ''' import sys import struct import os __author__ = 'bob_hu, hewitt924@gmail.com' __date__ = 'Dec 19,2011' __update__ = 'Dec

  • 对numpy中轴与维度的理解

    NumPy's main object is the homogeneous multidimensional array. It is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers. In NumPy dimensions are called axes. The number of axes is rank. For example, t

  • numpy 对矩阵中Nan的处理:采用平均值的方法

    尽管我们可以将所有的NaN替换成0,但是由于并不知道这些值的意义,所以这样做是个下策.如果它们是开氏温度,那么将它们置成0这种处理策略就太差劲了. 下面我们用平均值来代替缺失值,平均值根据那些非NaN得到. from numpy import * datMat = mat([[1,2,3],[4,Nan,6]]) numFeat = shape(datMat)[1] for i in range(numFeat): meanVal = mean(datMat[nonzero(~isnan(dat

  • python 判断矩阵中每行非零个数的方法

    如下所示: # -*- coding: utf-8 -*- # @Time : 2018/5/17 15:05 # @Author : Sizer # @Site : # @File : test.py # @Software: PyCharm import time import numpy as np # data = np.array([ # [5.0, 3.0, 4.0, 4.0, 0.0], # [3.0, 1.0, 2.0, 3.0, 3.0], # [4.0, 3.0, 4.0,

  • python中nan与inf转为特定数字方法示例

    前言 最近因为工作的需求,要处理两个矩阵的点除,得到结果后,再作其他的计算,发现有些内置的函数不work:查看得到的数据,发现有很多nan和inf,导致Python的基本函数运行不了,这是因为在除的过程中分母出现0的缘故.为了将结果能够被python其他函数处理,尤其numpy库,需要将nan,inf转为python所能识别的类型. 这里将nan,inf替换0作为例子.下面来看看详细的介绍: 1. 代码 import numpy as np a = np.array([[np.nan, np.n

  • Python Numpy:找到list中的np.nan值方法

    这个问题源于在训练机器学习的一个模型时,使用训练数据时提示prepare的数据中存在np.nan 报错信息如下: ValueError: np.nan is an invalid document, expected byte or unicode string. 刚开始不知道为什么会有这个,后来发现是list中存在nan值 下面是找到nan值的方法: 简单找到: import numpy as np x = np.array([2,3,np.nan,5, np.nan,5,2,3]) for

  • 在numpy矩阵中令小于0的元素改为0的实例

    如下所示: >>> import numpy as np >>> a = np.random.randint(-5, 5, (5, 5)) >>> a array([[-4, -4, -5, 2, 1], [-1, -2, -1, 3, 3], [-1, -2, 3, -5, 3], [ 0, -3, -5, 1, -4], [ 0, 3, 1, 3, -4]]) # 方式一 >>> np.maximum(a, 0) array([[

  • numpy和tensorflow中的各种乘法(点乘和矩阵乘)

    点乘和矩阵乘的区别: 1)点乘(即" * ") ---- 各个矩阵对应元素做乘法 若 w 为 m*1 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 m*n 的矩阵. 若 w 为 m*n 的矩阵,x 为 m*n 的矩阵,那么通过点乘结果就会得到一个 m*n 的矩阵. w的列数只能为 1 或 与x的列数相等(即n),w的行数与x的行数相等 才能进行乘法运算. 2)矩阵乘 ---- 按照矩阵乘法规则做运算 若 w 为 m*p 的矩阵,x 为 p*n 的矩阵,那么通过矩阵相乘结

  • Python numpy 提取矩阵的某一行或某一列的实例

    如下所示: import numpy as np a=np.arange(9).reshape(3,3) a Out[31]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) 矩阵的某一行 a[1] Out[32]: array([3, 4, 5]) 矩阵的某一列 a[:,1] Out[33]: array([1, 4, 7]) b=np.eye(3,3) b Out[36]: array([[ 1., 0., 0.], [ 0., 1., 0.], [ 0.,

  • Python3 中把txt数据文件读入到矩阵中的方法

    1.实例程序: ''' 数据文件:2.txt内容:(以空格分开每个数据) 1 2 2.5 3 4 4 7 8 7 ''' from numpy import * A = zeros((3,3),dtype=float) #先创建一个 3x3的全零方阵A,并且数据的类型设置为float浮点型 f = open('2.txt') #打开数据文件文件 lines = f.readlines() #把全部数据文件读到一个列表lines中 A_row = 0 #表示矩阵的行,从0行开始 for line

  • Python使用min、max函数查找二维数据矩阵中最小、最大值的方法

    本文实例讲述了Python使用min.max函数查找二维数据矩阵中最小.最大值的方法.分享给大家供大家参考,具体如下: 简单使用min.max函数来得到二维数据矩阵中的最大最小值,很简单,这是因为工作需要用到一个东西所以先简单来写了一下: #!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:找出来随机生成矩阵中的最大.最小值 ''' import time import random def random_matrix_ge

  • Python 使用Numpy对矩阵进行转置的方法

    如下所示: matrix.py #!/usr/bin/python # -*- encoding:UTF-8-*- import pprint import numpy as np matrix = [[1,2],[3,4],[5,6]] print('列表:') pprint.pprint(matrix) matrix_2 = np.matrix(matrix) print('原矩阵:') pprint.pprint(matrix_2) matrix_transpose = np.transp

随机推荐