用numpy找出a5=np.array([1, 3, 0, 0, 2, 2, 1, 3, 4, 2]) 中重复的元素并标记为True(如果元素第一次出现则标记为False)
时间: 2024-10-06 21:05:16 浏览: 37
可以使用NumPy库中的`np.unique`函数配合`return_counts=True`选项来找出数组`a5`中每个元素及其重复次数,然后通过比较原数组和唯一值数组的大小来判断元素是否重复。这里是一个步骤:
```python
import numpy as np
a5 = np.array([1, 3, 0, 0, 2, 2, 1, 3, 4, 2])
# 找出独特元素和它们的计数
unique_elements, counts = np.unique(a5, return_counts=True)
# 创建一个新的布尔数组,True表示元素在原数组中首次出现
is_unique = (counts == 1).astype(bool)
# 将原始索引对应到新的布尔数组上
repeated_indices = np.where(counts > 1)[0]
unique_indices = np.where(is_unique)[0]
print("重复元素及True值:", unique_elements[repeated_indices])
print("非重复元素及False值:", a5[unique_indices])
```
`repeated_indices`将包含所有重复元素的索引,而`unique_indices`将包含所有非重复元素的索引。
相关问题
使用C++ eigen库翻译以下python代码import pandas as pd import numpy as np import time import random def main(): eigen_list = [] data = [[1,2,4,7,6,3],[3,20,1,2,5,4],[2,0,1,5,8,6],[5,3,3,6,3,2],[6,0,5,2,19,3],[5,2,4,9,6,3]] g_csi_corr = np.cov(data, rowvar=True) #print(g_csi_corr) eigenvalue, featurevector = np.linalg.eigh(g_csi_corr) print("eigenvalue:",eigenvalue) eigen_list.append(max(eigenvalue)) #以下代码验证求解csi阈值 eigen_list.append(1.22) eigen_list.append(-54.21) eigen_list.append(8.44) eigen_list.append(-27.83) eigen_list.append(33.12) #eigen_list.append(40.29) print(eigen_list) eigen_a1 = np.array(eigen_list) num1 = len(eigen_list) eigen_a2 = eigen_a1.reshape((-1, num1)) eigen_a3 = np.std(eigen_a2, axis=0) eigen_a4 = eigen_a3.tolist() k = (0.016 - 0.014) / (max(eigen_a4) - min(eigen_a4)) eigen_a5 = [0.014 + k * (i - min(eigen_a4)) for i in eigen_a4] tri_threshold = np.mean(eigen_a5)
#include <iostream>
#include <Eigen/Dense>
using namespace Eigen;
int main()
{
std::vector<double> eigen_list;
MatrixXd data(6, 6);
data << 1, 2, 4, 7, 6, 3,
3, 20, 1, 2, 5, 4,
2, 0, 1, 5, 8, 6,
5, 3, 3, 6, 3, 2,
6, 0, 5, 2, 19, 3,
5, 2, 4, 9, 6, 3;
MatrixXd g_csi_corr = data.transpose() * data / 6.0;
EigenSolver<MatrixXd> es(g_csi_corr);
VectorXd eigenvalue = es.eigenvalues().real();
std::cout << "eigenvalue: " << eigenvalue.transpose() << std::endl;
eigen_list.push_back(eigenvalue.maxCoeff());
eigen_list.push_back(1.22);
eigen_list.push_back(-54.21);
eigen_list.push_back(8.44);
eigen_list.push_back(-27.83);
eigen_list.push_back(33.12);
//eigen_list.push_back(40.29);
std::cout << "eigen_list: ";
for (std::vector<double>::iterator it = eigen_list.begin(); it != eigen_list.end(); ++it)
std::cout << *it << " ";
std::cout << std::endl;
int num1 = eigen_list.size();
MatrixXd eigen_a2 = Map<MatrixXd>(eigen_list.data(), num1, 1);
VectorXd eigen_a3 = eigen_a2.array().rowwise().mean().transpose();
VectorXd eigen_a4 = (eigen_a2 - eigen_a3.replicate(num1, 1)).array().abs().rowwise().mean().transpose();
double k = 0.002 / (eigen_a4.maxCoeff() - eigen_a4.minCoeff());
VectorXd eigen_a5 = 0.014 + k * (eigen_a4.array() - eigen_a4.minCoeff());
double tri_threshold = eigen_a5.mean();
std::cout << "tri_threshold: " << tri_threshold << std::endl;
return 0;
}
import numpy as np import pylab as pl import pandas as pd import numpy as np from scipy.optimize import leastsq X2=[] X3=[] X4=[] X5=[] X6=[] X7=[] X1=[i for i in range(1,24) for j in range(128)] df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(3,)) X2=df.values.tolist() x2=[] for i in X2: if X2.index(i)>2927: #两个单元楼的分隔数 x2.append(i) df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(4,)) X3=df.values.tolist() x3=[] for i in X3: if X3.index(i)>2927: x3.append(i) df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(5,)) X4=df.values.tolist() x4=[] for i in X4: if X4.index(i)>2927: x4.append(i) df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(6,)) X5=df.values.tolist() x5=[] for i in X5: if X5.index(i)>2927: x5.append(i) df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(7,)) X6=df.values.tolist() x6=[] for i in X6: if X6.index(i)>2927: x6.append(i) df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(8,)) X7=df.values.tolist() x7=[] for i in X7: if X7.index(i)>2927: x7.append(i) df=pd.read_excel('C:/Users/86147/OneDrive/文档/66666.xlsx',header=0,usecols=(1,)) mylist1=df.values.tolist() room=[] for i in mylist1: room.append(i[0]) df=pd.read_excel('C:/Users/86147/OneDrive/文档/66666.xlsx',header=0,usecols=(2,)) mylist1=df.values.tolist() tomp=[] for i in mylist1: tomp.append(i[0]) Y=[] for i in range(1,185): room_tomp=zip(room,tomp) ls=[] for k,v in room_tomp: if k<=92: ls.append(v) for w in range(32): Y.append(ls[w])#通过循环y对应列表共有2944个数据 q=X1[:2922] w=X2[:2922] e=X3[:2922] r=X4[:2922] t=X5[:2922] p=X6[:2922] u=X7[:2922] x=np.column_stack((q,w,e,r,t,p,u)).T y=np.array(Y[:2922]).T # 定义待拟合的函数 def func(params, x, y): a1, a2, a3, a4, a5, a6, a7, b = paramsreturn a1 * x[:,0] + a2 * x[:,1] + a3 * x[:,2] + a4 * x[:,3] + a5 * x[:,4] + a6 * x[:,5] + a7 * x[:,6] + b - y # 求解参数 params0 = np.ones(8) # 初始参数 params, flag = leastsq(func, params0, args=(x, y)) # 求解参数 # 输出结果 print(f"a1: {params[0]}, a2: {params[1]}, a3: {params[2]}, a4: {params[3]}, a5: {params[4]}, a6: {params[5]}, a7: {params[6]}, b: {params[7]}")修改这个代码要求其可以准确的求出参数
这段代码使用最小二乘法拟合了一个多元线性回归模型,其中每个自变量的系数需要通过拟合得到。为了准确地求出参数,你可以尝试以下几点:
1. 确认数据的准确性:检查读入的 Excel 文件是否正确,是否包含缺失值或异常值。
2. 调整模型:可以尝试增加或减少自变量的数量,或者对模型进行其他的调整。
3. 调整初始参数:可以尝试使用不同的初始参数,以便更好地搜索最优解。
4. 调整参数的约束条件:可以尝试添加参数的约束条件,如限制系数的范围或设置参数之间的关系等,以便更好地控制拟合结果。
阅读全文