def get_values(self): raw_ints = self.get_raw_values() vals = {} vals["AcX"] = self.bytes_toint(raw_ints[0], raw_ints[1]) vals["AcY"] = self.bytes_toint(raw_ints[2], raw_ints[3]) vals["AcZ"] = self.bytes_toint(raw_ints[4], raw_ints[5]) vals["Tmp"] = self.bytes_toint(raw_ints[6], raw_ints[7]) / 340.00 + 36.53 vals["GyX"] = self.bytes_toint(raw_ints[8], raw_ints[9]) vals["GyY"] = self.bytes_toint(raw_ints[10], raw_ints[11]) vals["GyZ"] = self.bytes_toint(raw_ints[12], raw_ints[13]) return vals # returned in range of Int16
时间: 2023-06-03 10:05:53 浏览: 60
这个函数`get_values`会调用`get_raw_values`函数,并将返回的原始值转换为一个字典。字典中包含了加速度计和陀螺仪的 X、Y、Z 轴的值,以及当前温度的值,单位为摄氏度。其中,温度的值需要除以340.00然后加上36.53才能获得正确的结果。
相关问题
帮我改进一这段代码import machine import time from machine import I2C from machine import Pin from machine import sleep class accel(): def __init__(self, i2c, addr=0x68): self.iic = i2c self.addr = addr self.iic.start() self.iic.writeto(self.addr, bytearray([107, 0])) self.iic.stop() def get_raw_values(self): self.iic.start() a = self.iic.readfrom_mem(self.addr, 0x3B, 14) self.iic.stop() return a def get_ints(self): b = self.get_raw_values() c = [] for i in b: c.append(i) return c def bytes_toint(self, firstbyte, secondbyte): if not firstbyte & 0x80: return firstbyte << 8 | secondbyte return - (((firstbyte ^ 255) << 8) | (secondbyte ^ 255) + 1) def get_values(self): raw_ints = self.get_raw_values() vals = {} vals["AcX"] = self.bytes_toint(raw_ints[0], raw_ints[1]) vals["AcY"] = self.bytes_toint(raw_ints[2], raw_ints[3]) vals["AcZ"] = self.bytes_toint(raw_ints[4], raw_ints[5]) vals["Tmp"] = self.bytes_toint(raw_ints[6], raw_ints[7]) / 340.00 + 36.53 vals["GyX"] = self.bytes_toint(raw_ints[8], raw_ints[9]) vals["GyY"] = self.bytes_toint(raw_ints[10], raw_ints[11]) vals["GyZ"] = self.bytes_toint(raw_ints[12], raw_ints[13]) return vals # returned in range of Int16 # -32768 to 32767 def val_test(self): # ONLY FOR TESTING! Also, fast reading sometimes crashes IIC from time import sleep while 1: print(self.get_values()) sleep(0.05) clk = Pin(("clk", 36), Pin.OUT_OD) sda = Pin(("sda", 37), Pin.OUT_OD) i2c = I2C(-1, clk, sda, freq=100000) #initializing the I2C method for ESP32 #i2c = I2C(scl=Pin(5), sda=Pin(4)) #initializing the I2C method for ESP8266 mpu= accel(i2c) while True: mpu.get_values() print(mpu.get_values()) time.sleep(2)
Here are some potential improvements to the code:
1. Add comments: The code would be easier to read and understand if comments were added to explain what each section of the code does.
2. Use descriptive variable names: Some variable names are not very descriptive and could be improved. For example, "a", "b", and "c" are not very informative variable names.
3. Use list comprehension: Instead of using a for loop to append values to a list, you can use list comprehension to make the code more concise and readable.
4. Avoid unnecessary imports: The code imports the "sleep" function twice, which is unnecessary.
Here is an updated version of the code with these improvements implemented:
```
import machine
import time
from machine import I2C
from machine import Pin
class Accelerometer:
def __init__(self, i2c, addr=0x68):
"""
Initializes the accelerometer with the given I2C object and address.
"""
self.i2c = i2c
self.addr = addr
self.iic.start()
self.iic.writeto(self.addr, bytearray([107, 0]))
self.iic.stop()
def get_raw_values(self):
"""
Reads the raw values from the accelerometer and returns them as bytes.
"""
self.iic.start()
values = self.iic.readfrom_mem(self.addr, 0x3B, 14)
self.iic.stop()
return values
def bytes_to_int(self, first_byte, second_byte):
"""
Converts two bytes to a signed integer.
"""
if not first_byte & 0x80:
return first_byte << 8 | second_byte
return -(((first_byte ^ 255) << 8) | (second_byte ^ 255) + 1)
def get_values(self):
"""
Reads the values from the accelerometer and returns them as a dictionary.
"""
raw_values = self.get_raw_values()
values = {
"AcX": self.bytes_to_int(raw_values[0], raw_values[1]),
"AcY": self.bytes_to_int(raw_values[2], raw_values[3]),
"AcZ": self.bytes_to_int(raw_values[4], raw_values[5]),
"Tmp": self.bytes_to_int(raw_values[6], raw_values[7]) / 340.00 + 36.53,
"GyX": self.bytes_to_int(raw_values[8], raw_values[9]),
"GyY": self.bytes_to_int(raw_values[10], raw_values[11]),
"GyZ": self.bytes_to_int(raw_values[12], raw_values[13])
}
return values
def test_values(self):
"""
Prints the accelerometer values continuously for testing purposes.
"""
while True:
print(self.get_values())
time.sleep(0.05)
clk = Pin(("clk", 36), Pin.OUT_OD)
sda = Pin(("sda", 37), Pin.OUT_OD)
i2c = I2C(-1, clk, sda, freq=100000)
accelerometer = Accelerometer(i2c)
while True:
values = accelerometer.get_values()
print(values)
time.sleep(2)
```
y_pred = np.zeros((y_train.shape[0], 3)) for tree in forest: a = [] for j in range(X_train.shape[1]): if np.median(X_train[:, j]) > np.mean(X_train[:, j]): fuzzy_vals = fuzz.trapmf(X_train[:, j], [np.min(X_train[:, j]), np.mean(X_train[:, j]), np.median(X_train[:, j]), np.max(X_train[:, j])]) else: fuzzy_vals = fuzz.trapmf(X_train[:, j], [np.min(X_train[:, j]), np.median(X_train[:, j]), np.mean(X_train[:, j]), np.max(X_train[:, j])]) a.append(fuzzy_vals) fuzzy_vals = np.array(a).T y_pred += tree.predict_proba(fuzzy_vals) y_pred /= n_trees改成三分类预测代码
如果你的随机森林模型是二分类的,你需要做以下更改来进行三分类预测:
1. 修改 `y_pred` 的形状为 `(y_train.shape[0], 3)`,即有三个类别。
2. 对于每个决策树,预测的输出应该是每个类别的概率,而不是二分类的概率。所以你需要将 `tree.predict_proba(fuzzy_vals)` 改为 `tree.predict_proba(fuzzy_vals.reshape(-1, fuzzy_vals.shape[1], 1)).reshape(-1, 3)`。
3. 对于每个样本的预测值,你需要将它除以 `n_trees`,而不是 `n_trees` 乘以它。
以下是修改后的代码:
```
y_pred = np.zeros((y_train.shape[0], 3))
for tree in forest:
a = []
for j in range(X_train.shape[1]):
if np.median(X_train[:, j]) > np.mean(X_train[:, j]):
fuzzy_vals = fuzz.trapmf(X_train[:, j], [np.min(X_train[:, j]), np.mean(X_train[:, j]), np.median(X_train[:, j]), np.max(X_train[:, j])])
else:
fuzzy_vals = fuzz.trapmf(X_train[:, j], [np.min(X_train[:, j]), np.median(X_train[:, j]), np.mean(X_train[:, j]), np.max(X_train[:, j])])
a.append(fuzzy_vals)
fuzzy_vals = np.array(a).T
y_pred += tree.predict_proba(fuzzy_vals.reshape(-1, fuzzy_vals.shape[1], 1)).reshape(-1, 3)
y_pred /= n_trees
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)