array

Python Numpy 简单入门

喜你入骨 提交于 2020-01-03 02:35:53
Python Numpy Reference Python review(list, dictionary, tuple) List xs = [3, 1, 2] # Create a list print(xs, xs[2]) # Prints "[3, 1, 2] 2" print(xs[-1]) # Negative indices count from the end of the list; prints "2" xs[2] = 'foo' # Lists can contain elements of different types print(xs) # Prints "[3, 1, 'foo']" xs.append('bar') # Add a new element to the end of the list print(xs) # Prints "[3, 1, 'foo', 'bar']" x = xs.pop() # Remove and return the last element of the list print(x, xs) # Prints "bar [3, 1, 'foo']" Slicing nums = list(range(5)) # range is a built-in function that creates a list of

numpy模块学习笔记

被刻印的时光 ゝ 提交于 2020-01-03 02:35:36
# encoding=utf-8 import numpy as np from numpy.linalg import * def main(): # 1、最基本的array lst = [[1, 3, 5], [2, 4, 6]] print(type(lst)) # <class 'list'> np_lst = np.array(lst) print(type(np_lst)) # <class 'numpy.ndarray'> # 指定数据类型 bool int int8/16/32/64/128 uint8/16/32/64/128 float/16/32/64 complex64/128 np_lst1 = np.array(lst, dtype=np.float) # 相关属性 print(np_lst1.shape) # 形状 print(np_lst1.ndim) # 维度 print(np_lst1.dtype) # 类型 print(np_lst1.itemsize) # 每个元素的大小(字节) print(np_lst1.size) # 大小(元素个数) # 2、常用的数组 print(np.zeros([2, 4])) # 零 print(np.ones([3, 5])) # 壹 print("Rand:") print(np.random.rand()

Numpy数组对象的操作-索引机制、切片和迭代方法

做~自己de王妃 提交于 2020-01-03 02:33:52
前几篇博文我写了数组创建和数据运算,现在我们就来看一下数组对象的操作方法。使用索引和切片的方法选择元素,还有如何数组的迭代方法。 一、索引机制 1.一维数组 In [1]: a = np.arange(10,16) In [2]: a Out[2]: array([10, 11, 12, 13, 14, 15]) #使用正数作为索引 In [3]: a[3] Out[3]: 13 #还可以使用负数作为索引 In [4]: a[-4] Out[4]: 12 #方括号中传入多数索引值,可同时选择多个元素In [6]: a[[0,3,4]]Out[6]: array([10, 13, 14]) 2.二维数组 二维数组也被称为矩阵,是由行和列组成的。axes为2,用0轴表示行,用1表示列。[行索引,列索引] In [14]: A Out[14]: array([[10, 11, 12], [13, 14, 15], [16, 17, 18]])#取出第三行第二列的元素 In [15]: A[2,1] Out[15]: 17#可以使用方括号取出多个元素 In [17]: A[[[2,1],[1,2]]] Out[17]: array([17, 15]) 二、切片操作:抽取部分数组元素生成新数组 1.一维数组切片操作 In [26]: a = np.arange(10,20) In [27]:

numpy.concatenate

不羁岁月 提交于 2020-01-03 02:27:23
import numpy as np a = np.array([[1, 2], [3, 4]]) a.shape Out[3]: (2, 2) b = np.array([[5, 6]]) b.shape Out[5]: (1, 2) np.concatenate((a, b)) Out[6]: array([[1, 2], [3, 4], [5, 6]]) c= np.concatenate((a, b)) c.shape Out[8]: (3, 2) d = np.concatenate((a, b), axis=0) d.shape Out[10]: (3, 2) e = np.concatenate((a, b), axis=1) Traceback (most recent call last): File "<ipython-input-11-05a280a2cb02>", line 1, in <module> e = np.concatenate((a, b), axis=1) ValueError: all the input array dimensions except for the concatenation axis must match exactly e = np.concatenate((a, b.T), axis=1) e.shape Out

numpy模块介绍

回眸只為那壹抹淺笑 提交于 2020-01-03 02:25:26
import numpy as np np.array([1,2,3]) array([1, 2, 3]) np.array([[1,2,3],[4,5,6]]) array([[1, 2, 3], [4, 5, 6]]) arr = np.array([[1,2,3],[4,5,6],[7,8,9]]) arr array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) 把它给看成一个矩阵,或者看成一个ndarray数组的话,我们去获取他的形状. arr = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) arr.shape (4, 3) arr.shape[0] print(arr.shape[0]) 20 arr.shape[1] 3 切割矩阵 arr = np.array([1,2,3]) arr arr[:] array([1, 2, 3]) arr = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) arr array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]]) arr[:,:] array([[ 1, 2, 3], [ 4, 5, 6], [ 7, 8, 9], [10, 11, 12]])

