想了解Pythonnumpy模块-rot90()实例源码的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于python中numpy模块的相关问题,此外,我们还将为您介绍关于Jupyter中的N
想了解Python numpy 模块-rot90() 实例源码的新动态吗?本文将为您提供详细的信息,我们还将为您解答关于python中numpy模块的相关问题,此外,我们还将为您介绍关于Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable、numpy.random.random & numpy.ndarray.astype & numpy.arange、numpy.ravel()/numpy.flatten()/numpy.squeeze()、Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性的新知识。
本文目录一览:- Python numpy 模块-rot90() 实例源码(python中numpy模块)
- Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable
- numpy.random.random & numpy.ndarray.astype & numpy.arange
- numpy.ravel()/numpy.flatten()/numpy.squeeze()
- Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性
Python numpy 模块-rot90() 实例源码(python中numpy模块)
Python numpy 模块,rot90() 实例源码
我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.rot90()。
- def inner_extract(self,B,signature):
- w,h =B.shape[:2]
- LL,(HL,LH,HH) = pywt.dwt2(B[:32*(w//32),:32*(h//32)],''haar'')
- LL_1,(HL_1,LH_1,HH_1) = pywt.dwt2(LL,''haar'')
- LL_2,(HL_2,LH_2,HH_2) = pywt.dwt2(LL_1,''haar'')
- LL_3,(HL_3,LH_3,HH_3) = pywt.dwt2(LL_2,''haar'')
- LL_4,(HL_4,LH_4,HH_4) = pywt.dwt2(LL_3,''haar'')
- _,_,ori_sig = self._gene_embed_space(HH_3)
- ext_sigs=[]
- ext_sigs.extend(self._extract_sig(ori_sig,len(signature)))
- ext_sigs.extend(self._extract_sig(np.rot90(ori_sig,1),2),3),len(signature)))
- return ext_sigs
- def mk_rotations(img):
- #
- # DESCRIPTION
- # This function create 8 roatation image fro an input image 4 rotation from the raw image and 4 rotation form the transposed
- #
- # INPUTS
- # img np.array
- #
- # OUTPUTS
- # rotated_image_img,img90,img180,img270,imgT,imgT90,imgT180,imgT270
- #
- #
- img90 = np.rot90(img)
- img180 = np.rot90(img,k=2)
- img270 = np.rot90(img,k=3)
- imgT = np.zeros(img.shape)
- if np.size(img.shape)>2:
- for i in range(3):
- imgT[:,:,i] =img[:,i].T
- else:
- imgT = img.T
- imgT90 = np.rot90(imgT)
- imgT180 = np.rot90(imgT, k=2)
- imgT270 = np.rot90(imgT, k=3)
- return img, img90, img180, img270, imgT, imgT90, imgT180,imgT270
- def unrotate(rot0, rot1, rot2, rot3, rot4, rot5, rot6, rot7):
- #
- # DESCRIPTION
- # Functions that merges the 8 mapped images as described in the beginning of the file back to the original format
- # Uses element wise product
- #
- #
- unrot = np.copy(rot0)
- unrot*=np.rot90((rot1),k=3)
- unrot*=np.rot90((rot2),k=2)
- unrot*=np.rot90((rot3),k=1)
- unrot*=(rot4.T)
- unrot*=np.rot90((rot5),k=3).T
- unrot*=np.rot90((rot6),k=2).T
- unrot*=np.rot90((rot7),k=1).T
- return unrot
- ## ##
- ## ##
- ## EXECUTION ##
- ## ##
- ## ##
- def mk_rotations(img):
- ##INPUT:
- ## img: a 3D RGB array
- ##OUTPUT
- ## 8 rotated and transposed versions of img
- img90 = np.rot90(img)
- img180 = np.rot90(img,imgT270
- ## Formats an image to save format
- def getMagSpec(sig, rate, winlen, winstep, nfft):
- #get frames
- winfunc = lambda x:np.ones((x,))
- frames = psf.sigproc.framesig(sig, winlen*rate, winstep*rate, winfunc)
- #Magnitude Spectrogram
- magspec = np.rot90(psf.sigproc.magspec(frames, nfft))
- return magspec
- #Split signal into five-second chunks with overlap of 4 and minimum length of 3 seconds
- #Use these settings for other chunk lengths:
- #winlen,winstep,seconds
- #0.05,0.0097,5s
- #0.05,0.0195,10s
- #0.05,0.0585,30s
- def getMagSpec(sig, nfft))
- return magspec
- #Split signal into five-second chunks with overlap of 4 and minimum length of 1 second
- #Use these settings for other chunk lengths:
- #winlen,30s
- def test_discrete_phantom_uniform():
- """The uniform discrete phantom is the same after rotating 90 degrees."""
- d0 = discrete_phantom(p, 100, ratio=10, prop=''mass_atten'')
- p.rotate(theta=np.pi/2, point=Point([0.5, 0.5]))
- d1 = np.rot90(discrete_phantom(p, prop=''mass_atten''))
- # plot rotated phantom
- plot_phantom(p)
- # plot the error
- plt.figure()
- plt.imshow(d1-d0, interpolation=None)
- plt.colorbar()
- # plt.show(block=True)
- # assert_allclose(d0,d1)
- def load_dotted_image(self, train_id, scale=1, border=0, circled=False):
- img = self._load_image(''dotted'', scale, border)
- if train_id in self.extra_masks:
- for row, col, radius in self.extra_masks[train_id]:
- rr, cc = skimage.draw.circle(row, radius, shape = img.shape)
- img = np.copy(img)
- img[rr, cc] = (0, 0, 0)
- # When dotted image is rotated relative to train,apply hot patch. (kudos: @authman)
- if train_id in self.dotted_rotate:
- rot = self.dotted_rotate[train_id]
- img = np.rot90(img, rot)
- if circled:
- assert scale == 1
- assert border == 0
- img = np.copy(img)
- img = self.draw_circles(np.copy(img), self.tid_coords[train_id])
- return img
- def get_lateral(self, resolution=0.5):
- if hasattr(self, "lateral") and resolution == resolution:
- return self.lateral
- max_dist = self.get_max_dist()
- dim = np.ceil(np.absolute(max_dist / resolution))
- max_dist = dim * resolution
- self.resolution = resolution
- a = np.meshgrid(np.linspace(0, max_dist, dim), np.linspace(0, dim))
- r = (a[0]**2 + a[1]**2)**0.5
- sigma = self.sigma / ((8 * log(2))**0.5)
- lateral = 1 / ((2 * pi * sigma**2)**0.5) * np.exp(-(r**2) / (2 * sigma**2))
- tot_lat = np.zeros((2 * dim - 1, 2 * dim - 1))
- tot_lat[dim - 1:2 * dim - 1, dim - 1:2 * dim - 1] = lateral
- tot_lat[dim - 1:2 * dim - 1, 0:dim - 1] = np.rot90(lateral, 3)[:, 0:dim - 1]
- tot_lat[0:dim - 1, 2)[0:dim - 1, dim - 1:2 * dim - 1] = np.rot90(lateral)[0:dim - 1, :]
- self.lateral = tot_lat
- return self.lateral
- def _apply_transformations(plot_config, data_slice):
- """Rotate,flip and zoom the data slice.
- Depending on the plot configuration,this will apply some transformations to the given data slice.
- Args:
- plot_config (mdt.visualization.maps.base.MapPlotConfig): the plot configuration
- data_slice (ndarray): the 2d slice of data to transform
- Returns:
- ndarray: the transformed 2d slice of data
- """
- if plot_config.rotate:
- data_slice = np.rot90(data_slice, plot_config.rotate // 90)
- if not plot_config.flipud:
- # by default we flipud to correct for matplotlib lower origin. If the user
- # sets flipud,we do not need to to it
- data_slice = np.flipud(data_slice)
- data_slice = plot_config.zoom.apply(data_slice)
- return data_slice
- def _augment_chunks(self, chunks):
- if self.choices_augmentation is None:
- return chunks
- chunks_new = []
- choice = np.random.choice(self.choices_augmentation)
- for chunk in chunks:
- chunk_new = chunk
- if choice in [1, 3, 5, 7]:
- chunk_new = np.flip(chunk_new, axis=1)
- if choice in [2, 3]:
- chunk_new = np.rot90(chunk_new, 1, axes=(1, 2))
- elif choice in [4, 5]:
- chunk_new = np.rot90(chunk_new, 2, 2))
- elif choice in [6, 7]:
- chunk_new = np.rot90(chunk_new, 2))
- chunks_new.append(chunk_new)
- return chunks_new
- def random_augment_image(image, row):
- # start0_max,end0_max,start1_max,end1_max = get_bounding_Boxes_positions(image,row)
- # image = cv2.rectangle(image,(int(start1_max),int(start0_max)),(int(end1_max),int(end0_max)),(0,255),thickness=5)
- if random.randint(0, 1) == 0:
- image = return_random_crop(image, row)
- else:
- image = return_random_perspective(image, row)
- image = random_rotate(image)
- # all possible mirroring and flips (in total there are only 8 possible configurations)
- mirror = random.randint(0, 1)
- if mirror != 0:
- image = image[::-1, :, :]
- angle = random.randint(0, 3)
- if angle != 0:
- image = np.rot90(image, k=angle)
- image = lightning_change(image)
- image = blur_image(image)
- return image
- def rot(self):
- for i in range(len(self.Q)):
- Q = self.Q[i]
- ans = self.ans[i]
- refarea = self.area[i]
- refMask = self.mask[i]
- for rotate in range(3):
- self.Q.append(Q)
- self.ans.append(ans)
- refarea = np.rot90(refarea)
- refMask = np.rot90(refMask)
- self.area.append(refarea)
- self.mask.append(refMask)
- return self
- def rot90(img):
- ''''''
- rotate one or multiple grayscale or color images 90 degrees
- ''''''
- s = img.shape
- if len(s) == 3:
- if s[2] in (3, 4): # color image
- out = np.empty((s[1], s[0], s[2]), dtype=img.dtype)
- for i in range(s[2]):
- out[:, i] = np.rot90(img[:, i])
- else: # mutliple grayscale
- out = np.empty((s[0], s[2], s[1]), dtype=img.dtype)
- for i in range(s[0]):
- out[i] = np.rot90(img[i])
- elif len(s) == 2: # one grayscale
- out = np.rot90(img)
- elif len(s) == 4 and s[3] in (3, 4): # multiple color
- out = np.empty((s[0], s[1], s[3]), dtype=img.dtype)
- for i in range(s[0]): # for each img
- for j in range(s[3]): # for each channel
- out[i, j] = np.rot90(img[i, j])
- else:
- NotImplemented
- return out
- def shot_heatmap(df,sigma = 1,log=False,player_pic=True,ax=None,cmap=''jet''):
- ''''''
- This function plots a heatmap based on the shot chart.
- input - dataframe with x and y coordinates.
- optional - log (default false) plots heatmap in log scale.
- player (default true) adds player''s picture and name if true
- sigma - the sigma of the Gaussian kernel. In feet (default=1)
- ''''''
- n,_ = np.histogram2d( 0.1*df[''LOC_X''].values, 0.1*df[''LOC_Y''].values,bins = [500, 500],range = [[-25,25],[-5.25,44.75]])
- KDE = ndimage.filters.gaussian_filter(n,10.0*sigma)
- N = 1.0*KDE/np.sum(KDE)
- if ax is None:
- ax = plt.gca(xlim = [30,-30],ylim = [-7,43],xticks=[],yticks=[],aspect=1.0)
- court(ax,outer_lines=True,color=''black'',lw=2.0,direction=''down'')
- ax.axis(''off'')
- if log:
- ax.imshow(np.rot90(np.log10(N+1)),cmap=cmap,extent=[25.0, -25.0, -5.25, 44.75])
- else:
- ax.imshow(np.rot90(N), 44.75])
- if player_pic:
- player_id = df.PLAYER_ID.values[0]
- pic = players_picture(player_id)
- ax.imshow(pic,extent=[15,25,30,37.8261])
- ax.text(0,-7,''By: Doingthedishes'',color=''white'',horizontalalignment=''center'',fontsize=20,fontweight=''bold'')
- def rot90(self, turns=1):
- """
- Rotates the blocks in the counter-clockwise direction. (As numpy
- does it.)
- """
- # Rotate the individual Y-layer matrices
- new_blocks = np.array([np.rot90(by, turns) for by in self.blocks])
- new_data = np.array([np.rot90(dy, turns) for dy in self.data])
- new_mask = np.array([np.rot90(my, turns) for my in self.mask])
- # Rotate the data (if applicable)
- for y in xrange(new_data.shape[0]):
- for z in xrange(new_data.shape[1]):
- for x in xrange(new_data.shape[2]):
- b = new_blocks[y, z, x]
- d = new_data[y, x]
- new_data[y, x] = self.data_rot90(b, d, turns)
- return MaskedSubChunk(new_blocks, new_data, new_mask)
- def data_rot90(self, block, data, turns):
- """
- Specially rotate this block,which has an orientation that depends on
- the data value.
- """
- blockname = blocks.block_names[block]
- # Torches (redstone and normal)
- torches = ["redstone_torch", "unlit_redstone_torch", "torch"]
- if blockname in torches:
- return blocks.Torch.rot90(data, turns)
- # Repeaters
- repeaters = ["unpowered_repeater", "powered_repeater"]
- if blockname in repeaters:
- return blocks.Repeater.rot90(data, turns)
- # Comparators
- comparators = ["unpowered_comparator", "powered_comparator"]
- if blockname in comparators:
- return blocks.Comparator.rot90(data, turns)
- return data
- def _display_pixels(x, y, counts, pixelsize):
- """
- display pixels at coordinates (x,y) coloured with "counts".
- This routine is fast but not fully general as it assumes the spaxels
- are on a regular grid. This needs not be the case for Voronoi binning.
- """
- xmin, xmax = np.min(x), np.max(x)
- ymin, ymax = np.min(y), np.max(y)
- nx = int(round((xmax - xmin)/pixelsize) + 1)
- ny = int(round((ymax - ymin)/pixelsize) + 1)
- img = np.full((nx, ny), np.nan) # use nan for missing data
- j = np.round((x - xmin)/pixelsize).astype(int)
- k = np.round((y - ymin)/pixelsize).astype(int)
- img[j, k] = counts
- plt.imshow(np.rot90(img), interpolation=''nearest'', cmap=''prism'',
- extent=[xmin - pixelsize/2, xmax + pixelsize/2,
- ymin - pixelsize/2, ymax + pixelsize/2])
- #----------------------------------------------------------------------
- def visualize2D(fig, ax, xs, ys, bins=200,
- xlabel=''x'', ylabel=''y'',
- xlim=None, ylim=None):
- H, xedges, yedges = numpy.histogram2d(xs, bins)
- H = numpy.rot90(H)
- H = numpy.flipud(H)
- Hmasked = numpy.ma.masked_where(H == 0, H)
- ax.pcolormesh(xedges, yedges, Hmasked)
- ax.set_xlabel(xlabel)
- ax.set_ylabel(ylabel)
- if xlim is None:
- xlim = (min(xs), max(xs))
- if ylim is None:
- ylim = (min(ys), max(ys))
- ax.set_xlim(*xlim)
- ax.set_ylim(*ylim)
- fig.colorbar(pyplot.contourf(Hmasked))
- def rot180(images):
- """
- ????180??
- ??HW/CHW/NCHW?????images?
- """
- out = np.empty(shape=images.shape, dtype=images.dtype)
- if images.ndim == 2:
- out = np.rot90(images, k=2)
- elif images.ndim == 3:
- for c in xrange(images.shape[0]):
- out[c] = np.rot90(images[c], k=2)
- elif images.ndim == 4:
- for n in xrange(images.shape[0]):
- for c in xrange(images.shape[1]):
- out[n][c] = np.rot90(images[n][c], k=2)
- else:
- raise Exception(''Invalid ndim: '' + str(images.ndim) +
- '',only support ndim between 2 and 4.'')
- return out
- # ????f????x???f???df???x???
- def grad_magnitude(img):
- """Calculate the gradient magnitude of an image.
- Args:
- img The image
- Returns:
- gradient image"""
- img = img / 255.0
- sobel_y = np.array([
- [-1, -2, -1],
- [0, 0],
- [1, 1]
- ])
- sobel_x = np.rot90(sobel_y) # rotates counter-clockwise
- # apply x/y sobel filter to get x/y gradients
- imgx = signal.correlate(img, sobel_x, mode="same")
- imgy = signal.correlate(img, sobel_y, mode="same")
- imgmag = np.sqrt(imgx**2 + imgy**2)
- return imgmag
- def main():
- """Load image,apply filter,plot."""
- img = data.camera()
- # just like sobel,but no -2/+2 in the middle
- prewitt_y = np.array([
- [-1, -1, 1]
- ])
- prewitt_x = np.rot90(prewitt_y) # rotates counter-clockwise
- img_sx = signal.correlate(img, prewitt_x, mode="same")
- img_sy = signal.correlate(img, prewitt_y, mode="same")
- g_magnitude = np.sqrt(img_sx**2 + img_sy**2)
- ground_truth = skifilters.prewitt(data.camera())
- util.plot_images_grayscale(
- [img, img_sx, img_sy, g_magnitude, ground_truth],
- ["Image", "Prewitt (x)", "Prewitt (y)", "Prewitt (both/magnitude)",
- "Prewitt (Ground Truth)"]
- )
- def augment_image(self, image, i):
- if i == 0:
- return np.rot90(image)
- elif i == 1:
- return np.rot90(image,2)
- elif i == 2:
- return np.rot90(image,3)
- elif i == 3:
- return image
- elif i == 4:
- return np.fliplr(image)
- elif i == 5:
- return np.flipud(image)
- elif i == 6:
- return image.transpose(1,0,2)
- elif i == 7:
- return np.fliplr(np.rot90(image))
- def augment_image(self,2)
- elif i == 7:
- return np.fliplr(np.rot90(image))
- def test_get_geo_fact():
- res = np.array([0.017051023225738, 0.020779123804907, -0.11077204227395,
- -0.081155809427821, -0.098900024313067, 0.527229048585517,
- -0.124497144079623, -0.151717673241039, 0.808796206796408])
- res2 = np.rot90(np.fliplr(res.reshape(3, -1))).ravel()
- # EE,MM
- ab = [11, 12, 13, 21, 22, 23, 31, 32, 33]
- i = 0
- for i in range(9):
- out = utils.get_geo_fact(ab[i], 13.45, 23.8, 124.3, 5.3, False, False)
- assert_allclose(out[0], res[i])
- out = utils.get_geo_fact(ab[i], True, True)
- assert_allclose(out[0], res[i])
- i += 1
- # ME,EM
- ab = [14, 15, 16, 24, 25, 26, 34, 35, 36]
- i = 0
- for i in range(9):
- out = utils.get_geo_fact(ab[i], res2[i])
- out = utils.get_geo_fact(ab[i], res[i])
- i += 1
- def rotate_examples(X, files, extent, k=3):
- m,n = np.shape(X)
- augmentedX = np.ones(((k+1)*m,n))
- augmentedy = np.squeeze(np.ones(((k+1)*m,)))
- augmented_files = []
- for i in range(m):
- #print y[i]
- print((k+1)*i)
- augmentedX[(k+1)*i,:] *= X[i,:]
- augmentedy[(k+1)*i] *= y[i]
- #print augmentedy[(k+1)*i]
- augmented_files.append(files[i])
- for j in range(1,k+1):
- print(((k+1)*i)+j)
- rotatedX = np.rot90(np.reshape(X[i,:], (2*extent,2*extent), order="F"), j)
- augmentedX[((k+1)*i)+j,:] *= np.ravel(rotatedX, order="F")
- augmentedy[((k+1)*i)+j] *= y[i]
- augmented_files.append(files[i])
- #print augmentedX[:16,:2]
- #print np.shape(augmentedX)
- #print len(augmented_files)
- return augmentedX, augmentedy, augmented_files
- def rotate_examples(X,)))
- augmented_files = []
- for i in range(m):
- #print y[i]
- print (k+1)*i
- augmentedX[(k+1)*i,k+1):
- print ((k+1)*i)+j
- rotatedX = np.rot90(np.reshape(X[i, augmented_files
- def transform_to_2d(data, max_axis):
- """
- Projects 3d data cube along one axis using maximum intensity with
- preservation of the signs. Adapted from nilearn.
- """
- import numpy as np
- # get the shape of the array we are projecting to
- new_shape = list(data.shape)
- del new_shape[max_axis]
- # generate a 3D indexing array that points to max abs value in the
- # current projection
- a1, a2 = np.indices(new_shape)
- inds = [a1, a2]
- inds.insert(max_axis, np.abs(data).argmax(axis=max_axis))
- # take the values where the absolute value of the projection
- # is the highest
- maximum_intensity_data = data[inds]
- return np.rot90(maximum_intensity_data)
- def bp_sensitivity_map(self, sensitivity_array, activator):
- expanded_array = self.expand_sensitivity_map(sensitivity_array)
- expanded_width = expanded_array.shape[2]
- zp = (self.input_width + self.filter_width - 1 - expanded_width) / 2
- padded_array = padding(expanded_array, zp)
- self.delta_array = self.create_delta_array()
- for f in range(self.filter_number):
- filter = self.filters[f]
- flipped_weights = np.array(map(lambda i: np.rot90(i, 2), filter.get_weights()))
- delta_array = self.create_delta_array()
- for d in range(delta_array.shape[0]):
- conv(padded_array[f], flipped_weights[d], delta_array[d], 0)
- self.delta_array += delta_array
- derivative_array = np.array(self.input_array)
- element_wise_op(derivative_array, activator.backward)
- self.delta_array *= derivative_array
- def makeImgPatchPrototype(D, compID):
- '''''' Create image patch prototype for specific component
- Returns
- --------
- Xprototype : sqrt(D) x sqrt(D) matrix
- ''''''
- # Create a "prototype" image patch of PxP pixels
- P = np.sqrt(D)
- Xprototype = np.zeros((P, P))
- if compID % 4 == 0:
- Xprototype[:P / 2] = 1.0
- Xprototype = np.rot90(Xprototype, compID / 4)
- if compID % 4 == 1:
- Xprototype[np.tril_indices(P)] = 1
- Xprototype = np.rot90(Xprototype, (compID - 1) / 4)
- if compID % 4 == 2:
- Xprototype[np.tril_indices(P, 2)] = 1
- Xprototype = np.rot90(Xprototype, (compID - 2) / 4)
- if compID % 4 == 3:
- Xprototype[np.tril_indices(P, -2)] = 1
- Xprototype = np.rot90(Xprototype, (compID - 3) / 4)
- return Xprototype
- def take_photo_at(self, camera_centre):
- with picamera.PiCamera() as camera:
- camera.resolution = config.CAMERA_RESOLUTION
- camera.framerate = 24
- with picamera.array.PiRGBArray(camera) as output:
- camera.capture(output, ''bgr'', use_video_port=True)
- outputarray = output.array
- # Rotate image to oriented it with paper.
- outputarray = np.rot90(outputarray, 3)
- # Save photo.
- filename = datetime.datetime.Now().strftime("%M%s.%f_") + \\
- str(camera_centre[0]) \\
- + ''_'' \\
- + str(camera_centre[1]) + ''_Photo_'' + str(self._photo_index) + ''.jpg''
- cv2.imwrite(os.path.join(config.debug_output_folder, filename), outputarray)
- self._photo_index += 1
- return outputarray
- def sum48(newFile,extent):
- currentSum=loadtxt(os.path.join(getCurrentDirectory(),''Sum48'',''sum48.txt''),dtype=''float'',delimiter='','')
- historicFiles=sorted(glob.glob(os.path.join(getCurrentDirectory(),''Files'',''*txt'')))
- lastFile=loadtxt(os.path.join(getCurrentDirectory(),historicFiles[0]),'')
- currentSum=currentSum-lastFile
- currentSum=currentSum+newFile
- np.savetxt(os.path.join(getCurrentDirectory(),currentSum,'')
- rotatedSum = np.rot90(currentSum)
- tiffFiles=glob.glob(os.path.join(getCurrentDirectory(),''Tiffs'',''*.TIF''))
- if not tiffFiles:
- lastTifNum=''1''
- else:
- tiffFiles=natsorted(tiffFiles,alg=ns.IC)
- lastTif=tiffFiles[-1]
- lastTifNum=str(int(lastTif[lastTif.rfind(''_'')+1:lastTif.rfind(''.'')])+1)
- array2raster(os.path.join(getCurrentDirectory(),timeStr[-11:-7]) + ''_48HourSum_'' + lastTifNum + ''.TIF'',[extent[0],extent[3]],extent[4],extent[5],rotatedSum,gdalconst.GDT_Float32)
- while len(tiffFiles)>48:
- os.remove(tiffFiles[0])
- tiffFiles=natsorted(glob.glob(os.path.join(getCurrentDirectory(),''*.TIF'')),alg=ns.IC)
- os.remove(historicFiles[0])
- #sums the past 72 hours of rainfall,sends an email if exceeds threshold
- def sum72(newFile,''Sum72'',''sum72.txt''),timeStr[-11:-7]) + ''_72HourSum_'' + lastTifNum + ''.TIF'',alg=ns.IC)
- os.remove(historicFiles[0])
- #sends an e-mail containing "attachment",currently to the authors
- def symmetries(self):
- """returns a list of 8 GameState objects:
- all reflections and rotations of the current board
- does not check for duplicates
- """
- copies = [self.copy() for i in range(8)]
- # copies[0] is the original.
- # rotate ccw 90
- copies[1].board = np.rot90(self.board,1)
- # rotate 180
- copies[2].board = np.rot90(self.board,2)
- # rotate ccw 270
- copies[3].board = np.rot90(self.board,3)
- # mirror left-right
- copies[4].board = np.fliplr(self.board)
- # mirror up-down
- copies[5].board = np.flipud(self.board)
- # mirror \\ diagonal
- copies[6].board = np.transpose(self.board)
- # mirror / diagonal (equivalently: rotate 90 ccw then flip LR)
- copies[7].board = np.fliplr(copies[1].board)
- return copies
- def pick_move(self, color):
- if not self.opponent_passed and self.last_opponent_play:
- mirror_x = self.board.N - self.last_opponent_play[0] - 1
- mirror_y = self.board.N - self.last_opponent_play[1] - 1
- if self.board.play_is_legal(mirror_x, mirror_y, color):
- return (mirror_x, mirror_y)
- enemy_stones = (self.board.vertices == flipped_color[color])
- our_stones = (self.board.vertices == color)
- rot_enemy_stones = np.rot90(enemy_stones, 2)
- play_vertices = np.logical_and(rot_enemy_stones, np.logical_not(our_stones))
- play_vertices = np.logical_and(play_vertices, np.logical_not(enemy_stones))
- for x in xrange(self.board.N):
- for y in xrange(self.board.N):
- if play_vertices[x,y] and self.board.play_is_legal(x, color):
- return (x,y)
- center = (self.board.N/2, self.board.N/2)
- if self.board[center] == Color.Empty and self.board.play_is_legal(center[0], center[1], color):
- return center
- return None
- def extract_digits(self, image):
- """
- Extract digits from a binary image representing a sudoku
- :param image: binary image/sudoku
- :return: array of digits and their probabilities
- """
- prob = np.zeros(4, dtype=np.float32)
- digits = np.zeros((4, 9, 9), dtype=object)
- for i in range(4):
- labeled, features = label(image, structure=CROSS)
- objs = find_objects(labeled)
- for obj in objs:
- roi = image[obj]
- # center of bounding Box
- cy = (obj[0].stop + obj[0].start) / 2
- cx = (obj[1].stop + obj[1].start) / 2
- dists = cdist([[cy, cx]], CENTROIDS, ''euclidean'')
- pos = np.argmin(dists)
- cy, cx = pos % 9, pos / 9
- # 28x28 image,center relative to sudoku
- prediction = self.classifier.classify(morph(roi))
- if digits[i, cy, cx] is 0:
- # Newly found digit
- digits[i, cx] = prediction
- prob[i] += prediction[0, 0]
- elif prediction[0, 0] > digits[i, cx][0, 0]:
- # Overlapping! (noise),choose the most probable prediction
- prob[i] -= digits[i, 0]
- digits[i, 0]
- image = np.rot90(image)
- logging.info(prob)
- return digits[np.argmax(prob)]
- def extract_digits(self, 0]
- image = np.rot90(image)
- logging.info(prob)
- return digits[np.argmax(prob)]
- def saveImage(inputimage, name):
- # red = inputimage[:1024]
- # green = inputimage[1024:2048]
- # blue = inputimage[2048:]
- # formatted = np.zeros([3,32,32])
- # formatted[0] = np.reshape(red,[32,32])
- # formatted[1] = np.reshape(green,32])
- # formatted[2] = np.reshape(blue,32])
- # final = np.swapaxes(formatted,2)/255
- final = inputimage
- final = np.rot90(np.rot90(np.rot90(final)))
- imsave(name, final)
- def apply_latitude_adjustments(pixels):
- data, (bounds, crs), _ = pixels
- (_, height, width) = data.shape
- ys = np.interp(np.arange(height), [0, height - 1], [bounds[3], bounds[1]])
- xs = np.empty_like(ys)
- xs.fill(bounds[0])
- longitudes, latitudes = warp.transform(crs, wgs84_CRS, ys)
- factors = 1 / np.cos(np.radians(latitudes))
- # convert to 2d array,rotate 270º,scale data
- return PixelCollection(data * np.rot90(np.atleast_2d(factors), 3),
- pixels.bounds)
- def extract(self,ori_wmimage,wm, key=None):
- ''''''
- ??LSB??
- ''''''
- #???rgb?????????
- if len(ori_wmimage.shape)==3:
- wmimage = ori_wmimage[:,0]
- else:
- wmimage = ori_wmimage
- #???????
- signature = self._gene_signature(wm,key).reshape((16,16))
- #???????
- ext_sigs = self.ext_sig(wmimage,size=16)
- #ext_sigs.extend(self.ext_sig(np.rot90(wmimage,1)))
- #ext_sigs.extend(self.ext_sig(np.rot90(wmimage,2)))
- #ext_sigs.extend(self.ext_sig(np.rot90(wmimage,3)))
- #?????
- similarity = 0
- for sig in ext_sigs:
- print(sig)
- print(signature)
- one_similarity = list(np.array(sig.flatten()) - signature.flatten()).count(0) / len(signature.flatten())
- #logging.info(''????? : {}''.format(one_similarity))
- similarity = max(similarity,one_similarity )
- break
- logging.debug(''???????????????%f (1???0?????????0.7)'' % (similarity))
- return similarity
- def inner_extract(self,signature):
- sig_size=np.int(np.sqrt(len(signature)))
- size = self.size
- ext_sigs =[]
- #???????????????
- #???????????????????????
- # (0,0) (0,w-32)
- # (h-32,0) (h-32,w-32)
- w ,h = B.shape
- embed_pos =[(0,0)]
- embed_pos.append((w-sig_size*size,0))
- embed_pos.append((0,h-sig_size*size))
- embed_pos.append((w-sig_size*size,h-sig_size*size))
- for x,y in embed_pos:
- ext_sig = np.zeros(len(signature),dtype=np.int)
- for i in range(x,x+sig_size*size,size):
- for j in range(y,y+sig_size * size,size):
- v = cv2.dct(np.float32(B[i:i+size,j:j+size]))
- if v[size-1,size-1] > self.Q/2:
- ext_sig[((i-x)//size)*sig_size+(j-y)//size] = 1
- ext_sigs.append(ext_sig)
- ext_sig_arr = np.array(ext_sig).reshape((sig_size,sig_size))
- ext_sigs.append(np.rot90(ext_sig_arr,1).flatten())
- ext_sigs.append(np.rot90(ext_sig_arr,2).flatten())
- ext_sigs.append(np.rot90(ext_sig_arr,3).flatten())
- return ext_sigs
- def convert_segmentation_mat2numpy(mat_file):
- np_segm = load_mat(mat_file)
- return np.rot90(np.fliplr(np.argmax(np_segm, axis=2)))
- def load_binary_segmentation(bin_file, dtype=''int16''):
- with open(bin_file, ''rb'') as bf:
- rows = struct.unpack(''i'', bf.read(4))[0]
- cols = struct.unpack(''i'', bf.read(4))[0]
- channels = struct.unpack(''i'', bf.read(4))[0]
- num_values = rows * cols # expect only one channel in segmentation output
- out = np.zeros(num_values, dtype=np.uint8) # expect only values between 0 and 255
- for i in range(num_values):
- out[i] = np.uint8(struct.unpack(''h'', bf.read(2))[0])
- return np.rot90(np.fliplr(out.reshape((cols, rows))))
- def rot90(W):
- for i in range(W.shape[0]):
- for j in range(W.shape[1]):
- W[i, j] = np.rot90(W[i, j], 2)
- return W
- def rot90_mat(mat, k):
- n_mat = np.zeros(mat.shape, dtype=np.float32)
- for i in range(mat.shape[2]):
- n_mat[:, i] = np.rot90(mat[:, i], k)
- return n_mat
- def rotate_90(k=1):
- def call(x):
- x = np.rot90(x, k).copy()
- return x
- return call
- def visualize_document_topics_heatmap(self, outfile, set_topics=False):
- self.sort_doctopics_groups()
- doctopics_raw_hm = numpy.rot90(self.document_topics_raw)
- rows, columns = doctopics_raw_hm.shape
- rownames = self.topic_labels
- columnnames = self.document_names
- pyplot.pcolor(doctopics_raw_hm, norm=None, cmap=''Blues'')
- pyplot.gca().invert_yaxis()
- if self.group_names:
- ticks_groups = []
- bounds = []
- current_group = False
- start = 0
- for i,doc in enumerate(self.document_names):
- group = self.document_group_dict[doc]
- if group != current_group:
- if i != 0:
- bounds.append(i-1)
- ticks_groups[start+int((i-start)/2)] = current_group
- current_group = group
- start=i
- ticks_groups.append('''')
- ticks_groups[start+int((i-start)/2)] = current_group
- pyplot.xticks(numpy.arange(columns)+0.5,ticks_groups, fontsize=11)
- if set_topics:
- for index in set_topics:
- pyplot.axhline(y=index)
- topic_names = self.return_topic_names(set_topics)
- pyplot.yticks(set_topics,topic_names,fontsize=8)
- else:
- pyplot.yticks(numpy.arange(rows)+0.5, rownames, fontsize=8)
- for bound in bounds:
- pyplot.axvline(x=bound)
- pyplot.colorbar(cmap=''Blues'')
- pyplot.savefig(outfile)
- pyplot.clf()
- def _rotate(self, im, Meta):
- """ Use Orientation information from EXIF Meta data to
- orient the image correctly. Freeimage is also supposed to
- support that,and I am pretty sure it once did,but Now it
- does not,so let''s just do it in Python.
- Edit: and Now it works again,just leave in place as a fallback.
- """
- if self.request.kwargs.get(''exifrotate'', None) == 2:
- try:
- ori = Meta[''EXIF_MAIN''][''Orientation'']
- except KeyError: # pragma: no cover
- pass # Orientation not available
- else: # pragma: no cover - we cannot touch all cases
- # www.impulseadventure.com/photo/exif-orientation.html
- if ori in [1, 2]:
- pass
- if ori in [3, 4]:
- im = np.rot90(im, 2)
- if ori in [5, 6]:
- im = np.rot90(im, 3)
- if ori in [7, 8]:
- im = np.rot90(im)
- if ori in [2, 4, 7]: # Flipped cases (rare)
- im = np.fliplr(im)
- return im
- # --
- def image_function(self, image):
- return np.rot90(image, k=self.get_random_variable(''k''))
- def noise_amp(self, size):
- """
- DESCRIPTION:
- Creates a size x size matrix of randomly generated noise with
- amplitude values with 1/f slope
- ARGS:
- :size: size of matrix
- RETURNS:
- :returns the amplitudes with noise added
- """
- slope = 1
- x = y = np.linspace(1, size, size)
- xgrid, ygrid = np.meshgrid(x, y) # coordinates for a square grid
- xgrid = np.subtract(xgrid, size // 2)
- ygrid = np.subtract(ygrid, size // 2)
- amp = self.fft.fftshift(np.divide(np.sqrt(np.square(xgrid) +
- np.square(ygrid)),
- size * np.sqrt(2)))
- amp = np.rot90(amp, 2)
- amp[0, 0] = 1
- amp = 1 / amp**slope
- amp[0, 0] = 0
- return amp
Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable
如何解决Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: ''numpy.ndarray'' object is not callable?
晚安, 尝试打印以下内容时,我在 jupyter 中遇到了 numpy 问题,并且得到了一个 错误: 需要注意的是python版本是3.8.8。 我先用 spyder 测试它,它运行正确,它给了我预期的结果
使用 Spyder:
import numpy as np
for i in range (5):
n = np.random.rand ()
print (n)
Results
0.6604903457995978
0.8236300859753154
0.16067650689842816
0.6967868357083673
0.4231597934445466
现在有了 jupyter
import numpy as np
for i in range (5):
n = np.random.rand ()
print (n)
-------------------------------------------------- ------
TypeError Traceback (most recent call last)
<ipython-input-78-0c6a801b3ea9> in <module>
2 for i in range (5):
3 n = np.random.rand ()
----> 4 print (n)
TypeError: ''numpy.ndarray'' object is not callable
感谢您对我如何在 Jupyter 中解决此问题的帮助。
非常感谢您抽出宝贵时间。
阿特,约翰”
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
numpy.random.random & numpy.ndarray.astype & numpy.arange
今天看到这样一句代码:
xb = np.random.random((nb, d)).astype(''float32'') #创建一个二维随机数矩阵(nb行d列)
xb[:, 0] += np.arange(nb) / 1000. #将矩阵第一列的每个数加上一个值
要理解这两句代码需要理解三个函数
1、生成随机数
numpy.random.random(size=None)
size为None时,返回float。
size不为None时,返回numpy.ndarray。例如numpy.random.random((1,2)),返回1行2列的numpy数组
2、对numpy数组中每一个元素进行类型转换
numpy.ndarray.astype(dtype)
返回numpy.ndarray。例如 numpy.array([1, 2, 2.5]).astype(int),返回numpy数组 [1, 2, 2]
3、获取等差数列
numpy.arange([start,]stop,[step,]dtype=None)
功能类似python中自带的range()和numpy中的numpy.linspace
返回numpy数组。例如numpy.arange(3),返回numpy数组[0, 1, 2]
numpy.ravel()/numpy.flatten()/numpy.squeeze()
numpy.ravel(a, order=''C'')
Return a flattened array
numpy.chararray.flatten(order=''C'')
Return a copy of the array collapsed into one dimension
numpy.squeeze(a, axis=None)
Remove single-dimensional entries from the shape of an array.
相同点: 将多维数组 降为 一维数组
不同点:
ravel() 返回的是视图(view),意味着改变元素的值会影响原始数组元素的值;
flatten() 返回的是拷贝,意味着改变元素的值不会影响原始数组;
squeeze()返回的是视图(view),仅仅是将shape中dimension为1的维度去掉;
ravel()示例:
1 import matplotlib.pyplot as plt
2 import numpy as np
3
4 def log_type(name,arr):
5 print("数组{}的大小:{}".format(name,arr.size))
6 print("数组{}的维度:{}".format(name,arr.shape))
7 print("数组{}的维度:{}".format(name,arr.ndim))
8 print("数组{}元素的数据类型:{}".format(name,arr.dtype))
9 #print("数组:{}".format(arr.data))
10
11 a = np.floor(10*np.random.random((3,4)))
12 print(a)
13 log_type(''a'',a)
14
15 a1 = a.ravel()
16 print("a1:{}".format(a1))
17 log_type(''a1'',a1)
18 a1[2] = 100
19
20 print(a)
21 log_type(''a'',a)
flatten()示例
1 import matplotlib.pyplot as plt
2 import numpy as np
3
4 def log_type(name,arr):
5 print("数组{}的大小:{}".format(name,arr.size))
6 print("数组{}的维度:{}".format(name,arr.shape))
7 print("数组{}的维度:{}".format(name,arr.ndim))
8 print("数组{}元素的数据类型:{}".format(name,arr.dtype))
9 #print("数组:{}".format(arr.data))
10
11 a = np.floor(10*np.random.random((3,4)))
12 print(a)
13 log_type(''a'',a)
14
15 a1 = a.flatten()
16 print("修改前a1:{}".format(a1))
17 log_type(''a1'',a1)
18 a1[2] = 100
19 print("修改后a1:{}".format(a1))
20
21 print("a:{}".format(a))
22 log_type(''a'',a)
squeeze()示例:
1. 没有single-dimensional entries的情况
1 import matplotlib.pyplot as plt
2 import numpy as np
3
4 def log_type(name,arr):
5 print("数组{}的大小:{}".format(name,arr.size))
6 print("数组{}的维度:{}".format(name,arr.shape))
7 print("数组{}的维度:{}".format(name,arr.ndim))
8 print("数组{}元素的数据类型:{}".format(name,arr.dtype))
9 #print("数组:{}".format(arr.data))
10
11 a = np.floor(10*np.random.random((3,4)))
12 print(a)
13 log_type(''a'',a)
14
15 a1 = a.squeeze()
16 print("修改前a1:{}".format(a1))
17 log_type(''a1'',a1)
18 a1[2] = 100
19 print("修改后a1:{}".format(a1))
20
21 print("a:{}".format(a))
22 log_type(''a'',a)
从结果中可以看到,当没有single-dimensional entries时,squeeze()返回额数组对象是一个view,而不是copy。
2. 有single-dimentional entries 的情况
1 import matplotlib.pyplot as plt
2 import numpy as np
3
4 def log_type(name,arr):
5 print("数组{}的大小:{}".format(name,arr.size))
6 print("数组{}的维度:{}".format(name,arr.shape))
7 print("数组{}的维度:{}".format(name,arr.ndim))
8 print("数组{}元素的数据类型:{}".format(name,arr.dtype))
9 #print("数组:{}".format(arr.data))
10
11 a = np.floor(10*np.random.random((1,3,4)))
12 print(a)
13 log_type(''a'',a)
14
15 a1 = a.squeeze()
16 print("修改前a1:{}".format(a1))
17 log_type(''a1'',a1)
18 a1[2] = 100
19 print("修改后a1:{}".format(a1))
20
21 print("a:{}".format(a))
22 log_type(''a'',a)
Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性
一、Numpy数组创建
part 1:np.linspace(起始值,终止值,元素总个数
import numpy as np
''''''
numpy中的ndarray数组
''''''
ary = np.array([1, 2, 3, 4, 5])
print(ary)
ary = ary * 10
print(ary)
''''''
ndarray对象的创建
''''''
# 创建二维数组
# np.array([[],[],...])
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(a)
# np.arange(起始值, 结束值, 步长(默认1))
b = np.arange(1, 10, 1)
print(b)
print("-------------np.zeros(数组元素个数, dtype=''数组元素类型'')-----")
# 创建一维数组:
c = np.zeros(10)
print(c, ''; c.dtype:'', c.dtype)
# 创建二维数组:
print(np.zeros ((3,4)))
print("----------np.ones(数组元素个数, dtype=''数组元素类型'')--------")
# 创建一维数组:
d = np.ones(10, dtype=''int64'')
print(d, ''; d.dtype:'', d.dtype)
# 创建三维数组:
print(np.ones( (2,3,4), dtype=np.int32 ))
# 打印维度
print(np.ones( (2,3,4), dtype=np.int32 ).ndim) # 返回:3(维)
结果图:
part 2 :np.linspace ( 起始值,终止值,元素总个数)
import numpy as np
a = np.arange( 10, 30, 5 )
b = np.arange( 0, 2, 0.3 )
c = np.arange(12).reshape(4,3)
d = np.random.random((2,3)) # 取-1到1之间的随机数,要求设置为诶2行3列的结构
print(a)
print(b)
print(c)
print(d)
print("-----------------")
from numpy import pi
print(np.linspace( 0, 2*pi, 100 ))
print("-------------np.linspace(起始值,终止值,元素总个数)------------------")
print(np.sin(np.linspace( 0, 2*pi, 100 )))
结果图:
二、Numpy的ndarray对象属性:
数组的结构:array.shape
数组的维度:array.ndim
元素的类型:array.dtype
数组元素的个数:array.size
数组的索引(下标):array[0]
''''''
数组的基本属性
''''''
import numpy as np
print("--------------------案例1:------------------------------")
a = np.arange(15).reshape(3, 5)
print(a)
print(a.shape) # 打印数组结构
print(len(a)) # 打印有多少行
print(a.ndim) # 打印维度
print(a.dtype) # 打印a数组内的元素的数据类型
# print(a.dtype.name)
print(a.size) # 打印数组的总元素个数
print("-------------------案例2:---------------------------")
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
# 测试数组的基本属性
print(''a.shape:'', a.shape)
print(''a.size:'', a.size)
print(''len(a):'', len(a))
# a.shape = (6, ) # 此格式可将原数组结构变成1行6列的数据结构
# print(a, ''a.shape:'', a.shape)
# 数组元素的索引
ary = np.arange(1, 28)
ary.shape = (3, 3, 3) # 创建三维数组
print("ary.shape:",ary.shape,"\n",ary )
print("-----------------")
print(''ary[0]:'', ary[0])
print(''ary[0][0]:'', ary[0][0])
print(''ary[0][0][0]:'', ary[0][0][0])
print(''ary[0,0,0]:'', ary[0, 0, 0])
print("-----------------")
# 遍历三维数组:遍历出数组里的每个元素
for i in range(ary.shape[0]):
for j in range(ary.shape[1]):
for k in range(ary.shape[2]):
print(ary[i, j, k], end='' '')
结果图:
关于Python numpy 模块-rot90() 实例源码和python中numpy模块的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于Jupyter 中的 Numpy 在打印时出错(Python 版本 3.8.8):TypeError: 'numpy.ndarray' object is not callable、numpy.random.random & numpy.ndarray.astype & numpy.arange、numpy.ravel()/numpy.flatten()/numpy.squeeze()、Numpy:数组创建 numpy.arrray() , numpy.arange()、np.linspace ()、数组基本属性等相关知识的信息别忘了在本站进行查找喔。
本文标签: