>>> import numpy a
>>> list = [
... [1,2,3],
... [4,5,6],
... [7,8,9]
... ]
>>> arr = np.array
>>> arr
array([[1, 2, 3],
       [4, 5, 6],
       [7, 8, 9]])
>>> 

>>> a = arr[0:2, 0
>>> a
array([[1, 2],
       [4, 5]])
>>> 
>>> b=arr[1:, 1:]
>>> b
array([[5, 6],
       [8, 9]])

numpy 정수 인덱싱 (integer indexing)

# 정수 인덱싱 : numpy 배열 a에 대해서 a[[row1, row2] , [col1, col2]] = a[row1,col1]과 a[row2,col2] 배열 요소 집합

>>> list = [
... [1,2,3,4],
... [5,6,7,8],
... [9,10,11,12]
... ]
>>> a = np.array(list)
>>> 
>>> #정수 인덱싱
>>> res = a[[0,2], [1,3]]
>>> res
array([ 2, 12])

 

# 부울린 인덱싱 

>>> list1 = [
... [1,2,3],
... [4,5,6],
... [7,8,9]
... ]
>>> 
>>> aa = np.array(list1)
>>> b_arr = np.array([
... [False, True, False],
... [True, False, True],
... [False, True, False]
... ])
>>> 
>>> b_arr
array([[False,  True, False],
       [ True, False,  True],
       [False,  True, False]])
>>> n = aa[b_arr]
>>> 
>>> n
array([2, 4, 6, 8])

 

# 부울린 

'머신러닝 > 파이썬 라이브러리 Numpy, Pandas' 카테고리의 다른 글

python 버전 정리  (0) 2021.12.28
Numpy 1  (0) 2020.08.04
환경구성  (0) 2020.08.03

넘파이는 과학 계산을 위한 라이브러리로 다차원 배열을 처리하는데 필요한 여러 기능을 제공한다.

 

-numpy 설치

pip3 install numpy

 

- numpy 배열

배열은 동일한 타입의 값들을 갖는다.

배열의 차원을 rank라고 한다.

 

shape :  각차원의 크기를 튜플로 표시한 것

예> 2행, 3열 인 2차원 배열은 rank는 2이고, shape(2,3)

 

- numpy 배열을 생성하는 방법

> 파이썬의 리스트를 사용하는 방법

list1 = [1,2,3,4]
a = np.array(list1)
b = np.array([[1,2,3],[4,5,6]])

> numpy에서 제공하는 함수를 사용하는 방법

>>> aa = np.zeros((2,2))
>>> aa
array([[0., 0.],
       [0., 0.]])
>>> type(aa)
<class 'numpy.ndarray'>

>>> ones = np.ones((2,3))
>>> ones
array([[1., 1., 1.],
       [1., 1., 1.]])
       
>>> full = np.full((2,3),10)
>>> full
array([[10, 10, 10],
       [10, 10, 10]])
       
>>> eye = np.eye(5)
>>> eye
array([[1., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 1.]])
       

>>> abc = np.array(range(20))
>>> abc
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])

>>> ab = np.array(range(20)).reshape((4,5))
>>> ab
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])

- numpy 슬라이싱, 인덱싱, 연산

 

 

'머신러닝 > 파이썬 라이브러리 Numpy, Pandas' 카테고리의 다른 글

python 버전 정리  (0) 2021.12.28
Numpy 2  (0) 2020.08.26
환경구성  (0) 2020.08.03

1. Anaconda 설치 : Anaconda3-2020.07-MacOSX-x86_64.pkg 설치.

(python은 3.8.3 버전으로 설치 됨)

MAC-KHLYUM:~ khlyum$ python
Python 3.8.3 (default, Jul  2 2020, 11:26:31) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.

 

'머신러닝 > 파이썬 라이브러리 Numpy, Pandas' 카테고리의 다른 글

python 버전 정리  (0) 2021.12.28
Numpy 2  (0) 2020.08.26
Numpy 1  (0) 2020.08.04
package com.backjun.algorithm;

import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int hambergLowPrice=0;
		int beverageLowPrice=0;

		for (int i=0; i<5; i++){
			int price = Integer.parseInt(br.readLine());
			if(i==0){
				hambergLowPrice = price;
			} else if (i<3) {
				hambergLowPrice = price < hambergLowPrice ? price : hambergLowPrice;
			} else if (i==3) {
				beverageLowPrice = price;
			} else if (i<5) {
				beverageLowPrice = price < beverageLowPrice ? price : beverageLowPrice;
			}

		}
		System.out.println(hambergLowPrice + beverageLowPrice - 50);
	}
}

Math.min을 사용하면 아래처럼.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	static int min(int a, int b){
		return a<b ? a:b;
	}
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.print(min(Integer.parseInt(br.readLine()), min(Integer.parseInt(br.readLine()),
				Integer.parseInt(br.readLine()))) + min(Integer.parseInt(br.readLine()),
				Integer.parseInt(br.readLine())) - 50);
	}
}

min을 함수로 정의해서 사용.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
	static int min(int a, int b){
		return a<b ? a:b;
	}
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.print(min(Integer.parseInt(br.readLine()), min(Integer.parseInt(br.readLine()),
				Integer.parseInt(br.readLine()))) + min(Integer.parseInt(br.readLine()),
				Integer.parseInt(br.readLine())) - 50);
	}
}

'알고리즘 > acmicpc.net' 카테고리의 다른 글

#10039  (0) 2020.07.13
1110  (0) 2020.07.10
10951&10952  (0) 2020.07.10
10871  (0) 2020.07.08
2439  (0) 2020.07.08
package com.backjun.algorithm;

import java.io.*;
import java.util.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		ArrayList<Integer> arr = new ArrayList();
		String line;
		int cnt =0;
		while (true){
			line = br.readLine();
			int score = Integer.parseInt(line);
			if(score >= 40){
				arr.add(score);
			} else {
				arr.add(40);
			}
			cnt++;
			if(cnt==5){
				break;
			}
		}
		int result = (int)arr.stream().mapToInt(Integer::intValue).average().getAsDouble();
		System.out.println(result);

//		bw.flush();
//		bw.close();

	}
}

'알고리즘 > acmicpc.net' 카테고리의 다른 글

#5543  (0) 2020.07.13
1110  (0) 2020.07.10
10951&10952  (0) 2020.07.10
10871  (0) 2020.07.08
2439  (0) 2020.07.08
import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int input = Integer.parseInt(br.readLine());
		int a = input/10;
		int b = input%10;
		int cnt = 0;
		int result;
		while (true){
			cnt++;
			result = (b*10) + (a+b)%10;
			a = result/10;
			b = result%10;
			//System.out.println("Step" + cnt + ": " + result + " a: " + a + ", b: " + b);
			if(result == input){
				break;
			}
		}

		System.out.println(cnt);
	}
}

다른 사람꺼 퍼옴

import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.close();
         
        int number = n;
        int count = 0;
         
        do {
            number = number % 10 * 10 + (number / 10 + number % 10) % 10;
            count++;
        } while (n != number);
         
        System.out.println(count);
    }
}

'알고리즘 > acmicpc.net' 카테고리의 다른 글

#5543  (0) 2020.07.13
#10039  (0) 2020.07.13
10951&10952  (0) 2020.07.10
10871  (0) 2020.07.08
2439  (0) 2020.07.08

+ Recent posts