K Smallest In Unsorted Array

给你一囗甜甜゛ 提交于 2020-01-02 04:12:03
Find the K smallest numbers in an unsorted integer array A. The returned numbers should be in ascending order. Assumptions A is not null K is >= 0 and smaller than or equal to size of A Return an array with size K containing the K smallest numbers in ascending order Examples A = {3, 4, 1, 2, 5}, K = 3, the 3 smallest numbers are {1, 2, 3} public class Solution { // use a priority queue(max heap with size of k) to figure it out. // we traverse the array one time, whenever we met a element, we check it if smaller then the top element of the heap // if it is, we poll and insert that new element

RDD&Dataset&DataFrame

霸气de小男生 提交于 2020-01-01 15:01:20
Dataset创建 object DatasetCreation { def main(args: Array[String]): Unit = { val spark = SparkSession .builder() .appName("SparkSessionTest") .getOrCreate() import spark.implicits._ //1: range val ds1 = spark.range(0, 10, 2, 2) ds1.show() val dogs = Seq(Dog("jitty", "red"), Dog("mytty", "yellow")) val cats = Seq(new Cat("jitty", 2), new Cat("mytty", 4)) //2: 从Seq[T]中创建 val data = dogs val ds = spark.createDataset(data) ds.show() //3: 从RDD[T]中创建 val dogRDD = spark.sparkContext.parallelize(dogs) val dogDS = spark.createDataset(dogRDD) dogDS.show() val catRDD = spark.sparkContext.parallelize(cats)

泛型类

☆樱花仙子☆ 提交于 2020-01-01 12:18:32
JDK 1.5前,ArrayList的底层实现 class A{ } class B{ } class MyArrayList{ int size; int capacity; Object [] array; public MyArrayList(int capacity){ array = new Object[capacity]; size = 0; this.capacity = capacity; } void add(Object e) { // 检测容量 array[size++] = e; } int size() { return size; } Object get(int index) { // 检测index不能越界 return array[index]; } boolean isEmpty() { return 0 == size; } } 将对象添加到Object类型的数组中去,因为所有类的基类都是Object,所以将元素类型定义成 Object 类型,这样Object 类型的引用可以指向其他类型的对象了。 但是这样是不安全的: public class Test{ public static void main(String[] args) { MyArrayList list1=new MyArrayList(10); list1.add(new

剑指Offer_06_旋转数组的最小数字

我是研究僧i 提交于 2020-01-01 03:03:11
题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。 解题思路 直接查找 遍历数组,找到第一个非递增的数。 折半查找 首先判断首(a)尾(b)两个数的大小,如果a小于b,说明没有a即为最小值,因为数组是递增的,如果旋转之后,a的值应该大于等于b的值 如果a的值大于等于b的值,找到中间的值c,如果中间值大于a,则将范围缩小到c到b之间,若小于b,则范围缩小至a到c之间,然后继续查找。最后两个指针分别指向最大值和最小值。 如果a==b,且c == a == b,那么此时无法判断。顾需要从头扫描。 实现 直接查找 public class Solution { public int minNumberInRotateArray(int [] array) { if(array == null || array.length <= 0) return 0; int min = array[0]; for (int i = 0; i < array.length - 1; i++){ if (array[i] > array[i+1]){ min = array

python用numpy.array构建矩阵

ぐ巨炮叔叔 提交于 2020-01-01 02:51:06
( 1 1 2 4 2 1 3 3 4 4 ) (\begin{matrix}1&1&2&4&2\\1&3&3&4&4\end{matrix}) ( 1 1 ​ 1 3 ​ 2 3 ​ 4 4 ​ 2 4 ​ ) import numpy as np x = np . array ( [ [ 1 , 1 , 2 , 4 , 2 ] , [ 1 , 3 , 3 , 4 , 4 ] ] ) print ( x . shape ) # 结果是 ( 2 , 5 ) np.array.shape 返回的是一个元组 如果矩阵式2维的 则返回的 (行数,列数) 来源: CSDN 作者: @Sadam 链接: https://blog.csdn.net/weixin_43066097/article/details/103743910