Hướng dẫn scale a list python - mở rộng một danh sách python

Nếu bạn muốn kết quả cuối cùng của mình là numpy.array, thì sẽ nhanh hơn để chuyển đổi danh sách của bạn thành mảng numpy trước tay và sử dụng trực tiếp bộ phận mảng, hơn là hiểu danh sách. Thí dụ -

Nội phân chính

  • công thức bình thường hóa
  • Bình thường hóa danh sách các số bằng hàm MinMaxScaler trong Python Sklearn
  • Bình thường hóa một danh sách các số theo cách thủ công trong Python
  • Bài viết liên quan - Danh sách Python
  • Làm thế nào để bạn mở rộng quy mô trong Python?
  • Scale () làm gì trong Python?
  • Làm thế nào để bạn tìm thấy quy mô của một danh sách trong Python?
  • Làm thế nào để bạn bình thường hóa một danh sách?

import numpy as np
probsnp = np.array([proba[0] for proba in self.classifier.predict_proba(x_test)])
maximum = probs.max()
list_values = probs/maximum

Ví dụ về các bài kiểm tra thời gian -

In [46]: import numpy.random as ndr

In [47]: probs = ndr.random_sample(1000)

In [48]: probs.shape
Out[48]: (1000,)

In [49]: def func1(probs):
   ....:     maximum = max(probs)
   ....:     probsnew = [i/maximum for i in probs]
   ....:     return probsnew
   ....:

In [50]: def func2(probs):
   ....:     maximum = probs.max()
   ....:     probsnew = probs/maximum
   ....:     return probsnew
   ....:

In [51]: %timeit func1(probs)
The slowest run took 229.79 times longer than the fastest. This could mean that an intermediate result is being cached
1000 loops, best of 3: 279 µs per loop

In [52]: %timeit func1(probs)
1000 loops, best of 3: 278 µs per loop

In [53]: %timeit func2(probs)
The slowest run took 356.45 times longer than the fastest. This could mean that an intermediate result is being cached
10000 loops, best of 3: 81 µs per loop

In [54]: %timeit func1(probs)
1000 loops, best of 3: 278 µs per loop

In [55]: %timeit func2(probs)
10000 loops, best of 3: 81.5 µs per loop

Phương pháp Numpy chỉ mất thời gian 1/3 như là sự hiểu biết danh sách.


Kiểm tra thời gian với chuyển đổi

In [46]: import numpy.random as ndr

In [47]: probs = ndr.random_sample(1000)

In [48]: probs.shape
Out[48]: (1000,)

In [49]: def func1(probs):
   ....:     maximum = max(probs)
   ....:     probsnew = [i/maximum for i in probs]
   ....:     return probsnew
   ....:

In [50]: def func2(probs):
   ....:     maximum = probs.max()
   ....:     probsnew = probs/maximum
   ....:     return probsnew
   ....:

In [51]: %timeit func1(probs)
The slowest run took 229.79 times longer than the fastest. This could mean that an intermediate result is being cached
1000 loops, best of 3: 279 µs per loop

In [52]: %timeit func1(probs)
1000 loops, best of 3: 278 µs per loop

In [53]: %timeit func2(probs)
The slowest run took 356.45 times longer than the fastest. This could mean that an intermediate result is being cached
10000 loops, best of 3: 81 µs per loop

In [54]: %timeit func1(probs)
1000 loops, best of 3: 278 µs per loop

In [55]: %timeit func2(probs)
10000 loops, best of 3: 81.5 µs per loop
0 như một phần của func2 (ví dụ trên) -

In [60]: probslist = [p for p in probs]

In [61]: def func2(probs):
   ....:     probsnp = np,array(probs)
   ....:     maxprobs = probsnp.max()
   ....:     probsnew = probsnp/maxprobs
   ....:     return probsnew
   ....:

In [65]: %timeit func1(probslist)
1000 loops, best of 3: 212 µs per loop

In [66]: %timeit func2(probslist)
10000 loops, best of 3: 198 µs per loop

In [67]: probs = ndr.random_sample(60000)

In [68]: probslist = [p for p in probs]

In [74]: %timeit func1(probslist)
100 loops, best of 3: 11.5 ms per loop

In [75]: %timeit func2(probslist)
100 loops, best of 3: 5.79 ms per loop

In [76]: %timeit func1(probslist)
100 loops, best of 3: 11.4 ms per loop

In [77]: %timeit func2(probslist)
100 loops, best of 3: 5.81 ms per loop

Có vẻ như nó vẫn còn nhanh hơn một chút để sử dụng mảng numpy.

  1. Làm thế nào để
  2. Python làm thế nào
  3. Bình thường hóa một danh sách các số trong Python

Được tạo ra: Tháng 12-15, 2021 | Cập nhật: Tháng 4 đến 12, 2022

  1. công thức bình thường hóa
  2. Bình thường hóa danh sách các số bằng hàm MinMaxScaler trong Python Sklearn
  3. Bình thường hóa một danh sách các số theo cách thủ công trong Python

Bài viết liên quan - Danh sách Python

Làm thế nào để bạn mở rộng quy mô trong Python?

công thức bình thường hóa

Scale () làm gì trong Python?

Làm thế nào để bạn tìm thấy quy mô của một danh sách trong Python?

Làm thế nào để bạn bình thường hóa một danh sách?

Bình thường hóa danh sách các số bằng hàm MinMaxScaler trong Python Sklearn

Bình thường hóa một danh sách các số theo cách thủ công trong Python

Bài viết liên quan - Danh sách Python

# python 3.x
import numpy as np
from sklearn import preprocessing
list = np.array([6,1,0,2,7,3,8,1,5]).reshape(-1,1)
print('Original List:',list)
scaler = preprocessing.MinMaxScaler()
normalizedlist=scaler.fit_transform(list)
print('Normalized List:',normalizedlist)

Output:

Original List: [[6]
 [1]
 [0]
 [2]
 [7]
 [3]
 [8]
 [1]
 [5]]
Normalized List: [[0.75 ]
 [0.125]
 [0.   ]
 [0.25 ]
 [0.875]
 [0.375]
 [1.   ]
 [0.125]
 [0.625]]

Làm thế nào để bạn mở rộng quy mô trong Python?

Bài viết liên quan - Danh sách Python

Làm thế nào để bạn mở rộng quy mô trong Python?

Output:

Original List: [[6]
 [1]
 [0]
 [2]
 [7]
 [3]
 [8]
 [1]
 [5]]
Normalized List: [[2.25 ]
 [0.375]
 [0.   ]
 [0.75 ]
 [2.625]
 [1.125]
 [3.   ]
 [0.375]
 [1.875]]

Bình thường hóa một danh sách các số theo cách thủ công trong Python

Bài viết liên quan - Danh sách Python

Bài viết liên quan - Danh sách Python

list = [6,1,0,2,7,3,8,1,5]
print('Original List:',list)
xmin = min(list) 
xmax=max(list)
for i, x in enumerate(list):
    list[i] = (x-xmin) / (xmax-xmin)
print('Normalized List:',list)

Output:

Original List: [6, 1, 0, 2, 7, 3, 8, 1, 5]
Normalized List: [0.75, 0.125, 0.0, 0.25, 0.875, 0.375, 1.0, 0.125, 0.625]

Bài viết liên quan - Danh sách Python

  • Làm thế nào để bạn mở rộng quy mô trong Python?
  • Scale () làm gì trong Python?
  • Làm thế nào để bạn tìm thấy quy mô của một danh sách trong Python?
  • Làm thế nào để bạn bình thường hóa một danh sách?
  • Hướng dẫn scale a list python - mở rộng một danh sách python

    Làm thế nào để bạn mở rộng quy mô trong Python?

    Scale () làm gì trong Python?use a method called standardization. Where z is the new value, x is the original value, u is the mean and s is the standard deviation. Now you can compare -2.1 with -1.59 instead of comparing 790 with 1.0.

    Scale () làm gì trong Python?

    Làm thế nào để bạn tìm thấy quy mô của một danh sách trong Python?change the image size by scaling each pixel value by given columns and rows.

    Làm thế nào để bạn tìm thấy quy mô của một danh sách trong Python?

    Làm thế nào để bạn bình thường hóa một danh sách?len() to find the size of the list i.e. the length of the list. The len() method accepts an iterable as an argument and it counts and returns the number of elements present in the list.

    Làm thế nào để bạn bình thường hóa một danh sách?

    Ví dụ về các bài kiểm tra thời gian -subtract the minimum value from every number and divide it by the range i-e: max-min. So, in output, we get the normalized value of that specific number.