mean

Caching the mean of a Vector in R

有些话、适合烂在心里 提交于 2019-11-28 08:49:48
I am learning R and came across some code as part of the practice assignment. makeVector <- function(x = numeric()) { m <- NULL set <- function(y) { x <<- y m <<- NULL } get <- function() x setmean <- function(mean) m <<- mean getmean <- function() m list(set = set, get = get, setmean = setmean, getmean = getmean) } The documentation says: The function, makeVector creates a special "vector", which is really a list containing a function to set the value of the vector get the value of the vector set the value of the mean get the value of the mean But i can not understand how the function works

tensorlfow实现ZFNET

巧了我就是萌 提交于 2019-11-28 08:18:22
实现ZFNET网络的训练与验证部分 ZFNET 对ALexNet做了一些改进,但并不大,意义是可视化了卷积神经网络的内部参数。 本文训练使用的数据集是cifar-10。 网络架构(zfnet.py) import tensorflow as tf class ZFNet : def __init__ ( self , input_width = 224 , input_height = 224 , input_channels = 3 , num_classes = 1000 , learning_rate = 0.01 , momentum = 0.9 , keep_prob = 0.5 ) : self . input_width = input_width self . input_height = input_height self . input_channels = input_channels self . num_classes = num_classes self . learning_rate = learning_rate self . momentum = momentum self . keep_prob = keep_prob self . random_mean = 0 self . random_stddev = 0.01 with tf .

How to average over a cell-array of arrays?

白昼怎懂夜的黑 提交于 2019-11-28 07:25:23
问题 I have a cell array c of equal-sized arrays, i.e. size(c{n}) = [ m l ... ] for any n . How can I get the mean values (averaging over the cell array index n ) for all array elements in one sweep? I thought about using cell2mat and mean but the former does not add another dimension but changes l to l*n . And looping manually of course takes like forever... 回答1: If all of your arrays are the same size, it makes more sense to store them in a matrix rather than a cell array. That makes it easier

2.线性回归

☆樱花仙子☆ 提交于 2019-11-28 07:10:57
(一)简单线性回归 和之前介绍的KNN不同,KNN主要是解决分类问题,而线性回归顾名思义显然是用来解决回归问题的。线性回归具有如下特征: 解决回归问题 思想简单,实现容易 许多强大的非线性模型的基础,比如逻辑回归、多项式回归、svm等等 结果具有很好的可解释性 蕴含机器学习中的很多重要思想 图中是房屋的面积与价格之间的对应关系,不同的面积对应不同的价格,由此在二维平面中便形成了多个点。我们的目的就是要找到一条直线,最大程度上来拟合这些点。 但是在之前的KNN,分类问题中,横轴和纵轴都是样本的特征,而标签则是由这个点是红色还是蓝色决定的。 但是在线性回归中,由于是房产数据,我们必须要预测出一个具体的数值,而不能像分类问题那样,用简单的颜色来代表类别。而这些数据显然是在一个连续的样本空间中,因此需要一个坐标轴来表示。也正因为如此,在二维平面中只能有一个特征,要是多个特征,我们就要更高的维度上进行观察了。 如果样本的特征只有一个,我们称之为简单线性回归 我们的目的是要找到一个直线来尽可能多的拟合这些点,而在二维平面上显然是y = ax + b,那么每一个样本x,都会有一个真实值y和用拟合曲线预测出来的预测值ŷ,因此我们的真实值和预测值就会有一个差距 既然有真实值和预测值,那么评价一个直线的拟合程度,就看所有样本的真实值和预测值之差。如果只是简单的相减,那么两者之差可能有正有负,会抵消掉

Calculating weighted mean and standard deviation

浪子不回头ぞ 提交于 2019-11-28 06:48:33
I have a time series x_0 ... x_t . I would like to compute the exponentially weighted variance of the data. That is: V = SUM{w_i*(x_i - x_bar)^2, i=1 to T} where SUM{w_i} = 1 and x_bar=SUM{w_i*x_i} ref: http://en.wikipedia.org/wiki/Weighted_mean#Weighted_sample_variance The goal is to basically weight observations that are further back in time less. This is very simple to implement but I would like to use as much built in funcitonality as possible. Does anyone know what this corresponds to in R? Thanks R provides weighted mean. In fact, ?weighted.mean shows this example: ## GPA from Siegel

