numpy

Do what np.select(condlist, choicelist) does but instead with pandas only

佐手、 提交于 2021-01-29 18:32:28
问题 I have code and it works fine: import numpy as np import pandas as pd x = np.arange(10) condlist = [x<3, x==5, x>5] choicelist = [x, x**2, x**3] a=np.select(condlist, choicelist) Now lets add: y=pd.Series(x) Lets now use y instead of x . Now we need to get same result (same content as a has, and it should be Series.) with pandas only , and the conditions and choices should be coded as above (use above code for coding conditions and choices). How to do that? 回答1: construct a dataframe from

Given that i have a 2d array and i want to reshape it to 1d with one value per row

烈酒焚心 提交于 2021-01-29 18:15:09
问题 This is my array arr = np.array([[0, 1], [3, 4], [6, 7]]) flat_arr = np.reshape(arr, -1) am getting the following result: [0 1 2 3 4 5 6 7 8] my desired result is : [0] [1] [3] [4] [5]... 回答1: There are several ways to do it: flat_arr[:, None] flat_arr[:, np.newaxis] np.expand_dims(flat_arr, axis=1) Additionally, you could just reshape it like so: arr.reshape(-1, 1) 回答2: You can use this new shape: import numpy as np arr = np.array([[0, 1], [3, 4], [6, 7]]) flat_arr = np.reshape(arr, (arr

Indicating format of multiple numbers in numpy.savetxt

被刻印的时光 ゝ 提交于 2021-01-29 18:03:01
问题 I want to save an array using numpy.savetxt . The array contains eight numbers. Only the format of the first number is different with respect to the latter seven. I know I can set the format of numbers as follows: numpy.savetxt(filename, array, fmt = "%03d" "%.10f" "%.10f" "%.10f" "%.10f" "%.10f" "%.10f" "%.10f") Here filename is just the name of my file e.g. numbers.dat and array is a 1D numpy array containing my eight numbers. The above line of code works but looks ridiculous because I'm

总结:机器学习中的基本数学知识

℡╲_俬逩灬. 提交于 2021-01-29 17:46:20
注:本文的代码是使用Python 3写的。 机器学习中的基本数学知识 线性代数(linear algebra) 第一公式 矩阵的操作 换位(transpose) 矩阵乘法 矩阵的各种乘积 内积 外积 元素积(element-wise product/point-wise product/Hadamard product 加 低等数学 几何 范数(norm) 拉格朗日乘子法和KKT条件 微分(differential) 表示形式 法则 常见导数公式 统计学/概率论 信息论 香农熵(Shannon Entropy) 博弈论 不知道放到哪儿 机器学习 激活函数 损失函数 附录 希腊字母的含义和发音 数学符号的含义和发音 参照 线性代数(linear algebra) 第一公式 f(x)=xwT+b 这是在机器学习中,最常见的公式。我把这个称为机器学习的第一公式,实际上就是线性分类函数(linear classifier)。 训练分类器的目标就是求出 ( w , b ) (w,b) 。 其中: x x 是一个一行矩阵 [ [ x 1 , x 2 , . . . , x n ] ] [[x1,x2,...,xn]] 。 w w 是一个一行矩阵 [ [ w 1 , w 2 , . . . , w n ] ] [[w1,w2,...,wn]] 。 x x 和 w w 的维度相同。 b b

How do I plot an updating numpy ndarray in real time using matplotlib?

☆樱花仙子☆ 提交于 2021-01-29 17:29:41
问题 I have a numpy array which I initialized outside the loop using np.zeros. This array is updated using some function inside a for a loop. I wish to plot the array as it changes with each iteration. Most of the answers I have seen here are for lists and not ndarrays. I have seen the following links. Some of them I have tried to modify for my purpose but to no avail. How to update a plot in matplotlib? https://github.com/stsievert/python-drawnow/blob/master/drawnow/drawnow.py @Scott Sievert, I

PyTables create_array fails to save numpy array

假如想象 提交于 2021-01-29 17:27:03
问题 Why does the snipped below give: "TypeError: Array objects cannot currently deal with void, unicode or object arrays" ? Python 3.8.2, tables 3.6.1, numpy 1.19.1 import numpy as np import tables as tb TYPE = np.dtype([ ('d', 'f4') ]) with tb.open_file(r'c:\temp\file.h5', mode="a") as h5file: h5file.create_group(h5file.root, 'grp') arr = np.array([(1.1)], dtype=TYPE) h5file.create_array('/grp', str('arr'), arr) 回答1: File.create_array() is for homogeneous dtypes (all ints, or all floats, etc).

numpy - why Z[(0,2)] can be view for some cases and be copy in the others?

孤街醉人 提交于 2021-01-29 17:25:32
问题 Continuation to the question numpy - why Z[(0,2)] is view but Z[(0, 2), (0)] is copy?. I got the answer and understood that comma triggering advanced index makes a totally different indexing. The answer also provided a way using __array_interface__ to understand copy/view behavior. However apparently I have not got to the bottom of this part of the answer. A view is returned if the new array can be described with shape, strides and all or part of the original data buffer Because I still

how to write an algorithm for finding specific values of a numpy array in python

痴心易碎 提交于 2021-01-29 17:18:55
问题 I have a serie of lines and want to extract some of them. This is the number of lines: line_no= np.arange (17, 34) These lines are arranged in two perependicular direction. I have shown them with bluw and red lines in the fig. I know where the direction is changing, it is called sep: sep=25 # lines from 17 to 25 are blue and from 26 to end are red Then, I have the number of the points that create the lines. I call them chunks, because each number can be chunk: chunk_val=np.array([1,2,3,3,4])

Why does strptime from datetime not give me the correct hours?

*爱你&永不变心* 提交于 2021-01-29 16:43:07
问题 I am dealing with hourly data in which the date is stored in 4 different arrays, one for day, month, year, and hour. I am running a for-loop to rather store these dates as strings with this format: '01/01/1946 0' My code looks something like this: import numpy as np from datetime import datetime as dt import matplotlib.dates as mdates for nn in range(nnn): y1 = int(yr[nn]) m1 = int(mon[nn]) d1 = int(day[nn]) h1 = int(hr[nn]) #In the the last string we are specifying the format datenow = dt

Keras sequential network: Attempt to convert a value (75) with an unsupported type (<class 'numpy.int32'>) to a Tensor

那年仲夏 提交于 2021-01-29 16:33:58
问题 I am attempting to predict the ideal move in a game using a keras sequential network. The network is fairly simple with (what I think to be) an input shape of (3216,). The code for defining the network is attached bellow. self.model = keras.Sequential() self.model.add(Dense(2000, activation='relu', input_dim=(2 * 7 + 2 + Cfg.tiles_x * Cfg.tiles_y))) self.model.add(Dense(1000, activation='relu')) self.model.add(Dense(100, activation='relu')) self.model.add(Dense(9)) self.model.compile(loss