Different result for std between pandas and numpy

好久不见. 提交于 2019-11-28 06:05:41
问题 I am trying to subtract every element in the column from its mean and divide by the standard deviation. I did it in two different ways ( numeric_data1 and numeric_data2 ): import pandas as pd data = pd.read_csv("https://s3.amazonaws.com/demo-datasets/wine.csv") numeric_data = data.drop("color", 1) numeric_data1 = ((numeric_data - numeric_data.mean()) / numeric_data.std()) numeric_data2 = ((numeric_data - np.mean(numeric_data, axis=0)) / np.std(numeric_data, axis=0)) type(numeric_data1) # ->

Logistic回归

China☆狼群 提交于 2019-11-28 03:13:55
虽然Logistic回归叫回归,但是其实它是一个二分类或者多分类问题 这里的话我们使用信用诈骗的数据进行分析 第一步:导入数据,Amount的数值较大,后续将进行(-1,1)的归一化 data = pd.read_csv('creditcard.csv') #读取数据 #查看前5行数据 print(data.head()) 第二步: 对正常和欺诈的数目进行查看,正常样本的数目远大于欺诈样本,这个时候可以使用下采样或者过采样 # 画图查看 count_data = pd.value_counts(data['Class'], sort=True).sort_index() #统计样本数 count_data.plot(kind='bar') #画条形图 plt.title("Fraud class histogram") #标题 plt.xlabel('Classes') plt.ylabel('Frequency') plt.show() 第三步:将amount进行归一化形成amountNorm,并且去除time和amount项 #把amount数据标准化到-1, 1 from sklearn.preprocessing import StandardScaler #reshape 需要转换到的数值范围 data['NormAmount'] = StandardScaler()

Get mean of 2D slice of a 3D array in numpy

泄露秘密 提交于 2019-11-28 03:10:57
问题 I have a numpy array with a shape of: (11L, 5L, 5L) I want to calculate the mean over the 25 elements of each 'slice' of the array [0, :, :], [1, :, :] etc, returning 11 values. It seems silly, but I can't work out how to do this. I've thought the mean(axis=x) function would do this, but I've tried all possible combinations of axis and none of them give me the result I want. I can obviously do this using a for loop and slicing, but surely there is a better way? 回答1: Use a tuple for axis : >>>

np.mean() vs np.average() in Python NumPy?

余生长醉 提交于 2019-11-28 03:07:14
I notice that In [30]: np.mean([1, 2, 3]) Out[30]: 2.0 In [31]: np.average([1, 2, 3]) Out[31]: 2.0 However, there should be some differences, since after all they are two different functions. What are the differences between them? np.average takes an optional weight parameter. If it is not supplied they are equivalent. Take a look at the source code: Mean , Average np.mean: try: mean = a.mean except AttributeError: return _wrapit(a, 'mean', axis, dtype, out) return mean(axis, dtype, out) np.average: ... if weights is None : avg = a.mean(axis) scl = avg.dtype.type(a.size/avg.size) else: #code

How to Build a Deep Learning Project——With Keras

前提是你 提交于 2019-11-28 02:30:15
How to Build a Deep Learning Project——With Keras Step One: Data reading For cifar10, this step is very easy, Keras has already packaged it and split it into training data and testing data. from keras . datasets import cifar10 , cifar100 ( x_train , y_train ) , ( x_test , y_test ) = cifar10 . load_data ( ) For our DIY datasets, we should read and save the pictures one by one. def get_data ( dir ) ''' Return two lists ''' images = [ ] labels = [ ] dir = 'D:/' images_files = os . listdir ( dir + '/images' ) labels_files = os . listdir ( dir + '/labels' ) for x in images_files : images . append (